1
2 天以前 8dfd55ef1c0eccf3adf105f0d4f5828bdbc3f86d
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
import { useState, useEffect } from 'react';
import {
    useTranslate, useNotify, required
} from 'react-admin';
import { useController } from 'react-hook-form';
import request from '@/utils/request';
import { Select, MenuItem, FormControl, InputLabel } from '@mui/material';
 
const WarehouseSelect = (props) => {
    const { dictTypeCode, label, name, validate, ...params } = props;
    const translate = useTranslate();
    const notify = useNotify();
    const [list, setList] = useState([]);
 
    // 使用 useController 与 react-hook-form 集成
    const { field, fieldState } = useController({
        name: name,
        rules: validate ? {
            validate: (value) => {
                for (const rule of validate) {
                    const result = rule(value);
                    if (result) return result;
                }
                return true;
            }
        } : undefined
    });
 
    useEffect(() => {
        http();
    }, [dictTypeCode]);
 
    const http = async () => {
        const res = await request.post('/warehouseAreas/page', { current: 1, pageSize: 100 });
        if (res?.data?.code === 200) {
            setList(res.data.data.records.map((item) => {
                return {
                    id: item.id,
                    name: item.name
                };
            }));
        } else {
            notify(res.data.msg);
        }
    };
 
    const handleChange = (event) => {
        const selectedValue = event.target.value;
        field.onChange(selectedValue);
    };
 
    const validValue = list.some(item => item.id === field.value) ? field.value : '';
 
    return (
        <FormControl required fullWidth error={!!fieldState.error}>
            <InputLabel id={`${name}-label`}>{label}</InputLabel>
            <Select
                labelId={`${name}-label`}
                value={validValue}
                variant="filled"
                onChange={handleChange}
                size='small'
            >
                {list.map((item) => (
                    <MenuItem
                        key={item.id}
                        value={item.id}>
                        {item.name}
                    </MenuItem>
                ))}
            </Select>
        </FormControl>
    );
};
 
export default WarehouseSelect;