-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7a104e2
commit 6d3f204
Showing
4 changed files
with
152 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import FieldArrayRepro from './repros/FieldArrayRepro'; | ||
import React from 'react'; | ||
|
||
export default { | ||
title: 'Repros', | ||
}; | ||
|
||
export function FieldArrayReproStory() { | ||
return <FieldArrayRepro />; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
import { shuffle } from 'lodash'; | ||
import * as React from 'react'; | ||
import { useFieldArray, useForm, withFormProvider } from '../../FormProvider'; | ||
import Button from '../utils/Button'; | ||
import { InputField } from '../utils/Fields'; | ||
import MetaData from '../utils/MetaData'; | ||
|
||
function FieldArrayRepro() { | ||
const [formData, setFormData] = React.useState({}); | ||
|
||
function onSubmit(values: any, extra: any) { | ||
setFormData({ values, extra, time: new Date().toString() }); | ||
} | ||
|
||
const { handleSubmit, resetInitialValues, validateFields } = useForm({ | ||
onSubmit, | ||
onError: (error) => setFormData((value) => ({ ...value, error })), | ||
initialValues: { | ||
groupBy: [ | ||
{ | ||
id: 'customerId', | ||
label: 'Customer', | ||
sort: 'asc', | ||
}, | ||
{ | ||
id: 'itemId', | ||
label: 'Item', | ||
sort: 'desc', | ||
}, | ||
], | ||
}, | ||
}); | ||
|
||
const fields = ['id', 'label', 'sort']; | ||
|
||
const { | ||
fieldArrayProps, | ||
remove, | ||
append, | ||
error, | ||
getFieldArrayValue, | ||
setFieldArrayValue, | ||
} = useFieldArray({ | ||
fieldNames: fields, | ||
name: 'groupBy', | ||
}); | ||
|
||
function reorder() { | ||
const values = getFieldArrayValue(); | ||
|
||
const temp = shuffle(values); | ||
|
||
setFieldArrayValue(temp); | ||
} | ||
|
||
return ( | ||
<div> | ||
<form onSubmit={handleSubmit}> | ||
<InputField name="name" type="text" /> | ||
|
||
<div> | ||
<table> | ||
<tbody> | ||
{fieldArrayProps.rowIds.map((r, idx) => { | ||
return ( | ||
<tr key={r}> | ||
<React.Fragment> | ||
{fields.map((f) => ( | ||
<td key={f} className="px-2"> | ||
<InputField | ||
ancestors={[{ name: 'groupBy', rowId: r }]} | ||
name={f} | ||
type="text" | ||
validate={(value) => | ||
!value ? `Value missing` : '' | ||
} | ||
/> | ||
</td> | ||
))} | ||
<td className="px-2"> | ||
<div className="flex gap-2"> | ||
<Button | ||
small | ||
color="red" | ||
type="button" | ||
onClick={() => remove(idx)} | ||
> | ||
Remove | ||
</Button> | ||
</div> | ||
</td> | ||
</React.Fragment> | ||
</tr> | ||
); | ||
})} | ||
</tbody> | ||
</table> | ||
<Button small type="button" onClick={() => append()}> | ||
Add Row | ||
</Button> | ||
{error && <div style={{ color: 'red' }}>{error}</div>} | ||
</div> | ||
|
||
<br /> | ||
|
||
<div className="flex gap-4"> | ||
<Button type="submit" primary> | ||
Submit | ||
</Button> | ||
<Button type="button" onClick={() => resetInitialValues()}> | ||
Reset | ||
</Button> | ||
<Button | ||
type="button" | ||
onClick={() => | ||
validateFields([{ name: 'items', type: 'field-array' }]) | ||
} | ||
> | ||
Validate | ||
</Button> | ||
<Button onClick={reorder} color="green"> | ||
Reorder | ||
</Button> | ||
</div> | ||
</form> | ||
|
||
<br /> | ||
|
||
<MetaData formData={formData} /> | ||
</div> | ||
); | ||
} | ||
|
||
export default withFormProvider(FieldArrayRepro); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters