import React from 'react'; import { View, Text, Pressable } from 'react-native'; import Space from '../Space'; import { useThemeFactory } from '../Theme'; import { useControllableValue } from '../hooks'; import type { SelectorValue, SelectorProps, SelectorOption } from './type'; import { CheckMark } from './CheckMark'; import { createStyle } from './style'; // https://mobile.ant.design/zh/components/selector const defaultProps = { multiple: false, defaultValue: [], showCheckMark: true, }; const Selector = (p: SelectorProps) => { const props = { ...defaultProps, ...p }; const [value, setValue] = useControllableValue(props); const { styles } = useThemeFactory(createStyle); const handleChange = (val: V[]) => { const extend = { get items() { return props.options.filter(option => val.includes(option.value)); }, }; setValue(val, extend); }; const handlePress = (option: SelectorOption, active: boolean) => { if (props.multiple) { const val = active ? value.filter(v => v !== option.value) : [...value, option.value]; handleChange(val); } else { const val = active ? [] : [option.value]; handleChange(val); } }; const items = props.options.map(option => { const active = (value || []).includes(option.value); const disabled = option.disabled || props.disabled; return ( handlePress(option, active)} > {option.label} {option.description && {option.description}} {active && props.showCheckMark && ( )} ); }); return ( {items} ); }; export default Selector;