Skip to content

Commit d9df3f6

Browse files
authored
feat: List support initialValues (#224)
* feat: List support initialValues * docs: Update demo
1 parent 401e848 commit d9df3f6

File tree

3 files changed

+33
-4
lines changed

3 files changed

+33
-4
lines changed

docs/examples/list.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,15 @@ const Demo = () => {
2222
}}
2323
style={{ border: '1px solid red', padding: 15 }}
2424
preserve={false}
25-
initialValues={{
26-
users: ['little'],
27-
}}
25+
// initialValues={{
26+
// users: ['little'],
27+
// }}
2828
>
2929
<Form.Field shouldUpdate>{() => JSON.stringify(form.getFieldsValue(), null, 2)}</Form.Field>
3030

3131
<List
3232
name="users"
33+
initialValue={['bamboo', 'light']}
3334
rules={[
3435
{
3536
message: 'Must have at least 2 user!',

src/List.tsx

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,21 @@ export interface ListProps {
2121
name: NamePath;
2222
rules?: ValidatorRule[];
2323
validateTrigger?: string | string[] | false;
24+
initialValue?: any[];
2425
children?: (
2526
fields: ListField[],
2627
operations: ListOperations,
2728
meta: Meta,
2829
) => JSX.Element | React.ReactNode;
2930
}
3031

31-
const List: React.FunctionComponent<ListProps> = ({ name, children, rules, validateTrigger }) => {
32+
const List: React.FunctionComponent<ListProps> = ({
33+
name,
34+
initialValue,
35+
children,
36+
rules,
37+
validateTrigger,
38+
}) => {
3239
const context = React.useContext(FieldContext);
3340
const keyRef = React.useRef({
3441
keys: [],
@@ -59,6 +66,7 @@ const List: React.FunctionComponent<ListProps> = ({ name, children, rules, valid
5966
shouldUpdate={shouldUpdate}
6067
rules={rules}
6168
validateTrigger={validateTrigger}
69+
initialValue={initialValue}
6270
isList
6371
>
6472
{({ value = [], onChange }, meta) => {

tests/list.test.tsx

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -739,4 +739,24 @@ describe('Form.List', () => {
739739
expect(form.isFieldsTouched(['list'], true)).toBeTruthy();
740740
});
741741
});
742+
743+
it('initialValue', () => {
744+
generateForm(
745+
fields => (
746+
<div>
747+
{fields.map(field => (
748+
<Field {...field}>
749+
<Input />
750+
</Field>
751+
))}
752+
</div>
753+
),
754+
null,
755+
{ initialValue: ['light', 'bamboo'] },
756+
);
757+
758+
expect(form.getFieldsValue()).toEqual({
759+
list: ['light', 'bamboo'],
760+
});
761+
});
742762
});

0 commit comments

Comments
 (0)