1 | # Terra Form
|
2 |
|
3 | Components for building forms
|
4 |
|
5 | ## Getting Started
|
6 |
|
7 | - Install with [npmjs](https://www.npmjs.com):
|
8 | - `npm install terra-form`
|
9 | - `yarn add terra-form`
|
10 |
|
11 | ## Usage
|
12 |
|
13 | Terra has several different form components for building forms.
|
14 |
|
15 | [Field](https://github.com/cerner/terra-core/tree/master/packages/terra-form/docs/Field.md)
|
16 |
|
17 | [Fieldset](https://github.com/cerner/terra-core/tree/master/packages/terra-form/docs/Fieldset.md)
|
18 |
|
19 | [Input](https://github.com/cerner/terra-core/tree/master/packages/terra-form/docs/Input.md)
|
20 |
|
21 | [Control](https://github.com/cerner/terra-core/tree/master/packages/terra-form/docs/Control.md)
|
22 |
|
23 | [NumberField](https://github.com/cerner/terra-core/tree/master/packages/terra-form/docs/NumberField.md)
|
24 |
|
25 | [Textarea](https://github.com/cerner/terra-core/tree/master/packages/terra-form/docs/Textarea.md)
|
26 |
|
27 | [TextareaField](https://github.com/cerner/terra-core/tree/master/packages/terra-form/docs/TextareaField.md)
|
28 |
|
29 | [TextField](https://github.com/cerner/terra-core/tree/master/packages/terra-form/docs/TextField.md)
|
30 |
|
31 | [Select](https://github.com/cerner/terra-core/tree/master/packages/terra-form/docs/Select.md)
|
32 |
|
33 | [SelectField](https://github.com/cerner/terra-core/tree/master/packages/terra-form/docs/SelectField.md)
|
34 |
|
35 | ## Component Features
|
36 | * [Cross-Browser Support](https://github.com/cerner/terra-core/wiki/Component-Features#cross-browser-support)
|
37 | * [Responsive Support](https://github.com/cerner/terra-core/wiki/Component-Features#responsive-support)
|
38 | * [Mobile Support](https://github.com/cerner/terra-core/wiki/Component-Features#mobile-support)
|
39 |
|
40 |
|
41 | To use these components, create a React component that contains a form element, and then insert the Terra form components into that form.
|
42 |
|
43 | When using input fields, React has two ways of using inputs: [controlled](https://facebook.github.io/react/docs/forms.html#controlled-components) or [uncontrolled](https://facebook.github.io/react/docs/uncontrolled-components.html). Controlled input are designed to attach the value of the input to the state of the component, while uncontrolled inputs are left to operate as normal html inputs would. If you would like to use a controlled input (ideal for forms that need to do validations with JavaScript and send ajax requests), provide a value and an onChange function. If you would like to use an uncontrolled input (ideal for forms that do straight backend requests), provide a defaultValue into the input.
|
44 |
|
45 | **Note: If you provide both a value and defaultValue to the input, React will be confused as to whether you are working with an uncontrolled input or a controlled one. Provide either a value or a defaultValue to the input, but not both.**
|
46 |
|
47 | ## Examples
|
48 |
|
49 | ### Controlled Input
|
50 |
|
51 | class Form extends React.Component {
|
52 | constructor(props) {
|
53 | super(props);
|
54 |
|
55 | this.state = {
|
56 | formData: {
|
57 | name: 'Joe'
|
58 | },
|
59 | };
|
60 |
|
61 | this.submitForm = this.submitForm.bind(this);
|
62 | this.updateFormInput = this.updateFormInput.bind(this);
|
63 | }
|
64 |
|
65 | submitForm(e) {
|
66 | e.preventDefault();
|
67 | }
|
68 |
|
69 | updateFormInput(e) {
|
70 | const formData = Object.assign({}, this.state.formData);
|
71 | formData[e.target.name] = e.target.value;
|
72 | this.setState({ formData });
|
73 | }
|
74 |
|
75 | render() {
|
76 | return (
|
77 | <form onSubmit={this.submitForm}>
|
78 | <Input type="text" name="first" value={this.state.formData.first} onChange={this.updateFormInput} />
|
79 | </form>
|
80 | );
|
81 | }
|
82 | }
|
83 |
|
84 | ### Uncontrolled Input
|
85 |
|
86 | class Form extends React.Component {
|
87 | constructor(props) {
|
88 | super(props);
|
89 | }
|
90 |
|
91 | render() {
|
92 | return (
|
93 | <form>
|
94 | <Input type="text" name="first" defaultValue={'Joe'} />
|
95 | </form>
|
96 | );
|
97 | }
|
98 | }
|