|
| 1 | +import type { |
| 2 | + BuiltInLocaleKeys, |
| 3 | + EnumInit, |
| 4 | + EnumItemClass, |
| 5 | + EnumKey, |
| 6 | + EnumValue, |
| 7 | + IEnum, |
| 8 | + PluginFunc, |
| 9 | + ValueTypeFromSingleInit, |
| 10 | +} from 'enum-plus'; |
| 11 | +import type { StandardEnumInit } from 'lib/types'; |
| 12 | + |
| 13 | +// eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 14 | +export type PluginOptions = Pick<ToSelectConfig<any>, 'labelField' | 'valueField'>; |
| 15 | + |
| 16 | +const toSelectPlugin: PluginFunc<PluginOptions> = (options, Enum) => { |
| 17 | + const { labelField: globalLabelField, valueField: globalValueField } = options ?? {}; |
| 18 | + Enum.extends({ |
| 19 | + toList<FL extends string = string, FV extends string = string>( |
| 20 | + this: IEnum<StandardEnumInit<string, EnumValue>, string, EnumValue>, |
| 21 | + config: ToSelectConfig<StandardEnumInit<string, EnumValue>, FL, FV, string, EnumValue> & |
| 22 | + FirstOptionConfig<FL, FV, EnumValue> = {} |
| 23 | + ): SelectItem<FL, FV, EnumValue>[] { |
| 24 | + const { |
| 25 | + firstOption = false, |
| 26 | + labelField = globalLabelField ?? 'label', |
| 27 | + valueField = globalValueField ?? 'value', |
| 28 | + } = config; |
| 29 | + const selectItems = this.items.map( |
| 30 | + (item) => |
| 31 | + ({ |
| 32 | + [valueField]: item.value, |
| 33 | + [labelField]: item.label, |
| 34 | + }) as SelectItem<FL, FV, EnumValue> |
| 35 | + ); |
| 36 | + if (firstOption) { |
| 37 | + if (firstOption === true) { |
| 38 | + // the first option |
| 39 | + const value = '' as EnumValue; |
| 40 | + const label = Enum.localize |
| 41 | + ? Enum.localize('enum-plus.options.all' as BuiltInLocaleKeys) |
| 42 | + : 'enum-plus.options.all'; |
| 43 | + return [{ [valueField]: value, [labelField]: label } as SelectItem<FL, FV, EnumValue>, ...selectItems]; |
| 44 | + } else { |
| 45 | + return [ |
| 46 | + { |
| 47 | + ...firstOption, |
| 48 | + [labelField]: Enum.localize |
| 49 | + ? Enum.localize(firstOption[labelField as FL]) |
| 50 | + : firstOption[labelField as FL], |
| 51 | + }, |
| 52 | + ...selectItems, |
| 53 | + ]; |
| 54 | + } |
| 55 | + } else { |
| 56 | + return selectItems; |
| 57 | + } |
| 58 | + }, |
| 59 | + }); |
| 60 | +}; |
| 61 | + |
| 62 | +/** More options for the options method */ |
| 63 | +export interface ToSelectConfig< |
| 64 | + T extends EnumInit<K, V>, |
| 65 | + OV extends string | ((item: EnumItemClass<T[K], K, V>) => string) = string, |
| 66 | + OL extends string | ((item: EnumItemClass<T[K], K, V>) => string) = string, |
| 67 | + K extends EnumKey<T> = EnumKey<T>, |
| 68 | + V extends EnumValue = ValueTypeFromSingleInit<T[K], K>, |
| 69 | +> { |
| 70 | + /** |
| 71 | + * **EN:** The name of the value field in the output object, or a function to get the field name, |
| 72 | + * default is `value` |
| 73 | + * |
| 74 | + * **CN:** 输出对象的value字段名,或者获取字段名的函数,默认为 `value` |
| 75 | + */ |
| 76 | + valueField?: OV; |
| 77 | + /** |
| 78 | + * **EN:** The name of the label field in the output object, or a function to get the field name, |
| 79 | + * default is `label` |
| 80 | + * |
| 81 | + * **CN:** 输出对象的label字段名,或者获取字段名的函数,默认为 `label` |
| 82 | + */ |
| 83 | + labelField?: OL; |
| 84 | +} |
| 85 | + |
| 86 | +export interface FirstOptionConfig<FL extends string = string, FV extends string = string, V = EnumValue> { |
| 87 | + /** |
| 88 | + * **EN:** Add a default option at the top |
| 89 | + * |
| 90 | + * - `true`: the option uses the default value, `value` is `''`, `label` is `'All'`; |
| 91 | + * - `false`: the default option is not added; |
| 92 | + * - `object`: custom default option, must be an object with `key`, `value`, and `label` properties |
| 93 | + * |
| 94 | + * **CN:** 在头部添加一个默认选项 |
| 95 | + * |
| 96 | + * - `true`: 选项使用默认值,`value`为`''`,`label`为`'全部'` |
| 97 | + * - `false`: 不添加默认选项 |
| 98 | + * - `object`: 自定义默认选项 |
| 99 | + * |
| 100 | + * @default false |
| 101 | + */ |
| 102 | + firstOption?: boolean | SelectItem<FL, FV, V>; |
| 103 | +} |
| 104 | + |
| 105 | +/** Data structure of ant-design Select options */ |
| 106 | +export type SelectItem<FL extends string = string, FV extends string = string, V = EnumValue> = Record<FV, V> & |
| 107 | + Record<FL, string>; |
| 108 | + |
| 109 | +export default toSelectPlugin; |
| 110 | + |
| 111 | +declare module 'enum-plus-extend' { |
| 112 | + interface EnumExtension< |
| 113 | + T extends EnumInit<K, V>, |
| 114 | + K extends EnumKey<T> = EnumKey<T>, |
| 115 | + V extends EnumValue = ValueTypeFromSingleInit<T[K], K>, |
| 116 | + > { |
| 117 | + toList(this: IEnum<T, K, V> & EnumExtension<T, K, V>): SelectItem<'label', 'value', V>[]; |
| 118 | + toList< |
| 119 | + OV extends string | ((item: EnumItemClass<T[K], K, V>) => string) = string, |
| 120 | + OL extends string | ((item: EnumItemClass<T[K], K, V>) => string) = string, |
| 121 | + >( |
| 122 | + this: IEnum<T, K, V> & EnumExtension<T, K, V>, |
| 123 | + config: ToSelectConfig<T, OV, OL, K, V> & |
| 124 | + FirstOptionConfig< |
| 125 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 126 | + OL extends (...args: any) => any ? ReturnType<OL> : OL, |
| 127 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 128 | + OV extends (...args: any) => any ? ReturnType<OV> : OV, |
| 129 | + V |
| 130 | + > |
| 131 | + ): SelectItem< |
| 132 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 133 | + OL extends (...args: any) => any ? ReturnType<OL> : OL, |
| 134 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 135 | + OV extends (...args: any) => any ? ReturnType<OV> : OV, |
| 136 | + V |
| 137 | + >[]; |
| 138 | + } |
| 139 | +} |
0 commit comments