diff --git a/src/components/buffered-input/buffered-input.jsx b/src/components/buffered-input/buffered-input.jsx new file mode 100644 index 000000000..e12662d8d --- /dev/null +++ b/src/components/buffered-input/buffered-input.jsx @@ -0,0 +1,52 @@ +const bindAll = require('lodash.bindall'); +const PropTypes = require('prop-types'); +const React = require('react'); + +class BufferedInput extends React.Component { + constructor (props) { + super(props); + bindAll(this, [ + 'handleChange', + 'handleKeyPress', + 'handleFlush' + ]); + this.state = { + value: null + }; + } + handleKeyPress (e) { + if (e.key === 'Enter') { + this.handleFlush(); + e.target.blur(); + } + } + handleFlush () { + const isNumeric = typeof this.props.value === 'number'; + const validatesNumeric = isNumeric ? !isNaN(this.state.value) : true; + if (this.state.value !== null && validatesNumeric) { + this.props.onSubmit(isNumeric ? Number(this.state.value) : this.state.value); + } + this.setState({value: null}); + } + handleChange (e) { + this.setState({value: e.target.value}); + } + render () { + const bufferedValue = this.state.value === null ? this.props.value : this.state.value; + return ( + + ); + } +} + +BufferedInput.propTypes = { + onSubmit: PropTypes.func.isRequired, + value: PropTypes.oneOfType([PropTypes.string, PropTypes.number]) +}; +module.exports = BufferedInput; diff --git a/src/components/sprite-info/sprite-info.jsx b/src/components/sprite-info/sprite-info.jsx index 5aa1653e3..f9e3e73bd 100644 --- a/src/components/sprite-info/sprite-info.jsx +++ b/src/components/sprite-info/sprite-info.jsx @@ -3,6 +3,7 @@ import PropTypes from 'prop-types'; import React from 'react'; import Box from '../box/box.jsx'; +import BufferedInput from '../buffered-input/buffered-input.jsx'; import styles from './sprite-info.css'; import xIcon from './icon--x.svg'; @@ -32,16 +33,14 @@ class SpriteInfo extends React.Component {
Sprite -
@@ -53,16 +52,14 @@ class SpriteInfo extends React.Component { />
x - @@ -74,16 +71,14 @@ class SpriteInfo extends React.Component { /> y - @@ -134,15 +129,13 @@ class SpriteInfo extends React.Component {
Direction -
@@ -178,10 +171,6 @@ SpriteInfo.propTypes = { ]), disabled: PropTypes.bool, name: PropTypes.string, - onBlurDirection: PropTypes.func, - onBlurName: PropTypes.func, - onBlurX: PropTypes.func, - onBlurY: PropTypes.func, onChangeDirection: PropTypes.func, onChangeName: PropTypes.func, onChangeRotationStyle: PropTypes.func, @@ -189,7 +178,6 @@ SpriteInfo.propTypes = { onChangeY: PropTypes.func, onClickNotVisible: PropTypes.func, onClickVisible: PropTypes.func, - onKeyPress: PropTypes.func, rotationStyle: PropTypes.oneOf(ROTATION_STYLES), visible: PropTypes.bool, x: PropTypes.oneOfType([ diff --git a/src/containers/sprite-info.jsx b/src/containers/sprite-info.jsx index ba84fed0a..1b63c2e00 100644 --- a/src/containers/sprite-info.jsx +++ b/src/containers/sprite-info.jsx @@ -8,64 +8,14 @@ class SpriteInfo extends React.Component { constructor (props) { super(props); bindAll(this, [ - 'handleChangeName', 'handleChangeRotationStyle', - 'handleChangeDirection', - 'handleChangeX', - 'handleChangeY', 'handleClickVisible', - 'handleClickNotVisible', - 'handleFlush', - 'handleKeyPress' + 'handleClickNotVisible' ]); - this.state = { - direction: null, - name: null, - x: null, - y: null - }; - } - handleKeyPress (e) { - if (e.key === 'Enter') { - this.handleFlush(); - e.target.blur(); - } - } - handleFlush () { - if (this.state.direction !== null && !isNaN(this.state.direction)) { - this.props.onChangeDirection(this.state.direction); - } - if (this.state.name !== null) { - this.props.onChangeName(this.state.name); - } - if (this.state.x !== null && !isNaN(this.state.x)) { - this.props.onChangeX(this.state.x); - } - if (this.state.y !== null && !isNaN(this.state.y)) { - this.props.onChangeY(this.state.y); - } - this.setState({ - direction: null, - name: null, - x: null, - y: null - }); - } - handleChangeDirection (e) { - this.setState({direction: e.target.value}); - } - handleChangeName (e) { - this.setState({name: e.target.value}); } handleChangeRotationStyle (e) { this.props.onChangeRotationStyle(e.target.value); } - handleChangeX (e) { - this.setState({x: e.target.value}); - } - handleChangeY (e) { - this.setState({y: e.target.value}); - } handleClickVisible (e) { e.preventDefault(); this.props.onChangeVisibility(true); @@ -75,28 +25,12 @@ class SpriteInfo extends React.Component { this.props.onChangeVisibility(false); } render () { - const bufferedInputs = { - direction: this.state.direction === null ? this.props.direction : this.state.direction, - name: this.state.name === null ? this.props.name : this.state.name, - x: this.state.x === null ? this.props.x : this.state.x, - y: this.state.y === null ? this.props.y : this.state.y - }; return ( ); }