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

Commit 152463a

Browse files
committed
Added pseudo _onSubmit event to Control component. Fixes #78
1 parent 0284d1c commit 152463a

File tree

3 files changed

+21
-6
lines changed

3 files changed

+21
-6
lines changed

src/components/control-component.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
import { Component, cloneElement, PropTypes } from 'react';
22

33
class Control extends Component {
4+
constructor(props) {
5+
super(props);
6+
7+
this.handleKeyPress = this.handleKeyPress.bind(this);
8+
}
9+
410
componentWillMount() {
511
const { _onLoad, modelValue } = this.props;
612

@@ -9,20 +15,31 @@ class Control extends Component {
915
}
1016
}
1117

18+
handleKeyPress(event) {
19+
const { _onSubmit } = this.props;
20+
21+
if (_onSubmit && event.key === 'Enter') {
22+
_onSubmit(event);
23+
}
24+
}
25+
1226
render() {
1327
const { control } = this.props;
1428

1529
return cloneElement(
16-
control, {
30+
control,
31+
{
1732
...control.props,
1833
...this.props,
34+
onKeyPress: this.handleKeyPress,
1935
});
2036
}
2137
}
2238

2339
Control.propTypes = {
2440
control: PropTypes.object,
2541
_onLoad: PropTypes.func,
42+
_onSubmit: PropTypes.func,
2643
modelValue: PropTypes.any,
2744
};
2845

src/components/field-component.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,6 @@ const controlPropsMap = {
6363
...props,
6464
defaultValue: props.modelValue,
6565
name: props.model,
66-
onKeyPress: (e) => {
67-
if (e.key === 'Enter') {
68-
props.onChange(e);
69-
}
70-
},
7166
}),
7267
textarea: (props) => controlPropsMap.text(props),
7368
};
@@ -143,6 +138,7 @@ function sequenceEventActions(control, props) {
143138
onBlur: [() => dispatch(blur(model))],
144139
onChange: [],
145140
_onLoad: [], // pseudo-event
141+
_onSubmit: [], // pseudo-event
146142
};
147143

148144
const controlChangeMethod = changeMethod(model, changeAction);
@@ -159,6 +155,7 @@ function sequenceEventActions(control, props) {
159155
}
160156

161157
eventActions[updateOnEventHandler].push(updaterFn(dispatchChange));
158+
eventActions._onSubmit.push(updaterFn(dispatchChange));
162159

163160
if (control.props.defaultValue) {
164161
eventActions._onLoad.push(() => dispatch(

test/field-component-spec.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1088,6 +1088,7 @@ describe('<Field /> component', () => {
10881088
<Provider store={store}>
10891089
<Field
10901090
model="test.foo"
1091+
updateOn="blur"
10911092
>
10921093
<input type="text" />
10931094
</Field>

0 commit comments

Comments
 (0)