@fruits-chain/react-native-xiaoshu
Version:
🌈 React Native UI library
81 lines (80 loc) • 2.75 kB
JavaScript
import omit from 'lodash/omit';
import React, { memo, createElement as _createElement } from 'react';
import { ScrollView } from 'react-native';
import { useControllableValue } from "../hooks/index.js";
import Space from "../space/index.js";
import Checkbox from "./checkbox.js";
import { jsx as _jsx } from "react/jsx-runtime";
function CheckboxGroup({
theme,
options,
multiple,
editable = true,
scrollable = false,
deselect = true,
checkboxLabelTextStyle,
activeColor,
iconSize,
checkboxIconLabelGap,
...restProps
}) {
const [value, onChange] = useControllableValue(restProps, {
defaultValue: multiple ? [] : undefined
});
const contentJSX = /*#__PURE__*/_jsx(Space, {
...omit(restProps, ['value', 'defaultValue', 'onChange']),
children: options.map(({
value: checkboxValue,
...checkboxProps
}) => {
const selected = multiple ? value.indexOf(checkboxValue) > -1 : value === checkboxValue;
return /*#__PURE__*/_createElement(Checkbox, {
...checkboxProps,
theme: theme,
labelTextStyle: checkboxProps.labelTextStyle ?? checkboxLabelTextStyle,
gap: checkboxProps.gap ?? checkboxIconLabelGap,
activeColor: checkboxProps.activeColor ?? activeColor,
iconSize: checkboxProps.iconSize ?? iconSize,
key: `${checkboxValue}`,
activeValue: checkboxValue,
inactiveValue: null,
value: selected ? checkboxValue : null,
onChange: _value => {
if (!editable) {
return;
}
const isReset = _value !== checkboxValue;
if (multiple) {
const oldValue = value;
const newValue = isReset ? oldValue.filter(v => v !== checkboxValue) : [checkboxValue, ...oldValue];
const newOptions = newValue.map(v => {
const optionIndex = options.findIndex(o => o.value === v);
return {
...options[optionIndex]
};
});
onChange(newValue, newOptions);
} else {
if (!isReset || isReset && deselect) {
const newValue = isReset ? undefined : _value;
const newOptions = isReset ? undefined : options.filter(o => o.value === _value);
onChange(newValue, newOptions);
}
}
}
});
})
});
if (scrollable && restProps.direction === 'horizontal' && !restProps.wrap) {
return /*#__PURE__*/_jsx(ScrollView, {
horizontal: true,
bouncesZoom: false,
showsHorizontalScrollIndicator: false,
children: contentJSX
});
}
return contentJSX;
}
export default /*#__PURE__*/memo(CheckboxGroup);
//# sourceMappingURL=checkbox-group.js.map
;