New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
💈 useFieldArray #768
💈 useFieldArray #768
Changes from 2 commits
cc58895
cb3d29a
ec645a7
bf1239a
9769801
1fbd0e1
b678705
f629411
04039cd
871763e
d13603b
0b2d44f
839943b
22b1611
cad0d07
37775c6
2ee8635
e1a7852
d2d6a12
3e5620d
304aabd
8cf3b5a
29b065d
6e20217
b140e21
f35a366
2b74e6f
cbb11ff
89bc9a1
7d6a54e
a0733ba
8b36aa1
8212b89
79b9e95
2bc7c98
ac79959
e46ae00
a8d871c
e84983b
8258961
bac8d44
e313cb8
2d38446
b4b5972
18494b1
909b567
6f48109
11ee93b
98ac209
df0d93e
403a943
ca10818
ab07689
8e17052
1b5f549
6c840c2
8b26220
fe5e374
cbd81ae
e286eb7
a5a8958
e2d58e9
66c0d9d
68652af
2b06089
7acf6f1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import * as React from 'react'; | ||
import isUndefined from './utils/isUndefined'; | ||
|
||
const id = () => | ||
'x'.repeat(40).replace(/x/g, c => { | ||
const r = (Math.random() * 16) | 0; | ||
return (c === 'x' ? r : (r & 0x3) | 0x8).toString(16); | ||
}); | ||
|
||
const appendId = (value: any) => ({ | ||
...value, | ||
...(value.id ? {} : { id: id() }), | ||
}); | ||
|
||
export function useFieldArray({ | ||
getValues, | ||
name, | ||
}: { | ||
getValues: (payload: { nest: boolean }) => any; | ||
name: string; | ||
}) { | ||
const getData = () => getValues({ nest: true })[name]; | ||
const [fields, setField] = React.useState<any[]>( | ||
(getData() || []).map((value: any) => appendId(value)), | ||
); | ||
|
||
const prepend = (value: any) => setField([appendId(value), ...fields]); | ||
|
||
const append = (value: any) => setField([...fields, appendId(value)]); | ||
|
||
const remove = (index?: number) => | ||
setField( | ||
isUndefined(index) | ||
? [] | ||
: [...fields.slice(0, index), ...fields.slice(index + 1)], | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
); | ||
|
||
const update = (index: number, value: any) => { | ||
const data = getData(); | ||
data[index] = appendId(value); | ||
setField([...data]); | ||
}; | ||
|
||
const insert = (index: number, value: any) => { | ||
setField([...fields.slice(0, index), value, ...fields.slice(index)]); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it better than using const newFields = [...fields];
newFields.splice(index, 0, value);
setField(newFields); or is it about fewer lines the better or referential integrity of the values? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I try to avoid |
||
}; | ||
|
||
return { | ||
prepend, | ||
append, | ||
remove, | ||
update, | ||
insert, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it should be named as There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think insert is fine, less text to type as well. we have TS support the type as the argument :) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see. Sounds good to me :) Thanks |
||
fields, | ||
}; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we could also name this
generateId
insteadThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sounds good