-
-
Notifications
You must be signed in to change notification settings - Fork 272
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* chore: init ts def * chore: support validateOnly for only check * feat: validateOnly support * test: add test case * chore: fix ts
- Loading branch information
Showing
7 changed files
with
143 additions
and
18 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,3 @@ | ||
## validateOnly | ||
|
||
<code src="../examples/validateOnly.tsx" /> |
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,76 @@ | ||
/* eslint-disable react/prop-types, @typescript-eslint/consistent-type-imports */ | ||
|
||
import React from 'react'; | ||
import Form from 'rc-field-form'; | ||
import type { FormInstance } from 'rc-field-form'; | ||
import Input from './components/Input'; | ||
import LabelField from './components/LabelField'; | ||
|
||
function useSubmittable(form: FormInstance) { | ||
const [submittable, setSubmittable] = React.useState(false); | ||
const store = Form.useWatch([], form); | ||
|
||
React.useEffect(() => { | ||
form | ||
.validateFields({ | ||
validateOnly: true, | ||
}) | ||
.then( | ||
() => { | ||
setSubmittable(true); | ||
}, | ||
() => { | ||
setSubmittable(false); | ||
}, | ||
); | ||
}, [store]); | ||
|
||
return submittable; | ||
} | ||
|
||
export default () => { | ||
const [form] = Form.useForm(); | ||
|
||
const canSubmit = useSubmittable(form); | ||
|
||
const onValidateOnly = async () => { | ||
const result = await form.validateFields({ | ||
validateOnly: true, | ||
}); | ||
console.log('Validate:', result); | ||
}; | ||
|
||
return ( | ||
<> | ||
<Form form={form}> | ||
<LabelField | ||
name="name" | ||
label="Name" | ||
rules={[ | ||
{ required: true }, | ||
// { warningOnly: true, validator: () => Promise.reject('Warn Name!') }, | ||
]} | ||
> | ||
<Input /> | ||
</LabelField> | ||
<LabelField | ||
name="age" | ||
label="Age" | ||
rules={[ | ||
{ required: true }, | ||
// { warningOnly: true, validator: () => Promise.reject('Warn Age!') }, | ||
]} | ||
> | ||
<Input /> | ||
</LabelField> | ||
<button type="reset">Reset</button> | ||
<button type="submit" disabled={!canSubmit}> | ||
Submit | ||
</button> | ||
</Form> | ||
<button type="button" onClick={onValidateOnly}> | ||
Validate Without UI update | ||
</button> | ||
</> | ||
); | ||
}; |
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
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