|
1 | 1 | API
|
2 | 2 | ---
|
3 | 3 |
|
4 |
| -* [Base](#base) |
5 |
| -* [Text](#text) |
6 |
| -* [Checkbox / Switch](#checkbox-/-switch) |
7 |
| -* [Radio](#radio) |
8 |
| -* [Date](#date) |
9 |
| -* Time (comming soon) |
10 |
| -* [Select](#select) |
11 |
| -* [Switch](#switch) |
12 |
| -* [Color](#color) |
13 |
| -* [Autocomplete](#autocomplete) |
14 |
| -* [Html](#html) |
15 |
| -* [Hidden](#hidden) |
16 |
| -* [Custom Message](#custom-message) |
17 |
| - |
18 | 4 | ### Base (Common props)
|
19 | 5 |
|
20 |
| -| Props | Required | Type | Description | |
21 |
| -|-------------------|----------|------------------------|----------------------------------------------------| |
22 |
| -| label | false | string | | |
23 |
| -| helperText | false | string | | |
24 |
| -| disabled | false | boolean | | |
25 |
| -| validation | false | string | rules of validation | |
26 |
| -| validationContext | false | object { prop: value } | extra fields for validation bind (ex. required_if) | |
27 |
| -| errorMessage | false | string | custom error message from external validation | |
28 |
| - |
29 |
| -#### Text |
30 |
| - |
31 |
| -All [material-ui](https://material-ui.com/api/text-field/) props and: |
32 |
| - |
33 |
| -| Props | Required | Type | Description | |
34 |
| -|----------|----------|-------------------------|--------------------------------------| |
35 |
| -| value | true | any | | |
36 |
| -| mask | false | string | | |
37 |
| -| onChange | true | Function(string/number) | | |
38 |
| -| loading | false | boolean | if true will add a progress at right | |
39 |
| - |
40 |
| -#### Checkbox / Switch |
41 |
| - |
42 |
| -All material-ui |
43 |
| -[checkbox](https://material-ui.com/api/checkbox/) and |
44 |
| -[switch](https://material-ui.com/api/switch/) |
45 |
| - props and: |
46 |
| - |
47 |
| -| Props | Required | Type | Description | |
48 |
| -|------------|----------|------------------------------------------|-------------------------------| |
49 |
| -| value | false | any | if null will return a boolean | |
50 |
| -| checked | true | boolean | | |
51 |
| -| helperText | false | string | | |
52 |
| -| onChange | true | Function(value (if provided) or boolean) | | |
53 |
| - |
54 |
| -### Radio |
55 |
| - |
56 |
| -All material-ui [radio](https://material-ui.com/api/radio/) props and: |
57 |
| - |
58 |
| -| Props | Required | Type | Description | |
59 |
| -|------------|----------|-----------------|------------------------------------| |
60 |
| -| value | true | any | Value that will return if selected | |
61 |
| -| checked | true | boolean | | |
62 |
| -| helperText | false | string | | |
63 |
| -| onChange | true | Function(value) | | |
64 |
| - |
65 |
| - |
66 |
| -### Date |
67 |
| - |
68 |
| -All [material-ui-pickers](https://github.com/dmtrKovalenko/material-ui-pickers) props and: |
69 |
| - |
70 |
| -| Props | Required | Type | Description | |
71 |
| -|---------------|----------|----------------|----------------------------------| |
72 |
| -| value | true | Date | | |
73 |
| -| minDate | false | Date | | |
74 |
| -| maxDate | false | Date | | |
75 |
| -| disablePast | false | boolean | | |
76 |
| -| disableFuture | false | boolean | | |
77 |
| -| format | false | string | moment string format | |
78 |
| -| locale | false | string | use defaultDateLocale as default | |
79 |
| -| onChange | true | Function(date) | | |
80 |
| - |
81 |
| -#### Select |
82 |
| - |
83 |
| -All [material-ui](https://material-ui.com/api/select/) props and: |
84 |
| - |
85 |
| -| Props | Required | Type | Description | |
86 |
| -|----------|----------|------------------------------------------------|--------------------------------------| |
87 |
| -| value | true | any | | |
88 |
| -| options | true | object { value: string/number, label: string } | | |
89 |
| -| onChange | true | Function(string/number) | | |
90 |
| -| loading | false | boolean | if true will add a progress at right | |
91 |
| - |
92 |
| -#### Color |
93 |
| - |
94 |
| -| Props | Required | Type | Description | |
95 |
| -|----------|----------|------------------|-------------| |
96 |
| -| value | true | string | | |
97 |
| -| onChange | true | Function(string) | | |
98 |
| - |
99 |
| - |
100 |
| -#### Autocomplete |
101 |
| - |
102 |
| -| Props | Required | Type | Description | |
103 |
| -|----------|----------|------------------------------------------------|-------------| |
104 |
| -| value | true | any | | |
105 |
| -| options | true | object { value: string/number, label: string } | | |
106 |
| -| onChange | true | Function(string/number) | | |
107 |
| - |
108 |
| -#### Html |
109 |
| - |
110 |
| -| Props | Required | Type | Description | |
111 |
| -|----------|----------|------------------|-------------| |
112 |
| -| value | true | string | | |
113 |
| -| onChange | true | Function(string) | | |
114 |
| - |
115 |
| -#### Hidden |
116 |
| - |
117 |
| -| Props | Required | Type | Description | |
118 |
| -|-------|----------|------|-------------| |
119 |
| -| value | true | any | | |
120 |
| - |
121 |
| -#### Custom Message |
122 |
| - |
123 |
| -| Props | Required | Type | Description | |
124 |
| -|----------|----------|--------|--------------------| |
125 |
| -| rules | true | string | separated by comma | |
126 |
| -| children | true | string | message | |
| 6 | +Component Base to create a form field. |
| 7 | + |
| 8 | +| Props | Required | Type | Description | |
| 9 | +|-------------------|----------|------------------------|------------------------------------------------------------------------| |
| 10 | +| name | false | string | | |
| 11 | +| value | false | any | | |
| 12 | +| submitted | false | boolean | flag to set if form was submited (also can be set by setFormSubmitted) | |
| 13 | +| validation | false | string | rules of validation | |
| 14 | +| validationContext | false | object { prop: value } | extra fields for validation bind (ex. required_if) | |
| 15 | +| errorMessage | false | string | custom error message from external validation | |
| 16 | + |
| 17 | +### How to Create a Form Field |
| 18 | + |
| 19 | +```jsx |
| 20 | +import { FieldCoreBase } from '@react-form-fields/core'; |
| 21 | + |
| 22 | +class MyComponentField extends FieldCoreBase { |
| 23 | + onChange = event => { |
| 24 | + const value = this.mask.clean(event.target ? event.target.value : event); |
| 25 | + |
| 26 | + this.setState({ touched: true }); //<-- important to show the error |
| 27 | + this.props.onChange(value); |
| 28 | + } |
| 29 | + |
| 30 | + render() { |
| 31 | + const { value, label, name } = this.props; |
| 32 | + |
| 33 | + return ( |
| 34 | + <Fragment> |
| 35 | + {super.render() /*<-- important*/} |
| 36 | + |
| 37 | + {/* isRequired: check if validation prop contains the required rule */} |
| 38 | + <label>{label} {this.isRequired ? '*' : ''}</label> |
| 39 | + <input |
| 40 | + name={name} |
| 41 | + value={this.mask.apply(value)} |
| 42 | + onChange={this.onChange} |
| 43 | + /> |
| 44 | + |
| 45 | + {/* errorMessage will null if submitted and touched are false */} |
| 46 | + {this.errorMessage ? <p class="error">{this.errorMessage}</p> : null} |
| 47 | + </Fragment> |
| 48 | + ); |
| 49 | + } |
| 50 | +} |
| 51 | +``` |
0 commit comments