UNPKG

tuya-panel-kit

Version:

a functional component library for developing tuya device panels!

329 lines (265 loc) 12.2 kB
Object.defineProperty(exports, "__esModule", { value: true }); 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/dialog/checkbox.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 _list = require('../TYLists/list'); var _list2 = _interopRequireDefault(_list); var _TYText = require('../TYText'); var _TYText2 = _interopRequireDefault(_TYText); var _footer = require('./footer'); var _footer2 = _interopRequireDefault(_footer); var _styled = require('./styled'); var _withMotion = require('./withMotion'); var _withMotion2 = _interopRequireDefault(_withMotion); var _utils = require('../../utils'); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } 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 _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 _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 getTheme = _utils.ThemeUtils.getTheme, ThemeConsumer = _utils.ThemeUtils.ThemeConsumer; var ITEM_HEIGHT = 48; var CheckBoxDialog = function (_Component) { _inherits(CheckBoxDialog, _Component); function CheckBoxDialog(props) { _classCallCheck(this, CheckBoxDialog); var _this = _possibleConstructorReturn(this, (CheckBoxDialog.__proto__ || Object.getPrototypeOf(CheckBoxDialog)).call(this, props)); _this._handleCheckBoxChange = function (checked, value) { var _this$props = _this.props, type = _this$props.type, onChange = _this$props.onChange; if (type === 'radio') { _this.setState(function () { var newValue = checked ? value : undefined; onChange && onChange(newValue); return { value: newValue }; }); } else if (type === 'switch') { _this.setState(function (prevState) { var newValue = Array.isArray(prevState.value) ? prevState.value : []; if (checked) newValue = [].concat(_toConsumableArray(newValue), [value]);else newValue = newValue.filter(function (v) { return v !== value; }); onChange && onChange(newValue); return { value: newValue }; }); } }; _this._handleConfirm = function () { var onConfirm = _this.props.onConfirm; onConfirm && onConfirm(_this.state.value); }; _this.renderCheckBoxItem = function (_ref) { var item = _ref.item; var _item$styles = item.styles, styles = _item$styles === undefined ? {} : _item$styles, value = item.value, title = item.title, checkboxProps = _objectWithoutProperties(item, ['styles', 'value', 'title']); var isChecked = _this.isChecked(value); return _react2.default.createElement( ThemeConsumer, { __source: { fileName: _jsxFileName, lineNumber: 193 } }, function (globalTheme) { var checkItemProps = { theme: { dialog: _extends({}, globalTheme.dialog) } }; var itemBackGround = getTheme(checkItemProps, 'dialog.bg'); var itemFontColor = getTheme(checkItemProps, 'dialog.titleFontColor'); var itemFontSize = getTheme(checkItemProps, 'dialog.titleFontSize'); return _react2.default.createElement(_list2.default.CheckboxItem, _extends({ styles: _extends({}, styles, { container: [{ height: ITEM_HEIGHT, backgroundColor: itemBackGround }, styles.container], title: [{ fontSize: itemFontSize, color: itemFontColor }, styles.title] }), color: isChecked ? '#44DB5E' : '#e5e5e5' }, checkboxProps, { title: title || value, checked: isChecked, onChange: function onChange(checked) { return _this._handleCheckBoxChange(checked, value); }, __source: { fileName: _jsxFileName, lineNumber: 200 } })); } ); }; _this.state = { value: props.value }; if (props.type === 'switch' && !Array.isArray(props.value)) { console.warn('CheckBoxDialog: 复选框的 value 必须为 数组'); } return _this; } _createClass(CheckBoxDialog, [{ key: 'componentWillReceiveProps', value: function componentWillReceiveProps(nextProps) { if (this.state.value !== nextProps.value && this.props.value !== nextProps.value) { this.setState({ value: nextProps.value }); } } }, { key: 'isChecked', value: function isChecked(value) { var type = this.props.type; if (type === 'radio') { return this.state.value === value; } else if (type === 'switch') { return this.state.value.some(function (v) { return v === value; }); } return false; } }, { key: 'render', value: function render() { var _props = this.props, maxItemNum = _props.maxItemNum, style = _props.style, headerStyle = _props.headerStyle, titleNumberOfLines = _props.titleNumberOfLines, title = _props.title, titleStyle = _props.titleStyle, subTitle = _props.subTitle, subTitleStyle = _props.subTitleStyle, contentStyle = _props.contentStyle, dataSource = _props.dataSource, confirmText = _props.confirmText, confirmTextStyle = _props.confirmTextStyle, confirmAccessibilityLabel = _props.confirmAccessibilityLabel, footerWrapperStyle = _props.footerWrapperStyle, cancelText = _props.cancelText, cancelTextStyle = _props.cancelTextStyle, cancelAccessibilityLabel = _props.cancelAccessibilityLabel, onCancel = _props.onCancel, TYFlatListProps = _objectWithoutProperties(_props, ['maxItemNum', 'style', 'headerStyle', 'titleNumberOfLines', 'title', 'titleStyle', 'subTitle', 'subTitleStyle', 'contentStyle', 'dataSource', 'confirmText', 'confirmTextStyle', 'confirmAccessibilityLabel', 'footerWrapperStyle', 'cancelText', 'cancelTextStyle', 'cancelAccessibilityLabel', 'onCancel']); return _react2.default.createElement( _styled.StyledContainer, { style: style, __source: { fileName: _jsxFileName, lineNumber: 244 } }, _react2.default.createElement( _styled.StyledHeader, { style: headerStyle, __source: { fileName: _jsxFileName, lineNumber: 245 } }, _react2.default.createElement( _styled.StyledTitle, { style: titleStyle, numberOfLines: titleNumberOfLines, __source: { fileName: _jsxFileName, lineNumber: 246 } }, title ), !!subTitle && _react2.default.createElement( _styled.StyledSubTitle, { style: subTitleStyle, __source: { fileName: _jsxFileName, lineNumber: 249 } }, subTitle ) ), _react2.default.createElement(_styled.StyledCheckboxList, _extends({ style: [contentStyle, { maxHeight: ITEM_HEIGHT * maxItemNum }], scrollEnabled: dataSource.length > maxItemNum, keyExtractor: function keyExtractor(item) { return item.value; }, data: dataSource, renderItem: this.renderCheckBoxItem }, TYFlatListProps, { __source: { fileName: _jsxFileName, lineNumber: 251 } })), _react2.default.createElement(_footer2.default, { style: footerWrapperStyle, cancelTextStyle: cancelTextStyle, confirmTextStyle: confirmTextStyle, cancelText: cancelText, confirmText: confirmText, cancelAccessibilityLabel: cancelAccessibilityLabel, confirmAccessibilityLabel: confirmAccessibilityLabel, onCancel: onCancel, onConfirm: this._handleConfirm, __source: { fileName: _jsxFileName, lineNumber: 259 } }) ); } }]); return CheckBoxDialog; }(_react.Component); CheckBoxDialog.propTypes = { type: _propTypes2.default.oneOf(['radio', 'switch']), value: _propTypes2.default.oneOfType([_propTypes2.default.string, _propTypes2.default.number, _propTypes2.default.array]).isRequired, maxItemNum: _propTypes2.default.number, dataSource: _propTypes2.default.arrayOf(_propTypes2.default.shape(_extends({}, _list2.default.CheckboxItem.propTypes, { value: _propTypes2.default.string.isRequired }))).isRequired, onChange: _propTypes2.default.func, style: _reactNative.ViewPropTypes.style, headerStyle: _reactNative.ViewPropTypes.style, titleNumberOfLines: _propTypes2.default.number, title: _propTypes2.default.string.isRequired, titleStyle: _TYText2.default.propTypes.style, subTitle: _propTypes2.default.string, subTitleStyle: _TYText2.default.propTypes.style, contentStyle: _reactNative.ViewPropTypes.style, footerWrapperStyle: _reactNative.ViewPropTypes.style, cancelText: _propTypes2.default.string.isRequired, cancelTextStyle: _TYText2.default.propTypes.style, cancelAccessibilityLabel: _propTypes2.default.string, confirmText: _propTypes2.default.string.isRequired, confirmTextStyle: _TYText2.default.propTypes.style, confirmAccessibilityLabel: _propTypes2.default.string, onCancel: _propTypes2.default.func, onConfirm: _propTypes2.default.func }; CheckBoxDialog.defaultProps = { type: 'radio', maxItemNum: 5, style: null, headerStyle: null, titleNumberOfLines: 2, titleStyle: null, subTitle: '', subTitleStyle: null, contentStyle: null, footerWrapperStyle: null, cancelTextStyle: null, cancelAccessibilityLabel: 'Dialog.Cancel', confirmTextStyle: null, confirmAccessibilityLabel: 'Dialog.Confirm', onChange: null, onCancel: null, onConfirm: null }; exports.default = (0, _withMotion2.default)(CheckBoxDialog);