tuya-panel-kit
Version:
a functional component library for developing tuya device panels!
201 lines (144 loc) • 7.37 kB
JavaScript
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.PickerView = undefined;
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
var _jsxFileName = 'src/components/picker-view/TYRCTPicker.js';
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 _propTypes = require('prop-types');
var _propTypes2 = _interopRequireDefault(_propTypes);
var _react = require('react');
var _react2 = _interopRequireDefault(_react);
var _reactNative = require('react-native');
var _index = require('./picker/index.ios');
var _index2 = _interopRequireDefault(_index);
var _utils = require('../../utils');
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }
function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; }
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; }
var viewWidth = _utils.RatioUtils.viewWidth;
var MAX_ITEM_NUM = 1260;
var Item = function (_PureComponent) {
_inherits(Item, _PureComponent);
function Item() {
_classCallCheck(this, Item);
return _possibleConstructorReturn(this, (Item.__proto__ || Object.getPrototypeOf(Item)).apply(this, arguments));
}
_createClass(Item, [{
key: 'render',
value: function render() {
return null;
}
}]);
return Item;
}(_react.PureComponent);
var PickerView = exports.PickerView = function (_Component) {
_inherits(PickerView, _Component);
function PickerView(props) {
_classCallCheck(this, PickerView);
var _this2 = _possibleConstructorReturn(this, (PickerView.__proto__ || Object.getPrototypeOf(PickerView)).call(this, props));
_initialiseProps.call(_this2);
var children = props.children,
loop = props.loop;
_this2._loopTimes = children && children.length > 0 ? loop ? Math.round(MAX_ITEM_NUM / children.length) : 1 : 0;
_this2.state = _this2._stateFromProps(props);
return _this2;
}
_createClass(PickerView, [{
key: 'componentWillReceiveProps',
value: function componentWillReceiveProps(nextProps) {
this.setState(this._stateFromProps(nextProps));
}
}, {
key: 'render',
value: function render() {
var _props = this.props,
accessibilityLabel = _props.accessibilityLabel,
style = _props.style,
rest = _objectWithoutProperties(_props, ['accessibilityLabel', 'style']);
var _state = this.state,
items = _state.items,
selectedIndex = _state.selectedIndex;
return _react2.default.createElement(_index2.default, _extends({}, rest, {
style: [{ width: viewWidth, height: 216 }, style],
accessibilityLabel: accessibilityLabel,
items: items,
selectedIndex: selectedIndex,
onChange: this._onChange,
__source: {
fileName: _jsxFileName,
lineNumber: 148
}
}));
}
}]);
return PickerView;
}(_react.Component);
PickerView.propTypes = {
accessibilityLabel: _propTypes2.default.string,
itemTextColor: _reactNative.ColorPropType,
selectedItemTextColor: _reactNative.ColorPropType,
dividerColor: _reactNative.ColorPropType,
itemPadding: _propTypes2.default.number,
itemAlign: _propTypes2.default.string,
textSize: _propTypes2.default.number,
fontWeight: _propTypes2.default.string,
fontFamily: _propTypes2.default.oneOfType([_propTypes2.default.oneOfType(['DIN Alternate', 'Times New Roman', 'PingFangHK-Regular', 'PingFangHK-Medium']), _propTypes2.default.string]),
onValueChange: _propTypes2.default.func,
selectedValue: _propTypes2.default.oneOfType([_propTypes2.default.string, _propTypes2.default.number, _propTypes2.default.bool]),
children: _propTypes2.default.node.isRequired,
loop: _propTypes2.default.bool,
style: _reactNative.ViewPropTypes.style
};
PickerView.defaultProps = {
accessibilityLabel: 'PickerView',
loop: false,
itemTextColor: '#cccccc',
selectedItemTextColor: 'black',
dividerColor: '#cccccc',
itemAlign: 'center',
itemPadding: 14,
textSize: 22,
fontWeight: 'normal',
fontFamily: 'DIN Alternate',
style: null,
selectedValue: '-1',
onValueChange: function onValueChange() {}
};
var _initialiseProps = function _initialiseProps() {
var _this3 = this;
this._stateFromProps = function (props) {
var selectedIndex = 0;
var pickerItems = [];
var childArray = _react2.default.Children.toArray(props.children);
pickerItems = new Array(_this3._loopTimes).fill(1).reduce(function (acc) {
var current = childArray.map(function (child, index) {
if (child.props.value === props.selectedValue) {
selectedIndex = index + Math.floor(_this3._loopTimes / 2) * childArray.length;
}
return {
value: child.props.value,
label: child.props.label
};
});
return [].concat(_toConsumableArray(acc), _toConsumableArray(current));
}, []);
return { selectedIndex: selectedIndex, items: pickerItems };
};
this._onChange = function (event) {
var _props2 = _this3.props,
onValueChange = _props2.onValueChange,
children = _props2.children;
var newIndex = event.nativeEvent.newIndex;
var childArray = _react2.default.Children.toArray(children);
onValueChange && onValueChange(_this3.state.items[newIndex].value, newIndex % childArray.length);
_this3.setState({
selectedIndex: newIndex
});
};
};
PickerView.Item = Item;
exports.default = PickerView;