#
zhou zhou
1 天以前 2fb1e4fec9fd815c71291f65317c25c8f89de060
rsf-admin/src/page/components/WarehouseSelect.jsx
@@ -2,14 +2,29 @@
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, value, onChange, ...params } = 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();
@@ -31,19 +46,16 @@
    const handleChange = (event) => {
        const selectedValue = event.target.value;
        console.log(event);
        if (onChange) {
            onChange(event);
        }
        field.onChange(selectedValue);
    };
    const validValue = list.some(item => item.id === value) ? value : '';
    const validValue = list.some(item => item.id === field.value) ? field.value : '';
    return (
        <FormControl required fullWidth>
            <InputLabel id="demo-select-small-label">{label}</InputLabel>
        <FormControl required fullWidth error={!!fieldState.error}>
            <InputLabel id={`${name}-label`}>{label}</InputLabel>
            <Select
                labelId="demo-select-small-label"
                labelId={`${name}-label`}
                value={validValue}
                variant="filled"
                onChange={handleChange}