admin-on-rest-fr05t1k
Version:
A frontend Framework for building admin applications on top of REST services, using ES6, React and Material UI
85 lines (76 loc) • 2.38 kB
JavaScript
import React, { Component, PropTypes } from 'react';
import TextField from 'material-ui/TextField';
import FieldTitle from '../../util/FieldTitle';
/**
* An Input component for a number
*
* @example
* <NumberInput source="nb_views" />
*
* You can customize the `step` props (which defaults to "any")
* @example
* <NumberInput source="nb_views" step={1} />
*
* The object passed as `options` props is passed to the material-ui <TextField> component
*/
class NumberInput extends Component {
handleBlur = (eventOrValue) => {
this.props.onBlur(eventOrValue);
this.props.input.onBlur(eventOrValue);
/**
* Necessary because of a React bug on <input type="number">
* @see https://github.com/facebook/react/issues/1425
*/
this.handleChange(parseFloat(this.props.input.value));
}
handleFocus = (event) => {
this.props.onFocus(event);
this.props.input.onFocus(event);
}
handleChange = (eventOrValue) => {
this.props.onChange(eventOrValue);
this.props.input.onChange(eventOrValue);
}
render() {
const { elStyle, input, label, meta: { touched, error }, options, source, step, resource } = this.props;
return (
<TextField
{...input}
onBlur={this.handleBlur}
onFocus={this.handleFocus}
onChange={this.handleChange}
type="number"
step={step}
floatingLabelText={<FieldTitle label={label} source={source} resource={resource} />}
errorText={touched && error}
style={elStyle}
{...options}
/>
);
}
}
NumberInput.propTypes = {
addField: PropTypes.bool.isRequired,
elStyle: PropTypes.object,
input: PropTypes.object,
label: PropTypes.string,
meta: PropTypes.object,
name: PropTypes.string,
onBlur: PropTypes.func,
onChange: PropTypes.func,
onFocus: PropTypes.func,
options: PropTypes.object,
resource: PropTypes.string,
source: PropTypes.string,
step: PropTypes.string.isRequired,
validation: PropTypes.object,
};
NumberInput.defaultProps = {
addField: true,
onBlur: () => {},
onChange: () => {},
onFocus: () => {},
options: {},
step: 'any',
};
export default NumberInput;