@@ -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