import React, { useRef } from 'react'; import { TreeSelect, Input } from 'antd'; import { TreeSelectProps } from 'antd/es/tree-select'; export interface CustomTreeSelectProps extends TreeSelectProps { value?: string; onlySelectLeaf?: boolean; onChange?: (value: string) => void; } const InputGroup = Input.Group; const CustomTreeSelect: React.FC = props => { const { value = '', onChange: propOnChange, onlySelectLeaf, ...restProps } = props; const ref = useRef(); const onChange = (path: string, fileName: string) => { propOnChange(`${path}/${fileName}`.replace(/\/\//g, '/')); }; const fileArray = value.split('/'); const name = fileArray.pop(); const filePath = fileArray.join('/') || '/'; const realValue = onlySelectLeaf ? value : filePath; const selectDom = ( ref.current || document.body} onSelect={(_, node) => { if (onlySelectLeaf) { propOnChange(node.value as string); return; } if (onChange) { onChange(node.value as string, name); } }} {...restProps} /> ); return (
{onlySelectLeaf ? ( selectDom ) : ( {selectDom} { onChange(filePath, e.target.value); }} /> )}
); }; export default CustomTreeSelect;