Difference between revisions of "Input"
(Created page with "<Input> is a [https://reactjs.org/docs/react-component.html React component] which is primarily a convenience wrapper for <input>. Using it is not required for <Form> - you ca...") |
|||
| (5 intermediate revisions by the same user not shown) | |||
| Line 1: | Line 1: | ||
<Input> is a [https://reactjs.org/docs/react-component.html React component] which is primarily a convenience wrapper for <input>. Using it is not required for <Form> - you can mix and match regular <input> fields inside a form and they will still be submitted. | <Input> is a [https://reactjs.org/docs/react-component.html React component] which is primarily a convenience wrapper for <input>. Using it is not required for [[Forms|<Form>]] - you can mix and match regular <input> fields inside a form and they will still be submitted. | ||
=Form example= | =Form example= | ||
| Line 17: | Line 17: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
=Props= | =Props= | ||
This is the main available props for the < | This is the main available props for the <Input> react component. | ||
(todo!) | |||
{| class="wikitable" | {| class="wikitable" | ||
| Line 34: | Line 35: | ||
| No | | No | ||
| none | | none | ||
} | |} | ||
=Validation= | =Validation= | ||
| Line 50: | Line 51: | ||
<syntaxhighlight lang="javascript"> | <syntaxhighlight lang="javascript"> | ||
export default value => { | export default value => { | ||
if(!value || (value.trim && value.trim() == '')){ | if(!value || (value.trim && value.trim() == '')){ | ||
return { | return { | ||
| Line 59: | Line 59: | ||
}; | }; | ||
} | } | ||
} | |||
</syntaxhighlight> | </syntaxhighlight> | ||
Latest revision as of 15:38, 9 November 2021
<Input> is a React component which is primarily a convenience wrapper for <input>. Using it is not required for <Form> - you can mix and match regular <input> fields inside a form and they will still be submitted.
Form example
import Form from 'UI/Form';
import Input from 'UI/Input';
...
<Form action="user" submitLabel="Create a user" successMessage="User created!" failureMessage="Unable to create a user now">
<Input type="text" name="email" label="Email Address" validate={["Required"]} />
<Input type="password" name="password" label="Email Address" />
</Form>
Props
This is the main available props for the <Input> react component.
(todo!)
| Name | About | Required? | Default value |
|---|---|---|---|
| validate | It's an array of validators. See the section about validation below. | No | none |
Validation
The validate prop can be used to provide an array of validators. Each one is either a string or a function. If you provide a string, it will be treated as the name of a module under UI/Functions/Validation. Let's take the example above:
<Input type="text" name="email" label="Email Address" validate={["Required"]} />
This string validator, "Required", will be a function imported from UI/Functions/Validation/Required.
The validator functions themselves are relatively simple - they are given the input fields value as their input, and do nothing if the input was valid. Otherwise, they return an error with a visual version as well as a textual error code. Here is the source of Required itself:
export default value => {
if(!value || (value.trim && value.trim() == '')){
return {
error: 'EMPTY',
ui: <Text>
{`This field is required`}
</Text>
};
}
}