1
25 分钟以前 12d9f4e03c0331efc9a656356e78b9f314639707
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
import { useState, useEffect } from 'react';
import {
    useTranslate, useNotify, required
} from 'react-admin';
import request from '@/utils/request';
import { Autocomplete, TextField, FormControl } from '@mui/material';
 
const DictSelect = (props) => {
    const { dictTypeCode, label, group, value, onChange, ...params } = props;
    const translate = useTranslate();
    const notify = useNotify();
    const [list, setList] = useState([]);
 
    useEffect(() => {
        http();
    }, [dictTypeCode]);
 
    const http = async () => {
        const res = await request.post('/dictData/page', { dictTypeCode, group });
        if (res?.data?.code === 200) {
            setList(res.data.data.records.map((item) => {
                return {
                    value: item.value,
                    label: item.label
                };
            }));
        } else {
            notify(res.data.msg);
        }
    };
 
    const validValueObj = list.find(item => item.value === value) || null;
 
    const handleChange = (event, newValue) => {
        if (onChange) {
            onChange({ target: { value: newValue ? newValue.value : '' } });
        }
    };
 
    return (
        <FormControl fullWidth size="small">
            <Autocomplete
                value={validValueObj}
                onChange={handleChange}
                options={list}
                getOptionLabel={(option) => option.label || ''}
                isOptionEqualToValue={(option, val) => option.value === val.value}
                size="small"
                ListboxProps={{ style: { maxHeight: '200px' } }}
                renderInput={(p) => (
                    <TextField
                        {...p}
                        label={label}
                        variant="filled"
                    />
                )}
                {...params}
            />
        </FormControl>
    );
};
 
export default DictSelect;