-
-
Notifications
You must be signed in to change notification settings - Fork 921
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Form): add support for
nestedField
prop on Form (#3568)
* feat(Form): add support for `nestedField` prop on Form * test: fix tests * test: add tests for getFieldError and getFieldValue * test(Form): add test for onSubmit * fix: some of your tests did a full page reload! * docs(Form): form accessibility * fix: some of your tests did a full page reload!
- Loading branch information
Showing
16 changed files
with
458 additions
and
225 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
87 changes: 87 additions & 0 deletions
87
docs/pages/components/form-validation/fragments/form-nested-fields.md
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,87 @@ | ||
<!--start-code--> | ||
|
||
```js | ||
import { Form, Button, ButtonToolbar, Schema, Panel, FlexboxGrid } from 'rsuite'; | ||
import JSONTree from 'react-json-tree'; | ||
|
||
const JSONView = ({ formValue, formError }) => ( | ||
<div style={{ marginBottom: 10 }}> | ||
<Panel className="json-tree-wrapper" header={<p>formValue</p>}> | ||
<JSONTree data={formValue} /> | ||
</Panel> | ||
|
||
<Panel className="json-tree-wrapper" header={<p>formError</p>}> | ||
<JSONTree data={formError} /> | ||
</Panel> | ||
</div> | ||
); | ||
|
||
const { StringType, ObjectType, NumberType } = Schema.Types; | ||
|
||
const model = Schema.Model({ | ||
name: StringType().isRequired('This field is required.'), | ||
address: ObjectType().shape({ | ||
city: StringType().isRequired('This field is required.'), | ||
postCode: NumberType('Post Code must be a number').isRequired('This field is required.') | ||
}) | ||
}); | ||
|
||
const TextField = React.forwardRef((props, ref) => { | ||
const { name, label, accepter, ...rest } = props; | ||
return ( | ||
<Form.Group ref={ref}> | ||
<Form.ControlLabel>{label} </Form.ControlLabel> | ||
<Form.Control name={name} accepter={accepter} {...rest} /> | ||
</Form.Group> | ||
); | ||
}); | ||
|
||
const App = () => { | ||
const form = React.useRef(); | ||
const [formError, setFormError] = React.useState({}); | ||
const [formValue, setFormValue] = React.useState({ | ||
name: 'Tom', | ||
address: { city: 'ShangHai', postCode: '200000' } | ||
}); | ||
|
||
const handleSubmit = () => { | ||
if (!form.current.check()) { | ||
console.error('Form Error'); | ||
return; | ||
} | ||
console.log(formValue, 'Form Value'); | ||
}; | ||
|
||
return ( | ||
<FlexboxGrid> | ||
<FlexboxGrid.Item colspan={12}> | ||
<Form | ||
nestedField | ||
ref={form} | ||
onChange={setFormValue} | ||
onCheck={setFormError} | ||
formValue={formValue} | ||
model={model} | ||
> | ||
<TextField name="name" label="Name" /> | ||
<TextField name="address.city" label="City" /> | ||
<TextField name="address.postCode" label="Post Code" /> | ||
|
||
<ButtonToolbar> | ||
<Button appearance="primary" onClick={handleSubmit}> | ||
Submit | ||
</Button> | ||
</ButtonToolbar> | ||
</Form> | ||
</FlexboxGrid.Item> | ||
<FlexboxGrid.Item colspan={12}> | ||
<JSONView formValue={formValue} formError={formError} /> | ||
</FlexboxGrid.Item> | ||
</FlexboxGrid> | ||
); | ||
}; | ||
|
||
ReactDOM.render(<App />, document.getElementById('root')); | ||
``` | ||
|
||
<!--end-code--> |
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
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 |
---|---|---|
|
@@ -75,7 +75,6 @@ const App = () => { | |
|
||
return ( | ||
<Panel> | ||
dsdfsdf | ||
<Form | ||
disabled={disabled} | ||
readOnly={readOnly} | ||
|
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
Oops, something went wrong.
47fce55
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.
Successfully deployed to the following URLs:
rsuite-nextjs – ./docs
rsuite.vercel.app
rsuite-nextjs-git-main-rsuite.vercel.app
rsuite-nextjs-rsuite.vercel.app
rsuitejs.com