import omit from 'lodash/omit'; import isNumber from 'lodash/isNumber'; import React from 'react'; import { ScrollView, View } from 'react-native'; import { useControllableValue } from 'ahooks'; import Space from '../space'; import ButtonOption from './button-option'; import type { ButtonOptionGroupProps } from './interface'; function ButtonOptionGroup({ activeHighlight = true, type = 'hazy', editable = true, scrollable = false, deselect = true, scrollableWrap = false, options, multiple, onDisabledPress, ...restProps }: ButtonOptionGroupProps) { const [value, onChange] = useControllableValue(restProps, { defaultValue: multiple ? [] : undefined, }); const contentJSX = ( {options.map((item) => { const selected = multiple ? (value as T[])?.indexOf(item.value) > -1 : value === item.value; return ( { if (!editable) { return; } if (multiple) { const oldValue = value as T[]; 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) { // @ts-ignore onChange(null, []); } } else { onChange( item.value, options.filter((o) => o.value === item.value) ); } } }} onDisabledPress={() => onDisabledPress?.(item)} /> ); })} {/* 选项条右侧fake padding */} {isNumber(restProps.fakeRightPadding) && ( )} ); if (scrollable && !restProps.wrap) { return ( {contentJSX} ); } if (scrollable && scrollableWrap) { return {contentJSX}; } return contentJSX; } export default ButtonOptionGroup;