52 lines
1.4 KiB
JavaScript
52 lines
1.4 KiB
JavaScript
import { request } from "@cqsjjb/jjb-common-lib/http.js";
|
|
import { useEffect, useState } from "react";
|
|
import { DICTIONARY_APP_KEY_ENUM } from "zy-react-data/enum/dictionary";
|
|
import { executeWithCache } from "../../../utils";
|
|
import BasicCascader from "../Basic";
|
|
|
|
/**
|
|
* 数据字典级联组件
|
|
*/
|
|
function DictionaryCascader(props) {
|
|
const {
|
|
appKey = DICTIONARY_APP_KEY_ENUM.DEFAULT,
|
|
dictValue = "",
|
|
nameKey = "dictLabel",
|
|
idKey = "dictValue",
|
|
...restProps
|
|
} = props;
|
|
|
|
const [treeData, setTreeData] = useState([]);
|
|
|
|
const getData = async () => {
|
|
if (!Object.values(DICTIONARY_APP_KEY_ENUM).includes(appKey)) {
|
|
console.error(`【DictionaryCascader】 传入的 appKey 不在 DICTIONARY_APP_KEY_ENUM 中,当前传入的 appKey 是 ${appKey}`);
|
|
return;
|
|
}
|
|
|
|
if (!dictValue) {
|
|
console.error(`【DictionaryCascader】 缺少 dictValue 参数`);
|
|
return;
|
|
}
|
|
|
|
const cacheKey = { appKey, dictValue };
|
|
|
|
await executeWithCache(cacheKey, async () => {
|
|
const { data } = await request("/config/dict-trees/list/by/dictValues", "get", { appKey, dictValue });
|
|
return data;
|
|
}, setTreeData);
|
|
};
|
|
|
|
useEffect(() => {
|
|
getData();
|
|
}, [dictValue]);
|
|
|
|
return (
|
|
<BasicCascader data={treeData} nameKey={nameKey} idKey={idKey} {...restProps} />
|
|
);
|
|
}
|
|
|
|
DictionaryCascader.displayName = "DictionaryCascader";
|
|
|
|
export default DictionaryCascader;
|