Spaces:
Running
Running
File size: 2,521 Bytes
f2bee8a |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
import PropTypes from 'prop-types';
import React from 'react';
import bindAll from 'lodash.bindall';
import SliderPromptComponent from '../components/slider-prompt/slider-prompt.jsx';
class SliderPrompt extends React.Component {
constructor (props) {
super(props);
bindAll(this, [
'handleOk',
'handleCancel',
'handleChangeMin',
'handleChangeMax',
'handleKeyPress',
'validates',
'shouldBeDiscrete'
]);
const {isDiscrete, minValue, maxValue} = this.props;
this.state = {
// For internal use, convert values to strings based on isDiscrete
// This is because `<input />` always returns values as strings.
minValue: isDiscrete ? minValue.toFixed(0) : minValue.toFixed(2),
maxValue: isDiscrete ? maxValue.toFixed(0) : maxValue.toFixed(2)
};
}
handleKeyPress (event) {
if (event.key === 'Enter') this.handleOk();
}
handleOk () {
const {minValue, maxValue} = this.state;
if (!this.validates(minValue, maxValue)) {
this.props.onCancel();
return;
}
this.props.onOk(
parseFloat(minValue),
parseFloat(maxValue),
this.shouldBeDiscrete(minValue, maxValue));
}
handleCancel () {
this.props.onCancel();
}
handleChangeMin (e) {
this.setState({minValue: e.target.value});
}
handleChangeMax (e) {
this.setState({maxValue: e.target.value});
}
shouldBeDiscrete (min, max) {
return min.indexOf('.') + max.indexOf('.') === -2; // Both -1
}
validates (min, max) {
return isFinite(min) && isFinite(max);
}
render () {
return (
<SliderPromptComponent
maxValue={this.state.maxValue}
minValue={this.state.minValue}
onCancel={this.handleCancel}
onChangeMax={this.handleChangeMax}
onChangeMin={this.handleChangeMin}
onKeyPress={this.handleKeyPress}
onOk={this.handleOk}
/>
);
}
}
SliderPrompt.propTypes = {
isDiscrete: PropTypes.bool,
maxValue: PropTypes.number,
minValue: PropTypes.number,
onCancel: PropTypes.func.isRequired,
onOk: PropTypes.func.isRequired
};
SliderPrompt.defaultProps = {
maxValue: 100,
minValue: 0,
isDiscrete: true
};
export default SliderPrompt;
|