5dfc0f0f24
Adding support for `min`, `max`, `step` on the input element and adding `valueType` for specifying the return type, so the `NumberControl` can return actual numbers.
42 lines
1.0 KiB
JavaScript
42 lines
1.0 KiB
JavaScript
import React, { PropTypes } from 'react';
|
|
|
|
export default class NumberControl extends React.Component {
|
|
handleChange = (e) => {
|
|
const valueType = this.props.field.get('valueType');
|
|
const { onChange } = this.props;
|
|
if(valueType === 'int') {
|
|
onChange(parseInt(e.target.value, 10));
|
|
} else if(valueType === 'float') {
|
|
onChange(parseFloat(e.target.value));
|
|
} else {
|
|
onChange(e.target.value);
|
|
}
|
|
};
|
|
|
|
render() {
|
|
const { field, value, forID } = this.props;
|
|
const min = field.get('min', '');
|
|
const max = field.get('max', '');
|
|
const step = field.get('step', field.get('valueType') === 'int' ? 1 : '');
|
|
return <input
|
|
type="number"
|
|
id={forID}
|
|
value={value || ''}
|
|
step={step}
|
|
min={min}
|
|
max={max}
|
|
onChange={this.handleChange}
|
|
/>;
|
|
}
|
|
}
|
|
|
|
NumberControl.propTypes = {
|
|
onChange: PropTypes.func.isRequired,
|
|
value: PropTypes.node,
|
|
forID: PropTypes.string,
|
|
valueType: PropTypes.string,
|
|
step: PropTypes.number,
|
|
min: PropTypes.number,
|
|
max: PropTypes.number,
|
|
};
|