@enum-plus/plugin-antd
Version: 
A plugin for enum-plus that provides functionality to bind enums to Ant Design
36 lines (35 loc) • 3.14 kB
TypeScript
import { type EnumInit, type EnumItemClass, type EnumKey, type EnumValue, type PluginFunc, type ValueTypeFromSingleInit } from 'enum-plus';
import type { ExactEqual } from 'enum-plus/types';
export type ToSelectPluginOptions = Pick<ToSelectConfig<EnumInit, string, string>, 'labelField' | 'valueField'>;
declare const toSelectPlugin: PluginFunc<ToSelectPluginOptions>;
/** More options for the options method */
export interface ToSelectConfig<T extends EnumInit<K, V>, FOV extends string | ((item: EnumItemClass<T[K], K, V>) => any), FOL extends string | ((item: EnumItemClass<T[K], K, V>) => any), K extends EnumKey<T> = EnumKey<T>, V extends EnumValue = ValueTypeFromSingleInit<T[K], K>> {
    /**
     * - **EN:** The name of the value field in the output object, or a function to get the field name,
     *   default is `value`
     * - **CN:** 输出对象的value字段名,或者获取字段名的函数,默认为 `value`
     */
    valueField?: FOV;
    /**
     * - **EN:** The name of the label field in the output object, or a function to get the field name,
     *   default is `label`
     * - **CN:** 输出对象的label字段名,或者获取字段名的函数,默认为 `label`
     */
    labelField?: FOL;
}
export interface FirstOptionConfig<V extends EnumValue, FOV extends string = 'value', FOL extends string = 'label'> {
    /**
     * - **EN:** Add a default option at the top, the data structure is the same as `SelectItem`
     * - **CN:** 在头部添加一个默认选项,数据结构与 `SelectItem` 相同
     */
    firstOption?: SelectItem<V, FOV, FOL>;
}
/** Data structure of ant-design Select options */
export type SelectItem<V extends EnumValue, FOV extends string = 'value', FOL extends string = 'label'> = Record<FOV, (V extends string ? V | '' : V extends number ? V | 0 : V) | undefined> & Record<FOL, string>;
export default toSelectPlugin;
declare module 'enum-plus/extension' {
    interface EnumExtension<T extends EnumInit<K, V>, K extends EnumKey<T> = EnumKey<T>, V extends EnumValue = ValueTypeFromSingleInit<T[K], K>> {
        toSelect(): SelectItem<V, 'value', 'label'>[];
        toSelect<FOV extends string | ((item: EnumItemClass<T[K], K, V> | undefined) => any), FOL extends string | ((item: EnumItemClass<T[K], K, V> | undefined) => any)>(config: ToSelectConfig<T, FOV, FOL, K, V> & FirstOptionConfig<V, ExactEqual<FOV, string | ((item: EnumItemClass<T[K], K, V> | undefined) => any)> extends true ? 'value' : FOV extends (item: EnumItemClass<T[K], K, V> | undefined) => infer R ? R : FOV, ExactEqual<FOL, string | ((item: EnumItemClass<T[K], K, V> | undefined) => any)> extends true ? 'label' : FOL extends (item: EnumItemClass<T[K], K, V> | undefined) => infer R ? R : FOL>): SelectItem<V, ExactEqual<FOV, string | ((item: EnumItemClass<T[K], K, V> | undefined) => any)> extends true ? 'value' : FOV extends (item: EnumItemClass<T[K], K, V> | undefined) => infer R ? R : FOV, ExactEqual<FOL, string | ((item: EnumItemClass<T[K], K, V> | undefined) => any)> extends true ? 'label' : FOL extends (item: EnumItemClass<T[K], K, V> | undefined) => infer R ? R : FOL>[];
    }
}