UNPKG

mh-rn-component

Version:

116 lines (103 loc) 3.68 kB
function _extends() { _extends = Object.assign ? Object.assign.bind() : 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; }; return _extends.apply(this, arguments); } import React, { useState, useEffect } from 'react'; import { View } from 'react-native'; import Checkbox from '../Checkbox'; /** * todo 目前先用any保证运行 */ const CheckboxGroup = /*#__PURE__*/React.forwardRef((_ref, ref) => { let { checked, onChange, options, ...rest } = _ref; const [_value, setValue] = useState(checked); const [_optionsAll, setOptionsAll] = useState([]); const checkRadio = val => { const _have = _value.includes(val); setValue(prev => { if (_have) { prev.splice(prev.indexOf(val), 1); } else { prev.push(val); } return [...prev]; }); onChange(_value); }; useEffect(() => { let _optionsValueAll = []; options && options.map(item => { _optionsValueAll.push(item.value); }); rest.children && React.Children.toArray(rest.children).map(child => { _optionsValueAll.push(child === null || child === void 0 ? void 0 : child.props.value); }); setOptionsAll([..._optionsValueAll]); }, []); const toggleAll = status => { /** * 全选和取消全选 */ if (typeof status === 'boolean') { if (status) { setValue([..._optionsAll]); onChange(_optionsAll); } else { setValue([]); onChange([]); } } else { // 取反 let reverseArr = []; _optionsAll.map(item => { if (!_value.includes(item)) { reverseArr.push(item); } }); setValue([...reverseArr]); onChange(reverseArr); } }; const root = React.useRef(null); React.useImperativeHandle(ref, () => { const input = root.current; if (input) { return { toggleAll: toggleAll }; } const noop = () => { throw new Error('Checkbox is not available'); }; return { toggleAll: noop }; }); return /*#__PURE__*/React.createElement(View, { style: rest.style, ref: root }, /*#__PURE__*/React.createElement(React.Fragment, null, options && options.map((item, index) => { return /*#__PURE__*/React.createElement(Checkbox, _extends({ onPress: checkRadio, checked: _value.includes(item.value), key: index }, item, rest)); }), rest.children && React.Children.toArray(rest.children).map(child => { return /*#__PURE__*/React.cloneElement(child, { onPress: checkRadio, checked: _value.includes(child === null || child === void 0 ? void 0 : child.props.value), name: rest.name, disabled: (child === null || child === void 0 ? void 0 : child.props.disabled) || rest.disabled, activeIcon: (child === null || child === void 0 ? void 0 : child.props.activeIcon) || rest.activeIcon, inactiveIcon: (child === null || child === void 0 ? void 0 : child.props.inactiveIcon) || rest.inactiveIcon, rightIcon: (child === null || child === void 0 ? void 0 : child.props.rightIcon) || rest.rightIcon, checkedColor: (child === null || child === void 0 ? void 0 : child.props.checkedColor) || rest.checkedColor, children: child === null || child === void 0 ? void 0 : child.props.children, iconSize: (child === null || child === void 0 ? void 0 : child.props.iconSize) || rest.iconSize }); }))); }); export default CheckboxGroup; //# sourceMappingURL=index.js.map