Skip to content
This repository was archived by the owner on Aug 23, 2022. It is now read-only.

Commit 0308e63

Browse files
committed
Adding docs on disabled attribute
1 parent 852d51b commit 0308e63

File tree

3 files changed

+41
-35
lines changed

3 files changed

+41
-35
lines changed

docs/api/Control.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ The following pre-defined `<Control>`s are available:
2929
- `<Control.checkbox>` for `<input type="checkbox" />`
3030
- `<Control.file>` for `<input type="file" />`
3131
- `<Control.select>` for `<select></select>`
32+
- `<Control.button>` for `<button></button>`
3233

3334
For making custom controls that work with React Redux Form, see the [custom controls documentation](../guides/custom-controls.md).
3435

@@ -304,3 +305,26 @@ For instance, if you don't care whether a `<Control>` is focused or blurred:
304305
ignore={['focus', 'blur']}
305306
/>
306307
```
308+
309+
### `disabled={...}`
310+
_(Any)_: The `disabled` prop works just like you'd expect for controls that support the HTML5 `disabled` attribute.
311+
312+
However, in `<Control>`, it can be a boolean, or a function, string, or object as a [Lodash iteratee](https://lodash.com/docs#iteratee).
313+
314+
```jsx
315+
// Disable the submit button when the form is invalid
316+
<Control.button
317+
model="user"
318+
disabled={{ valid: false }}
319+
>
320+
Submit!
321+
</Control.button>
322+
```
323+
324+
For example:
325+
- `disabled={true}` or `disabled={false}` will disable or enable the control respectively, as will any other primitive value, such as `undefined`, `null`, or a number
326+
- `disabled="touched"` will disable if the field is `touched` (works with any property on the field)
327+
- `disabled={{ valid: false, touched: true }}` will disable if the field is both `touched` and not `valid`
328+
- `disabled={(fieldValue) => !fieldValue.valid}` will call the function provided with the `fieldValue` to determine its `disabled` state.
329+
330+
(since: version 1.2.6)

examples/quick-start/components/user-form.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,6 @@ class UserForm extends React.Component {
3535
dispatch(actions.submit('user', somePromise));
3636
}
3737
render() {
38-
const { forms: { user }, dispatch } = this.props;
39-
4038
return (
4139
<Form model="user" onSubmit={this.handleSubmit.bind(this)}>
4240
<div>
@@ -71,9 +69,9 @@ class UserForm extends React.Component {
7169
</label>
7270
</Field>
7371

74-
<Control.submit model="user" disabled={{ valid: false }}>
72+
<Control.button model="user" disabled={{ valid: false }}>
7573
Finish registration!
76-
</Control.submit>
74+
</Control.button>
7775
<input type="reset" value="Reset" title="reset"/>
7876
</Form>
7977
);

src/constants/control-props-map.js

Lines changed: 15 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -24,70 +24,54 @@ function createControlPropsMap() {
2424
return !!props.modelValue;
2525
}
2626

27-
const textPropsMap = {
28-
value: (props) => ((!props.defaultValue && !props.hasOwnProperty('value'))
29-
? getTextValue(props.viewValue)
30-
: props.value),
27+
const standardPropsMap = {
3128
name: (props) => props.name || props.model,
29+
disabled: ({ fieldValue, disabled }) => iterateeValue(fieldValue, disabled),
3230
onChange: ({ onChange }) => onChange,
3331
onBlur: ({ onBlur }) => onBlur,
3432
onFocus: ({ onFocus }) => onFocus,
3533
onKeyPress: ({ onKeyPress }) => onKeyPress,
3634
};
3735

36+
const textPropsMap = {
37+
...standardPropsMap,
38+
value: (props) => ((!props.defaultValue && !props.hasOwnProperty('value'))
39+
? getTextValue(props.viewValue)
40+
: props.value),
41+
};
42+
3843
return {
3944
default: textPropsMap,
4045
checkbox: {
41-
name: (props) => props.name || props.model,
46+
...standardPropsMap,
4247
checked: (props) => (props.defaultChecked
4348
? props.checked
4449
: isChecked(props)),
45-
onChange: ({ onChange }) => onChange,
46-
onBlur: ({ onBlur }) => onBlur,
47-
onFocus: ({ onFocus }) => onFocus,
48-
onKeyPress: ({ onKeyPress }) => onKeyPress,
4950
},
5051
radio: {
51-
name: (props) => props.name || props.model,
52+
...standardPropsMap,
5253
checked: (props) => (props.defaultChecked
5354
? props.checked
5455
: props.modelValue === props.value),
5556
value: (props) => props.value,
56-
onChange: ({ onChange }) => onChange,
57-
onBlur: ({ onBlur }) => onBlur,
58-
onFocus: ({ onFocus }) => onFocus,
59-
onKeyPress: ({ onKeyPress }) => onKeyPress,
6057
},
6158
select: {
62-
name: (props) => (props.name || props.model),
59+
...standardPropsMap,
6360
value: (props) => (props.modelValue),
64-
onChange: ({ onChange }) => onChange,
65-
onBlur: ({ onBlur }) => onBlur,
66-
onFocus: ({ onFocus }) => onFocus,
67-
onKeyPress: ({ onKeyPress }) => onKeyPress,
6861
},
6962
text: {
7063
...textPropsMap,
7164
type: 'text',
7265
},
7366
textarea: textPropsMap,
74-
file: {
75-
name: (props) => props.name || props.model,
76-
onChange: ({ onChange }) => onChange,
77-
onBlur: ({ onBlur }) => onBlur,
78-
onFocus: ({ onFocus }) => onFocus,
79-
onKeyPress: ({ onKeyPress }) => onKeyPress,
80-
},
81-
button: {
82-
disabled: ({ fieldValue, disabled }) => iterateeValue(fieldValue, disabled),
83-
},
67+
file: standardPropsMap,
68+
button: standardPropsMap,
8469
reset: {
70+
...standardPropsMap,
8571
onClick: (props) => (event) => {
8672
event.preventDefault();
8773
props.dispatch(actions.reset(props.model));
8874
},
89-
onFocus: ({ onFocus }) => onFocus,
90-
onBlur: ({ onBlur }) => onBlur,
9175
},
9276
};
9377
}

0 commit comments

Comments
 (0)