mh-rn-component
Version:
135 lines (112 loc) • 5.22 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _react = _interopRequireWildcard(require("react"));
var _reactNative = require("react-native");
var _Checkbox = _interopRequireDefault(require("../Checkbox"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
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); }
/**
* todo 目前先用any保证运行
*/
const CheckboxGroup = /*#__PURE__*/_react.default.forwardRef((_ref, ref) => {
let {
checked,
onChange,
options,
...rest
} = _ref;
const [_value, setValue] = (0, _react.useState)(checked);
const [_optionsAll, setOptionsAll] = (0, _react.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);
};
(0, _react.useEffect)(() => {
let _optionsValueAll = [];
options && options.map(item => {
_optionsValueAll.push(item.value);
});
rest.children && _react.default.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.default.useRef(null);
_react.default.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.default.createElement(_reactNative.View, {
style: rest.style,
ref: root
}, /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, options && options.map((item, index) => {
return /*#__PURE__*/_react.default.createElement(_Checkbox.default, _extends({
onPress: checkRadio,
checked: _value.includes(item.value),
key: index
}, item, rest));
}), rest.children && _react.default.Children.toArray(rest.children).map(child => {
return /*#__PURE__*/_react.default.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
});
})));
});
var _default = CheckboxGroup;
exports.default = _default;
//# sourceMappingURL=index.js.map