zhou zhou
3 小时以前 6103a8e5d2316af7fcf6b246e9e866e5216476f0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import React, { useState, useEffect } from 'react';
import { useCreateContext, useTranslate, useRecordContext } from 'react-admin'
import { Autocomplete, TextField, FormControl } from '@mui/material';
import request from '@/utils/request';
import * as Common from '@/utils/common';
import { useFormContext } from 'react-hook-form';
 
const TreeSelectInput = ({ resource, required, onChange, label, source = 'parentId', value, isTranslate = false, ...rest }) => {
    const translate = useTranslate();
    const form = useFormContext();
    const [filter, setFilter] = React.useState("");
    const [treeData, setTreeData] = React.useState([]);
 
    const [proxyVal, setProxyVal] = React.useState('');
 
    const record = useRecordContext()
    const val = value || record?.[source];
 
    useEffect(() => {
        const http = async (resource) => {
            const res = await request.post(resource + '/tree', {
                condition: filter
            });
            if (res?.data?.code === 200) {
                // Add a null option at the beginning
                const flatTree = Common.flattenTree(res.data.data);
                setTreeData([{ id: 0, name: ' ' }, ...flatTree]);
                setProxyVal(val);
            } else {
                notify(res.data.msg);
            }
        }
        http(resource);
    }, [filter, value]);
 
    const handleChange = (event, newValue) => {
        const selectedVal = newValue ? newValue.id : 0;
        setProxyVal(selectedVal);
        form?.setValue(source, selectedVal, {
            shouldValidate: true,
            shouldDirty: true,
        });
        if (onChange) {
            onChange({ target: { value: selectedVal } })
        }
    };
 
    const validValueObj = treeData.find(item => item.id === (proxyVal || 0)) || null;
 
    return (
        <FormControl fullWidth required={required} size="small">
            <Autocomplete
                value={validValueObj}
                onChange={handleChange}
                options={treeData}
                getOptionLabel={(option) => isTranslate && option.type === 0 ? translate(option.name) : option.name}
                isOptionEqualToValue={(option, val) => option.id === val.id}
                size="small"
                ListboxProps={{ style: { maxHeight: '300px' } }}
                renderOption={(props, option) => (
                    <li {...props} style={{ paddingLeft: (option.depth || 0) * 16 + 16 }}>
                        {isTranslate && option.type === 0 ? translate(option.name) : option.name}
                    </li>
                )}
                renderInput={(params) => (
                    <TextField
                        {...params}
                        label={translate(label)}
                        variant="filled"
                        required={required}
                    />
                )}
                {...rest}
            />
        </FormControl>
    );
};
 
export default TreeSelectInput;