File tree Expand file tree Collapse file tree 4 files changed +83
-14
lines changed
Expand file tree Collapse file tree 4 files changed +83
-14
lines changed Original file line number Diff line number Diff line change 11import React from 'react'
22
3- import { ExampleComponent } from 'react-dynamic-fields'
3+ import { SingleField } from 'react-dynamic-fields'
44import 'react-dynamic-fields/dist/index.css'
55
66const 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
1019export default App
Original file line number Diff line number Diff line change 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 ,
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}
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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 }
You can’t perform that action at this time.
0 commit comments