mirror of
https://github.com/scratchfoundation/scratch-paint.git
synced 2024-12-23 05:52:42 -05:00
create liveinput hoc instead
this should solve the issue of being able to delete what you typed. Thanks!
This commit is contained in:
parent
7dbabf60a9
commit
d2952c2114
3 changed files with 67 additions and 19 deletions
60
src/components/forms/live-input-hoc.jsx
Normal file
60
src/components/forms/live-input-hoc.jsx
Normal file
|
@ -0,0 +1,60 @@
|
||||||
|
import bindAll from 'lodash.bindall';
|
||||||
|
import PropTypes from 'prop-types';
|
||||||
|
import React from 'react';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Higher Order Component to manage inputs that submit on change and <enter>
|
||||||
|
* @param {React.Component} Input text input that consumes onChange, onBlur, onKeyPress
|
||||||
|
* @returns {React.Component} Live input that calls onSubmit on change and <enter>
|
||||||
|
*/
|
||||||
|
export default function (Input) {
|
||||||
|
class LiveInput extends React.Component {
|
||||||
|
constructor (props) {
|
||||||
|
super(props);
|
||||||
|
bindAll(this, [
|
||||||
|
'handleChange',
|
||||||
|
'handleKeyPress',
|
||||||
|
'handleFlush'
|
||||||
|
]);
|
||||||
|
this.state = {
|
||||||
|
value: null
|
||||||
|
};
|
||||||
|
}
|
||||||
|
handleKeyPress (e) {
|
||||||
|
if (e.key === 'Enter') {
|
||||||
|
this.handleChange(e);
|
||||||
|
e.target.blur();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
handleFlush () {
|
||||||
|
this.setState({value: null});
|
||||||
|
}
|
||||||
|
handleChange (e) {
|
||||||
|
const isNumeric = typeof this.props.value === 'number';
|
||||||
|
const validatesNumeric = isNumeric ? !isNaN(e.target.value) : true;
|
||||||
|
if (e.target.value !== null && validatesNumeric) {
|
||||||
|
this.props.onSubmit(Number(e.target.value));
|
||||||
|
}
|
||||||
|
this.setState({value: e.target.value});
|
||||||
|
}
|
||||||
|
render () {
|
||||||
|
const liveValue = this.state.value === null ? this.props.value : this.state.value;
|
||||||
|
return (
|
||||||
|
<Input
|
||||||
|
{...this.props}
|
||||||
|
value={liveValue}
|
||||||
|
onBlur={this.handleFlush}
|
||||||
|
onChange={this.handleChange}
|
||||||
|
onKeyPress={this.handleKeyPress}
|
||||||
|
/>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
LiveInput.propTypes = {
|
||||||
|
onSubmit: PropTypes.func.isRequired,
|
||||||
|
value: PropTypes.oneOfType([PropTypes.string, PropTypes.number])
|
||||||
|
};
|
||||||
|
|
||||||
|
return LiveInput;
|
||||||
|
}
|
|
@ -6,6 +6,7 @@ import React from 'react';
|
||||||
import {changeBrushSize} from '../../reducers/brush-mode';
|
import {changeBrushSize} from '../../reducers/brush-mode';
|
||||||
import {changeBrushSize as changeEraserSize} from '../../reducers/eraser-mode';
|
import {changeBrushSize as changeEraserSize} from '../../reducers/eraser-mode';
|
||||||
|
|
||||||
|
import LiveInputHOC from '../forms/live-input-hoc.jsx';
|
||||||
import {injectIntl, intlShape} from 'react-intl';
|
import {injectIntl, intlShape} from 'react-intl';
|
||||||
import Input from '../forms/input.jsx';
|
import Input from '../forms/input.jsx';
|
||||||
// import LabeledIconButton from '../labeled-icon-button/labeled-icon-button.jsx';
|
// import LabeledIconButton from '../labeled-icon-button/labeled-icon-button.jsx';
|
||||||
|
@ -21,6 +22,7 @@ import eraserIcon from '../eraser-mode/eraser.svg';
|
||||||
|
|
||||||
import {MAX_STROKE_WIDTH} from '../../reducers/stroke-width';
|
import {MAX_STROKE_WIDTH} from '../../reducers/stroke-width';
|
||||||
|
|
||||||
|
const LiveInput = LiveInputHOC(Input);
|
||||||
const ModeToolsComponent = props => {
|
const ModeToolsComponent = props => {
|
||||||
const brushMessage = props.intl.formatMessage({
|
const brushMessage = props.intl.formatMessage({
|
||||||
defaultMessage: 'Brush',
|
defaultMessage: 'Brush',
|
||||||
|
@ -44,17 +46,12 @@ const ModeToolsComponent = props => {
|
||||||
src={brushIcon}
|
src={brushIcon}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
<Input
|
<LiveInput
|
||||||
small
|
small
|
||||||
max={MAX_STROKE_WIDTH}
|
max={MAX_STROKE_WIDTH}
|
||||||
min="1"
|
min="1"
|
||||||
type="number"
|
type="number"
|
||||||
value={props.brushValue}
|
value={props.brushValue}
|
||||||
onChange={function (e) {
|
|
||||||
if (e.target.value !== null && !isNaN(e.target.value)) {
|
|
||||||
props.onBrushSliderChange(Number(e.target.value));
|
|
||||||
}
|
|
||||||
}}
|
|
||||||
onSubmit={props.onBrushSliderChange}
|
onSubmit={props.onBrushSliderChange}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
|
@ -69,17 +66,12 @@ const ModeToolsComponent = props => {
|
||||||
src={eraserIcon}
|
src={eraserIcon}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
<Input
|
<LiveInput
|
||||||
small
|
small
|
||||||
max={MAX_STROKE_WIDTH}
|
max={MAX_STROKE_WIDTH}
|
||||||
min="1"
|
min="1"
|
||||||
type="number"
|
type="number"
|
||||||
value={props.eraserValue}
|
value={props.eraserValue}
|
||||||
onChange={function (e) {
|
|
||||||
if (e.target.value !== null && !isNaN(e.target.value)) {
|
|
||||||
props.onEraserSliderChange(Number(e.target.value));
|
|
||||||
}
|
|
||||||
}}
|
|
||||||
onSubmit={props.onEraserSliderChange}
|
onSubmit={props.onEraserSliderChange}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
|
|
|
@ -3,24 +3,20 @@ import PropTypes from 'prop-types';
|
||||||
|
|
||||||
import Input from './forms/input.jsx';
|
import Input from './forms/input.jsx';
|
||||||
import InputGroup from './input-group/input-group.jsx';
|
import InputGroup from './input-group/input-group.jsx';
|
||||||
|
import LiveInputHOC from './forms/live-input-hoc.jsx';
|
||||||
|
|
||||||
import {MAX_STROKE_WIDTH} from '../reducers/stroke-width';
|
import {MAX_STROKE_WIDTH} from '../reducers/stroke-width';
|
||||||
|
|
||||||
|
const LiveInput = LiveInputHOC(Input);
|
||||||
const StrokeWidthIndicatorComponent = props => (
|
const StrokeWidthIndicatorComponent = props => (
|
||||||
<InputGroup disabled={props.disabled}>
|
<InputGroup disabled={props.disabled}>
|
||||||
<Input
|
<LiveInput
|
||||||
small
|
small
|
||||||
disabled={props.disabled}
|
disabled={props.disabled}
|
||||||
max={MAX_STROKE_WIDTH}
|
max={MAX_STROKE_WIDTH}
|
||||||
min="0"
|
min="0"
|
||||||
type="number"
|
type="number"
|
||||||
value={props.strokeWidth ? props.strokeWidth : 0}
|
value={props.strokeWidth ? props.strokeWidth : 0}
|
||||||
onChange={function (e) {
|
|
||||||
if (e.target.value !== null && !isNaN(e.target.value)) {
|
|
||||||
props.onChangeStrokeWidth(Number(e.target.value));
|
|
||||||
}
|
|
||||||
|
|
||||||
}}
|
|
||||||
onSubmit={props.onChangeStrokeWidth}
|
onSubmit={props.onChangeStrokeWidth}
|
||||||
/>
|
/>
|
||||||
</InputGroup>
|
</InputGroup>
|
||||||
|
|
Loading…
Reference in a new issue