UNPKG

dragon-mobile-ui

Version:
237 lines (203 loc) 8.46 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); var _react = require('react'); var _react2 = _interopRequireDefault(_react); var _classnames = require('classnames'); var _classnames2 = _interopRequireDefault(_classnames); var _zscroller = require('zscroller'); var _zscroller2 = _interopRequireDefault(_zscroller); var _isChildrenEqual = require('./isChildrenEqual'); var _isChildrenEqual2 = _interopRequireDefault(_isChildrenEqual); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } function getChildMember(child, m) { return child[m]; } function toChildrenArray(children) { return children; } var Picker = function (_Component) { _inherits(Picker, _Component); function Picker(props) { _classCallCheck(this, Picker); var _this = _possibleConstructorReturn(this, (Picker.__proto__ || Object.getPrototypeOf(Picker)).call(this, props)); var selectedValueState = void 0; var _this$props = _this.props, selectedValue = _this$props.selectedValue, defaultSelectedValue = _this$props.defaultSelectedValue, children = _this$props.children; if (selectedValue !== undefined) { selectedValueState = selectedValue; } else if (defaultSelectedValue !== undefined) { selectedValueState = defaultSelectedValue; } else if (children && children.length) { selectedValueState = children[0].value; } _this.state = { selectedValue: selectedValueState }; return _this; } _createClass(Picker, [{ key: 'componentDidMount', value: function componentDidMount() { this.itemHeight = this.refs.indicator.offsetHeight; // compact this.refs.content.style.padding = this.itemHeight * 3 + 'px 0'; this.zscroller = new _zscroller2.default(this.refs.content, { scrollingX: false, snapping: true, penetrationDeceleration: 0.1, minVelocityToKeepDecelerating: 0.5, scrollingComplete: this.scrollingComplete.bind(this) }); this.zscroller.setDisabled(this.props.disabled); this.zscroller.scroller.setSnapSize(0, this.itemHeight); this.select(this.state.selectedValue); } }, { key: 'componentWillReceiveProps', value: function componentWillReceiveProps(nextProps) { if ('selectedValue' in nextProps) { this.setState({ selectedValue: nextProps.selectedValue }); } this.zscroller.setDisabled(nextProps.disabled); } }, { key: 'shouldComponentUpdate', value: function shouldComponentUpdate(nextProps, nextState) { return this.state.selectedValue !== nextState.selectedValue || !(0, _isChildrenEqual2.default)(this.props.children, nextProps.children, this.props.pure); } }, { key: 'componentDidUpdate', value: function componentDidUpdate() { this.zscroller.reflow(); this.select(this.state.selectedValue); } }, { key: 'componentWillUnmount', value: function componentWillUnmount() { this.zscroller.destroy(); } }, { key: 'getValue', value: function getValue() { return this.props.selectedValue || this.props.children && this.props.children[0] && this.props.children[0].value; } }, { key: 'scrollTo', value: function scrollTo(top) { this.zscroller.scroller.scrollTo(0, top); } }, { key: 'fireValueChange', value: function fireValueChange(selectedValue) { if (selectedValue !== this.state.selectedValue) { if (!('selectedValue' in this.props)) { this.setState({ selectedValue: selectedValue }); } this.props.onValueChange(selectedValue); } } }, { key: 'scrollingComplete', value: function scrollingComplete() { var _zscroller$scroller$g = this.zscroller.scroller.getValues(), top = _zscroller$scroller$g.top; if (top >= 0) { this.doScrollingComplete(top); } } }, { key: 'select', value: function select(value) { var children = toChildrenArray(this.props.children); for (var i = 0, len = children.length; i < len; i += 1) { if (getChildMember(children[i], 'value') === value) { this.selectByIndex(i); return; } } this.selectByIndex(0); } }, { key: 'selectByIndex', value: function selectByIndex(index) { if (index < 0 || index >= toChildrenArray(this.props.children).length || !this.itemHeight) { return; } this.scrollTo(index * this.itemHeight); } }, { key: 'doScrollingComplete', value: function doScrollingComplete(top) { var index = top / this.itemHeight; var floor = Math.floor(index); if (index - floor > 0.5) { index = floor + 1; } else { index = floor; } var children = toChildrenArray(this.props.children); index = Math.min(index, children.length - 1); var child = children[index]; if (child) { this.fireValueChange(getChildMember(child, 'value')); } else if (console.warn) { console.warn('child not found', children, index); } } }, { key: 'render', value: function render() { var _pickerCls; var _props = this.props, children = _props.children, prefixCls = _props.prefixCls, className = _props.className, itemStyle = _props.itemStyle, indicatorStyle = _props.indicatorStyle; var selectedValue = this.state.selectedValue; var itemClassName = prefixCls + '-item'; var selectedItemClassName = itemClassName + ' ' + prefixCls + '-item-selected'; var items = children.map(function (item) { return _react2.default.createElement( 'div', { style: itemStyle, className: selectedValue === item.value ? selectedItemClassName : itemClassName, key: item.value }, item.label ); }); var pickerCls = (_pickerCls = {}, _defineProperty(_pickerCls, className, !!className), _defineProperty(_pickerCls, prefixCls, true), _pickerCls); return _react2.default.createElement( 'div', { className: (0, _classnames2.default)(pickerCls) }, _react2.default.createElement('div', { className: prefixCls + '-indicator', ref: 'indicator', style: indicatorStyle }), _react2.default.createElement( 'div', { className: prefixCls + '-content', ref: 'content' }, items ) ); } }]); return Picker; }(_react.Component); Picker.defaultProps = { prefixCls: 'ui-datepicker', pure: true, onValueChange: function onValueChange() {} }; exports.default = Picker;