skyouc
8 天以前 b9d414bc2d61b4824ce6a019b1c10f526f71ced1
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
import { useState, useEffect } from 'react';
import {
    useTranslate, useNotify
} from 'react-admin';
import request from '@/utils/request';
import { Select, MenuItem, FormControl, InputLabel } from '@mui/material';
 
const DictSelect = (props) => {
    const { dictTypeCode, label, 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 });
        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 handleChange = (event) => {
        const selectedValue = event.target.value;
        if (onChange) {
            onChange(event);
        }
    };
 
    const validValue = list.some(item => item.value === value) ? value : '';
 
    return (
        <FormControl fullWidth>
            <InputLabel id="demo-select-small-label">{label}</InputLabel>
            <Select
                labelId="demo-select-small-label"
                value={validValue}
                onChange={handleChange}
                size='small'
            >
                {list.map((item) => (
                    <MenuItem key={item.value} value={item.value}>
                        {item.label}
                    </MenuItem>
                ))}
            </Select>
        </FormControl>
    );
};
 
export default DictSelect;