数据字典集中治理
This commit is contained in:
@@ -0,0 +1,15 @@
|
||||
import type { Dict, DictItem } from "./types";
|
||||
|
||||
export const createDict = (items: DictItem[]): Dict => {
|
||||
const normalized = items.map((item, idx) => ({ order: idx, ...item }));
|
||||
const get = (value?: string | null) => normalized.find((i) => i.value === value);
|
||||
const list = () =>
|
||||
[...normalized].sort((a, b) => (a.order || 0) - (b.order || 0));
|
||||
return { items: normalized, get, list };
|
||||
};
|
||||
|
||||
export const getDictItem = (dict: Dict, value?: string | null) => dict.get(value);
|
||||
export const getDictLabel = (dict: Dict, value?: string | null) => dict.get(value)?.label || value || "";
|
||||
export const getDictColor = (dict: Dict, value?: string | null) => dict.get(value)?.color;
|
||||
export const getDictOptions = (dict: Dict) =>
|
||||
dict.list().map((i) => ({ label: i.label, value: i.value, disabled: false }));
|
||||
Reference in New Issue
Block a user