Skip to content

Commit aef10fa

Browse files
committed
add single field option
1 parent 2f55cae commit aef10fa

File tree

4 files changed

+83
-14
lines changed

4 files changed

+83
-14
lines changed

example/src/App.tsx

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,19 @@
11
import React from 'react'
22

3-
import { ExampleComponent } from 'react-dynamic-fields'
3+
import { SingleField } from 'react-dynamic-fields'
44
import 'react-dynamic-fields/dist/index.css'
55

66
const App = () => {
7-
return <ExampleComponent text="Create React Library Example 😄" />
7+
const [options, setOptions] = React.useState([''])
8+
return (
9+
<SingleField
10+
//@ts-ignore
11+
options={options}
12+
//@ts-ignore
13+
setOptions={setOptions}
14+
label='Types of fruits'
15+
/>
16+
)
817
}
918

1019
export default App

example/tsconfig.json

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22
"compilerOptions": {
33
"outDir": "dist",
44
"module": "esnext",
5-
"lib": ["dom", "esnext"],
5+
"lib": [
6+
"dom",
7+
"esnext"
8+
],
69
"moduleResolution": "node",
710
"jsx": "react",
811
"sourceMap": true,
@@ -15,8 +18,21 @@
1518
"suppressImplicitAnyIndexErrors": true,
1619
"noUnusedLocals": true,
1720
"noUnusedParameters": true,
18-
"allowSyntheticDefaultImports": true
21+
"allowSyntheticDefaultImports": true,
22+
"target": "es5",
23+
"allowJs": true,
24+
"skipLibCheck": true,
25+
"strict": true,
26+
"forceConsistentCasingInFileNames": true,
27+
"resolveJsonModule": true,
28+
"isolatedModules": true,
29+
"noEmit": true
1930
},
20-
"include": ["src"],
21-
"exclude": ["node_modules", "build"]
31+
"include": [
32+
"src"
33+
],
34+
"exclude": [
35+
"node_modules",
36+
"build"
37+
]
2238
}

src/SingleField.tsx

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import * as React from 'react'
2+
3+
interface Props {
4+
options: [string]
5+
setOptions: (x: any) => {}
6+
label: string
7+
}
8+
9+
export default function SingleField({ options, setOptions, label }: Props) {
10+
function handleOptionChange(
11+
event: React.ChangeEvent<HTMLInputElement>,
12+
index: number
13+
) {
14+
const values = options
15+
values[index] = event.target.value
16+
setOptions([...values])
17+
}
18+
19+
function handleAddOption() {
20+
setOptions([...options, ''])
21+
}
22+
23+
function handleRemoveOption(index: number) {
24+
const values = options
25+
values.splice(index, 1)
26+
setOptions([...values])
27+
}
28+
29+
return (
30+
<React.Fragment>
31+
{options.map((_option, i) => (
32+
<div key={i}>
33+
<input
34+
placeholder={`type your ${label}`}
35+
value={options[i]}
36+
onChange={(event) => handleOptionChange(event, i)}
37+
autoFocus
38+
/>
39+
<button onClick={() => handleRemoveOption(i)}>remove</button>
40+
</div>
41+
))}
42+
<button onClick={handleAddOption}>{`add ${label}`}</button>
43+
</React.Fragment>
44+
)
45+
}

src/index.tsx

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
import * as React from 'react'
2-
import styles from './styles.module.css'
1+
// import * as React from 'react'
2+
// import styles from './styles.module.css'
3+
import SingleField from './SingleField'
34

4-
interface Props {
5-
text: string
6-
}
5+
// interface Props {
6+
// text: string
7+
// }
78

8-
export const ExampleComponent = ({ text }: Props) => {
9-
return <div className={styles.test}>Example Component: {text}</div>
10-
}
9+
export { SingleField }

0 commit comments

Comments
 (0)