UNPKG

@fruits-chain/react-native-xiaoshu

Version:
83 lines (82 loc) • 2.78 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _omit = _interopRequireDefault(require("lodash/omit")); var _react = _interopRequireDefault(require("react")); var _reactNative = require("react-native"); var _index = require("../hooks/index.js"); var _index2 = _interopRequireDefault(require("../space/index.js")); var _buttonOption = _interopRequireDefault(require("./button-option.js")); var _jsxRuntime = require("react/jsx-runtime"); function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; } function ButtonOptionGroup({ theme, activeHighlight = true, type = 'hazy', round = false, editable = true, scrollable = false, deselect = true, options, multiple, ...restProps }) { const [value, onChange] = (0, _index.useControllableValue)(restProps, { defaultValue: multiple ? [] : undefined }); const contentJSX = /*#__PURE__*/(0, _jsxRuntime.jsx)(_index2.default, { ...(0, _omit.default)(restProps, ['value', 'defaultValue', 'onChange']), direction: "horizontal", children: options.map(item => { const selected = multiple ? (value || []).indexOf(item.value) > -1 : value === item.value; return /*#__PURE__*/(0, _jsxRuntime.jsx)(_buttonOption.default, { theme: theme, type: type, round: round, text: item.label, badge: item.badge, disabled: item.disabled, activeHighlight: activeHighlight, active: selected, onPress: () => { if (!editable) { return; } if (multiple) { const oldValue = value || []; const _value = oldValue.filter(v => v !== item.value); const newValue = _value.length === oldValue.length ? [item.value, ...oldValue] : _value; const newOptions = newValue.map(v => { const optionIndex = options.findIndex(o => o.value === v); return { ...options[optionIndex] }; }); onChange(newValue, newOptions); } else { if (item.value === value) { if (deselect) { onChange(null, []); } } else { onChange(item.value, options.filter(o => o.value === item.value)); } } } }, `${item.value}`); }) }); if (scrollable && !restProps.wrap) { return /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.ScrollView, { horizontal: true, bouncesZoom: false, showsHorizontalScrollIndicator: false, children: contentJSX }); } return contentJSX; } var _default = exports.default = ButtonOptionGroup; //# sourceMappingURL=button-option-group.js.map