import React, {useEffect, useState} from 'react'; import { TreeSelect } from 'antd'; const treeData = [ { value: 'parent 1-value', title: 'parent 1-title', children: [ { value: 'parent 1-0-value', title: 'parent 1-0-title', children: [ { value: 'leaf1-value', title: 'leaf1-title', }, { value: 'leaf2-value', title: 'leaf2-title', }, ], }, ], }, ];
const SelfTreeSelect = (props:any) => { const { value,onChange } = props; console.log('查看传入的值',props) useEffect(() => { setCurrentValue(value?.list[0]) }, [props]); const [currentValue, setCurrentValue] = useState<string>();
const onSelectChange = (newValue: string,selectList:never[]) => { console.log('输出新值第一个为选中的value,第二个为选中的title数组',newValue,selectList) setCurrentValue(newValue); onChange({ value:newValue, list:selectList, }) }; return ( <TreeSelect showSearch style={{ width: '100%' }} value={currentValue} dropdownStyle={{ maxHeight: 400, overflow: 'auto' }} placeholder="Please select" allowClear treeDefaultExpandAll onChange={onSelectChange} treeData={treeData} /> ); };
export default SelfTreeSelect;
|