UNPKG

ssc-grid

Version:

React grid component for SSC 3.0

139 lines (116 loc) 3.73 kB
import _classCallCheck from 'babel-runtime/helpers/classCallCheck'; import _possibleConstructorReturn from 'babel-runtime/helpers/possibleConstructorReturn'; import _inherits from 'babel-runtime/helpers/inherits'; import React, { Component } from 'react'; import PropTypes from 'prop-types'; import FormControl from 'react-bootstrap/lib/FormControl'; /** * 控件(control/widget)分类 * Command input: Button, Drop-down list, ... * Data input-output: Checkbox Color picker Combo box Cycle button Date Picker Grid view List box List builder Radio button Scrollbar Search box Slider Spinner Text box * 来源:https://en.wikipedia.org/wiki/Widget_(GUI) * 第二来源:https://docs.joomla.org/Standard_form_field_types * Joomla的名称和Web更贴切,wikipedia不区分Web还是Client */ var propTypes = { /** * 输入框类型 */ type: PropTypes.string, /** * 是否禁用输入框 */ disabled: PropTypes.bool, /** * 当光标离开输入框 */ onBlur: PropTypes.func, /** * 当文本框内容被修改时候调用 */ onChange: PropTypes.func, /** * 当文本框被聚焦 */ onFocus: PropTypes.func, onKeyDown: PropTypes.func, /** * 文本框占位字符 */ placeholder: PropTypes.string, /** * 文本框中显示的值 * TODO `value`应该改为`defaultValue`,类似于默认的`input`组件区分`value`和 * `defaultValue`一样,使用`defaultValue`说明该组件是uncontrolled */ value: PropTypes.oneOfType([PropTypes.string, PropTypes.number]) }; var defaultProps = { value: '' }; /** * 文本框控件 (uncontrolled) */ var TextField = function (_Component) { _inherits(TextField, _Component); function TextField(props) { _classCallCheck(this, TextField); var _this = _possibleConstructorReturn(this, _Component.call(this, props)); _this.state = { value: props.value }; _this.handleChange = _this.handleChange.bind(_this); _this.handleFocus = _this.handleFocus.bind(_this); _this.handleBlur = _this.handleBlur.bind(_this); _this.handleKeyDown = _this.handleKeyDown.bind(_this); return _this; } TextField.prototype.componentWillReceiveProps = function componentWillReceiveProps(nextProps) { // 只有当值改变的情况下才会更新状态 if (nextProps.value !== this.props.value) { this.setState({ value: nextProps.value }); } }; TextField.prototype.handleChange = function handleChange(event) { var value = event.target.value; this.setState({ value: value }); if (this.props.onChange) { this.props.onChange(event); } }; TextField.prototype.handleBlur = function handleBlur(event) { if (this.props.onBlur) { this.props.onBlur(event); } }; TextField.prototype.handleFocus = function handleFocus(event) { if (this.props.onFocus) { this.props.onFocus(event); } }; TextField.prototype.handleKeyDown = function handleKeyDown(event) { if (this.props.onKeyDown) { this.props.onKeyDown(event); } }; TextField.prototype.render = function render() { return React.createElement(FormControl, { componentClass: this.props.type || 'input', type: this.props.type || 'text', value: this.state.value, disabled: this.props.disabled === true, placeholder: this.props.placeholder, onChange: this.handleChange, onFocus: this.handleFocus, onBlur: this.handleBlur, onKeyDown: this.handleKeyDown }); }; return TextField; }(Component); TextField.displayName = 'TextField'; TextField.propTypes = propTypes; TextField.defaultProps = defaultProps; export default TextField;