import React, { useState, useEffect } from 'react'; import { View, Pressable } from 'react-native'; import Node from '../node'; import styles from './style'; const prefixCls = 'selector'; export interface OptionProps { value: string | number; text: string; children?: OptionProps[]; } export interface SelectorProps { options?: OptionProps[]; value?: string[]; style?: React.CSSProperties; onChange?: (value: any, valueStr?: string) => void; onChangeParentOption?: (e: any) => void; [key: string]: any; } let currentParent: any = null; const Selector = (props: SelectorProps) => { const { value = [], options = [], style, onChange, onChangeParentOption } = props; const [parentData, setParentData] = useState([]); const [childrenData, setChildrenData] = useState([]); const [activeParent, setActiveParent] = useState(''); useEffect(() => { if (options.length > 0) { const data = options[0].children || []; currentParent = options[0]; setActiveParent(currentParent.value); setParentData(options); setChildrenData(data); } }, [options]); const handleClickParentOption = (option: OptionProps) => { const data = option.children || []; currentParent = option; setChildrenData(data); setActiveParent(option.value); onChangeParentOption?.(option); }; const handleClickChildrenOption = (option: OptionProps) => { onChange?.([currentParent.value, option.value], `${currentParent.text} ${option.text}`); }; return ( {parentData?.map((option, index) => ( { handleClickParentOption(option); }} > {option.text} {activeParent === option.value && index !== 0 ? ( ) : null} {activeParent === option.value ? ( ) : null} ))} {childrenData?.map(option => ( { handleClickChildrenOption(option); }} > {option.text} ))} ); }; export default Selector;