Skip to content

Commit eb140c1

Browse files
Fix editing entity id, trigger the update only on blur and enter, not on each keystroke (fix #51) (#52) (#823)
1 parent 27c861c commit eb140c1

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

‎src/components/components/CommonComponents.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ export default class CommonComponents extends React.Component {
133133
ID
134134
</label>
135135
<InputWidget
136-
onChange={changeId}
136+
onBlur={changeId}
137137
entity={entity}
138138
name="id"
139139
value={entity.id}

‎src/components/widgets/InputWidget.js‎

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,42 @@ export default class InputWidget extends React.Component {
55
static propTypes = {
66
id: PropTypes.string,
77
name: PropTypes.string.isRequired,
8+
onBlur: PropTypes.func,
89
onChange: PropTypes.func,
910
value: PropTypes.any
1011
};
1112

1213
constructor(props) {
1314
super(props);
1415
this.state = { value: this.props.value || '' };
16+
this.input = React.createRef();
1517
}
1618

17-
onChange = (e) => {
18-
var value = e.target.value;
19+
onChange = (event) => {
20+
const value = event.target.value;
1921
this.setState({ value: value });
2022
if (this.props.onChange) {
2123
this.props.onChange(this.props.name, value);
2224
}
2325
};
2426

27+
onBlur = (event) => {
28+
if (this.props.onBlur) {
29+
const value = event.target.value;
30+
this.props.onBlur(this.props.name, value);
31+
}
32+
};
33+
34+
onKeyDown = (event) => {
35+
event.stopPropagation();
36+
37+
// enter
38+
if (event.keyCode === 13) {
39+
this.input.current.blur();
40+
return;
41+
}
42+
};
43+
2544
componentDidUpdate(prevProps) {
2645
if (this.props.value !== prevProps.value) {
2746
this.setState({ value: this.props.value || '' });
@@ -32,10 +51,13 @@ export default class InputWidget extends React.Component {
3251
return (
3352
<input
3453
id={this.props.id}
54+
ref={this.input}
3555
type="text"
3656
className="string"
3757
value={this.state.value}
58+
onBlur={this.onBlur}
3859
onChange={this.onChange}
60+
onKeyDown={this.onKeyDown}
3961
/>
4062
);
4163
}

0 commit comments

Comments
 (0)