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;
|