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

Commit 852d51b

Browse files
committed
Adding button component
1 parent af74187 commit 852d51b

File tree

6 files changed

+69
-2
lines changed

6 files changed

+69
-2
lines changed

SUMMARY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,6 @@
2525
* [action creators](docs/api/actions.md)
2626
* [Recipes and Examples](docs/recipes/README.md)
2727
* [Quick Start](docs/recipes/quickstart.md)
28+
* [Redux Form Examples](docs/recipes/redux-form.md)
2829
* [Simple Form](docs/recipes/simple.md)
2930
* [Sync Validation](docs/recipes/sync-validation.md)

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,9 @@ class UserForm extends React.Component {
7171
</label>
7272
</Field>
7373

74-
<button type="submit" disabled={!user.$form.valid}>
74+
<Control.submit model="user" disabled={{ valid: false }}>
7575
Finish registration!
76-
</button>
76+
</Control.submit>
7777
<input type="reset" value="Reset" title="reset"/>
7878
</Form>
7979
);

src/components/control-component.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -698,6 +698,17 @@ function createControlClass(customControlPropsMap = {}, s = defaultStrategy) {
698698
/>
699699
);
700700

701+
ConnectedControl.button = (props) => (
702+
<ConnectedControl
703+
component="button"
704+
mapProps={{
705+
...controlPropsMap.button,
706+
...props.mapProps,
707+
}}
708+
{...omit(props, 'mapProps')}
709+
/>
710+
);
711+
701712
ConnectedControl.reset = (props) => (
702713
<ConnectedControl
703714
component="button"

src/constants/control-props-map.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import isMulti from '../utils/is-multi';
2+
import { iterateeValue } from '../utils/iteratee';
23
import actions from '../actions';
34

45
function createControlPropsMap() {
@@ -77,6 +78,9 @@ function createControlPropsMap() {
7778
onFocus: ({ onFocus }) => onFocus,
7879
onKeyPress: ({ onKeyPress }) => onKeyPress,
7980
},
81+
button: {
82+
disabled: ({ fieldValue, disabled }) => iterateeValue(fieldValue, disabled),
83+
},
8084
reset: {
8185
onClick: (props) => (event) => {
8286
event.preventDefault();

src/utils/iteratee.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,17 @@ export default function iteratee(value) {
2828

2929
return propChecker(value);
3030
}
31+
32+
export function iterateeValue(data, value) {
33+
if (typeof value === 'function') {
34+
return value(data);
35+
}
36+
37+
if (!Array.isArray(value)
38+
&& typeof value !== 'object'
39+
&& typeof value !== 'string') {
40+
return !!value;
41+
}
42+
43+
return iteratee(value)(data);
44+
}

test/control-component-spec.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1633,6 +1633,43 @@ Object.keys(testContexts).forEach((testKey) => {
16331633
});
16341634
});
16351635

1636+
describe('with <Control.button>', () => {
1637+
it('should exist', () => {
1638+
assert.isFunction(Control.button);
1639+
});
1640+
1641+
const disabledProps = [
1642+
true,
1643+
{ valid: false },
1644+
(fieldValue) => !fieldValue.valid,
1645+
];
1646+
1647+
disabledProps.forEach((disabled) => {
1648+
it(`should be disabled with ${typeof disabled} as disabled prop value`, () => {
1649+
const initialState = getInitialState({ foo: '' });
1650+
const store = testCreateStore({
1651+
testForm: formReducer('test', initialState),
1652+
});
1653+
1654+
const field = testRender(
1655+
<Control.button model="test" disabled={disabled} />,
1656+
store);
1657+
1658+
const button = TestUtils.findRenderedDOMComponentWithTag(field, 'button');
1659+
1660+
store.dispatch(actions.setValidity('test', false));
1661+
1662+
assert.isTrue(button.disabled);
1663+
1664+
if (disabled !== true) {
1665+
store.dispatch(actions.setValidity('test', true));
1666+
1667+
assert.isFalse(button.disabled);
1668+
}
1669+
});
1670+
});
1671+
});
1672+
16361673
describe('manual focus/blur', () => {
16371674
const initialState = getInitialState({ foo: 'bar' });
16381675
const store = testCreateStore({

0 commit comments

Comments
 (0)