Skip to content
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

Merged
merged 66 commits into from Jan 14, 2020
Merged
Changes from 2 commits
Commits
Show all changes
66 commits
Select commit Hold shift + click to select a range
cc58895
POC for useFieldArray
bluebill1049 Jan 4, 2020
cb3d29a
clean up code
bluebill1049 Jan 4, 2020
ec645a7
update from code review
bluebill1049 Jan 4, 2020
bf1239a
adding unit tests
bluebill1049 Jan 4, 2020
9769801
include use context API
bluebill1049 Jan 4, 2020
1fbd0e1
update with more unit tests
bluebill1049 Jan 4, 2020
b678705
remove update
bluebill1049 Jan 4, 2020
f629411
use control prop instead of individual props
bluebill1049 Jan 4, 2020
04039cd
fix delete issue with useFieldArray
bluebill1049 Jan 4, 2020
871763e
add new method swap
bluebill1049 Jan 4, 2020
d13603b
include move
bluebill1049 Jan 4, 2020
0b2d44f
chnage any to objec type
bluebill1049 Jan 4, 2020
839943b
chnage any to objec type
bluebill1049 Jan 4, 2020
22b1611
fix type
bluebill1049 Jan 4, 2020
cad0d07
include more unit tests for methods
bluebill1049 Jan 4, 2020
37775c6
automation working in progress
bluebill1049 Jan 5, 2020
2ee8635
Merge branch 'master' into useFieldArray
bluebill1049 Jan 5, 2020
e1a7852
WIP with automation
bluebill1049 Jan 5, 2020
d2d6a12
pass automation test
bluebill1049 Jan 5, 2020
3e5620d
Merge branch 'master' into useFieldArray
bluebill1049 Jan 5, 2020
304aabd
update generate ID function
bluebill1049 Jan 5, 2020
8cf3b5a
ts-ignore generate ID function
bluebill1049 Jan 5, 2020
29b065d
Merge branch 'master' into useFieldArray
bluebill1049 Jan 5, 2020
6e20217
add generics for useFieldArray
kotarella1110 Jan 6, 2020
b140e21
rename type from WithOptionalId to WithFieldId
kotarella1110 Jan 6, 2020
f35a366
fix useFieldArray app
kotarella1110 Jan 6, 2020
2b74e6f
update unit test with default value useFieldArray
bluebill1049 Jan 6, 2020
cbb11ff
Merge branch 'master' into useFieldArray
bluebill1049 Jan 7, 2020
89bc9a1
Merge branch 'master' into useFieldArray
bluebill1049 Jan 7, 2020
7d6a54e
fix an issue when user add or delete items
bluebill1049 Jan 7, 2020
a0733ba
update unit tests
bluebill1049 Jan 7, 2020
8b36aa1
fix issue around reset with useForm
bluebill1049 Jan 7, 2020
8212b89
code clean up
bluebill1049 Jan 7, 2020
79b9e95
clean up logic on reset
bluebill1049 Jan 7, 2020
2bc7c98
fix a bug catched with automation
bluebill1049 Jan 7, 2020
ac79959
Merge branch 'master' into useFieldArray
bluebill1049 Jan 7, 2020
e46ae00
Merge branch 'master' into useFieldArray
bluebill1049 Jan 7, 2020
a8d871c
Merge branch 'master' into useFieldArray
bluebill1049 Jan 8, 2020
e84983b
remove generate id with crypto
bluebill1049 Jan 8, 2020
8258961
Merge branch 'master' into useFieldArray
bluebill1049 Jan 8, 2020
bac8d44
remove fields reference in control
bluebill1049 Jan 9, 2020
e313cb8
Merge branch 'master' into useFieldArray
bluebill1049 Jan 9, 2020
2d38446
Merge branch 'master' into useFieldArray
bluebill1049 Jan 9, 2020
b4b5972
Merge branch 'master' into useFieldArray
bluebill1049 Jan 10, 2020
18494b1
bring back the fields
bluebill1049 Jan 10, 2020
909b567
Update package.json
bluebill1049 Jan 10, 2020
6f48109
fix issue around delete node
bluebill1049 Jan 11, 2020
11ee93b
fix automation and issue around api
bluebill1049 Jan 11, 2020
98ac209
update unit test
bluebill1049 Jan 11, 2020
df0d93e
fix automation script
bluebill1049 Jan 11, 2020
403a943
code clean up
bluebill1049 Jan 11, 2020
ca10818
fix a bug with move and update automation with submit data
bluebill1049 Jan 11, 2020
ab07689
fix unit tests
bluebill1049 Jan 11, 2020
8e17052
include generate id with performance.now
bluebill1049 Jan 12, 2020
1b5f549
remove unnecessary variable
bluebill1049 Jan 12, 2020
6c840c2
clean up generateID
bluebill1049 Jan 12, 2020
8b26220
add sideEffect false
bluebill1049 Jan 12, 2020
fe5e374
revert sideeffect
bluebill1049 Jan 12, 2020
cbd81ae
Merge branch 'master' into useFieldArray
bluebill1049 Jan 13, 2020
e286eb7
fix issue around defaultValues
bluebill1049 Jan 13, 2020
a5a8958
minor code clean up
bluebill1049 Jan 13, 2020
e2d58e9
convert string check logic into a function
bluebill1049 Jan 13, 2020
66c0d9d
clean up the code
bluebill1049 Jan 13, 2020
68652af
code clean up at reset
bluebill1049 Jan 13, 2020
2b06089
more code clean up
bluebill1049 Jan 13, 2020
7acf6f1
fix review comment
bluebill1049 Jan 14, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
56 changes: 56 additions & 0 deletions src/useFieldArray.ts
@@ -0,0 +1,56 @@
import * as React from 'react';
import isUndefined from './utils/isUndefined';

const id = () =>
Copy link
Contributor

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 instead

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sounds good

'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)],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

splice?

);

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)]);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it better than using splice?

const newFields = [...fields];
newFields.splice(index, 0, value);
setField(newFields);

or is it about fewer lines the better or referential integrity of the values?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I try to avoid splice cause it's a mutation, slice is always return new Array

};

return {
prepend,
append,
remove,
update,
insert,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it should be named as insertAt because insert is more like a generic term for insertion of values. Same goes with update

Copy link
Member Author

Choose a reason for hiding this comment

The 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 :)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see. Sounds good to me :) Thanks

fields,
};
}