Skip to content

Commit e937037

Browse files
committed
feat: add toSelectPlugin
1 parent c1a081e commit e937037

File tree

6 files changed

+36
-109
lines changed

6 files changed

+36
-109
lines changed

packages/plugin/src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import { Enum } from 'enum-plus';
22
import searchItemsPlugin from './plugins/searchItems';
3+
import toSelectPlugin from './plugins/toSelect';
34

45
Enum.install(searchItemsPlugin);
6+
Enum.install(toSelectPlugin);

packages/plugin/src/plugins/searchItems.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import type {
22
EnumInit,
33
EnumItemClass,
44
EnumItemInit,
5-
EnumItemOptions,
65
EnumKey,
76
EnumValue,
87
PluginFunc,

packages/plugin/src/plugins/toSelect.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@ import type {
1010
} from 'enum-plus';
1111
import type { StandardEnumInit } from 'lib/types';
1212

13-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
14-
export type PluginOptions = Pick<ToSelectConfig<any>, 'labelField' | 'valueField'>;
13+
export type PluginOptions = Pick<ToSelectConfig<EnumInit>, 'labelField' | 'valueField'>;
1514

1615
const toSelectPlugin: PluginFunc<PluginOptions> = (options, Enum) => {
1716
const { labelField: globalLabelField, valueField: globalValueField } = options ?? {};

packages/plugin/tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@
66
"@enum-plus-plugin/*": ["./src/*"]
77
}
88
},
9-
"include": ["./src", "./test", "./e2e", "./playwright.config.ts", "./global.d.ts"],
9+
"include": ["./src", "./test", "./e2e", "./playwright.config.ts", "../.."],
1010
"exclude": ["node_modules", "es", "lib"]
1111
}

src/index.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,9 @@ export type {
77
EnumKey,
88
EnumValue,
99
ValueTypeFromSingleInit,
10-
EnumOptionConfig,
1110
BuiltInLocaleKeys,
1211
EnumItemOptions,
13-
EnumItemOptionData,
12+
EnumListItem,
1413
MenuItemOption,
1514
ColumnFilterItem,
1615
EnumInitOptions,

src/types.ts

Lines changed: 31 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -171,59 +171,44 @@ export interface IEnumItems<
171171
has(keyOrValue?: string | V): boolean;
172172

173173
/**
174-
* **EN:** Generate an array of objects that can be bound to those `options like` of components
175-
* such as Select, Radio, and Checkbox, following the data specification of ant-design
174+
* **EN:** Generate an object array containing all enumeration items
176175
*
177-
* **CN:** 生成一个对象数组,可以绑定到 Select、Radio、Checkbox 等组件的`options`,遵循 ant-design 的数据规范
176+
* **CN:** 生成一个对象数组,包含所有的枚举项
178177
*
179178
* @example
180179
* [
181180
* { value: 0, label: 'Sunday' },
182181
* { value: 1, label: 'Monday' },
183182
* ];
184-
*
185-
* @see https://ant.design/components/checkbox-cn#option
186183
*/
187-
// eslint-disable-next-line @typescript-eslint/method-signature-style
188-
toList(): EnumItemOptionData<K, V>[];
184+
toList(): EnumListItem<'label', 'value'>[];
189185
/**
190-
* **EN:** Generate an array of objects that can be bound to those `options like` of components
191-
* such as Select, Radio, and Checkbox, following the data specification of ant-design
186+
* **EN:** Generate an object array containing all enumeration items, with customizable value and
187+
* label field names
192188
*
193-
* **CN:** 生成一个对象数组,可以绑定到 Select、Radio、Checkbox 等组件的`options`,遵循 ant-design 的数据规范
189+
* **CN:** 生成一个对象数组,包含所有的枚举项,可自定义值和标签字段名
194190
*
195191
* @example
196192
* [
197193
* { value: 0, label: 'Sunday' },
198194
* { value: 1, label: 'Monday' },
199195
* ];
200196
*
201-
* @param config Custom options | 自定义选项
202-
*
203-
* @see https://ant.design/components/checkbox-cn#option
197+
* @param config Custom options, supports customizing value and label field names |
198+
* 自定义选项,支持自定义值和标签字段名
204199
*/
205-
// eslint-disable-next-line @typescript-eslint/method-signature-style
206-
toList(config: ToListConfig & BooleanFirstOptionConfig<V>): EnumItemOptionData<K | '', V | ''>[];
207-
/**
208-
* **EN:** Generate an array of objects that can be bound to those `options like` of components
209-
* such as Select, Radio, and Checkbox, following the data specification of ant-design
210-
*
211-
* **CN:** 生成一个对象数组,可以绑定到 Select、Radio、Checkbox 等组件的`options`,遵循 ant-design 的数据规范
212-
*
213-
* @example
214-
* [
215-
* { value: 0, label: 'Sunday' },
216-
* { value: 1, label: 'Monday' },
217-
* ];
218-
*
219-
* @param config Custom options | 自定义选项
220-
*
221-
* @see https://ant.design/components/checkbox-cn#option
222-
*/
223-
// eslint-disable-next-line @typescript-eslint/method-signature-style
224-
toList<FK, FV>(
225-
config: ToListConfig & ObjectFirstOptionConfig<FK, FV>
226-
): EnumItemOptionData<K | (FK extends never ? FV : FK), V | (FV extends never ? V : FV)>[];
200+
toList<
201+
OV extends string | ((item: EnumItemClass<T[K], K, V>) => string) = string,
202+
OL extends string | ((item: EnumItemClass<T[K], K, V>) => string) = string,
203+
>(
204+
config: ToListConfig<T, OL, OV, K, V>
205+
): EnumListItem<
206+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
207+
OL extends (...args: any) => any ? ReturnType<OL> : OL,
208+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
209+
OV extends (...args: any) => any ? ReturnType<OV> : OV,
210+
V
211+
>[];
227212

228213
/**
229214
* **EN:** Generate an object array that can be bound to the data source of components such as
@@ -415,15 +400,13 @@ export interface LabelOnlyEnumItemInit {
415400
}
416401
export type CompactEnumItemInit = Record<string, never>; // 等价于{}
417402

418-
/** Data structure of ant-design Select options */
419-
export interface EnumItemOptionData<K, V> {
420-
/** Option value */
421-
value: V;
422-
/** Option label */
423-
label: string;
424-
/** Option key, default is `value` */
425-
key: K;
426-
}
403+
/**
404+
* **EN:** Data structure of enumeration item options, used in `toList` method
405+
*
406+
* **CN:** 枚举项选项的数据结构,用于`toList`方法
407+
*/
408+
export type EnumListItem<FL extends string = string, FV extends string = string, V = EnumValue> = Record<FV, V> &
409+
Record<FL, string>;
427410

428411
/** Data structure of column filter items of ant-design Table */
429412
export interface ColumnFilterItem<V> {
@@ -453,6 +436,8 @@ export type EnumKey<T> = keyof T;
453436
/** More options for the options method */
454437
export interface ToListConfig<
455438
T extends EnumInit<K, V>,
439+
OV extends string | ((item: EnumItemClass<T[K], K, V>) => string) = string,
440+
OL extends string | ((item: EnumItemClass<T[K], K, V>) => string) = string,
456441
K extends EnumKey<T> = EnumKey<T>,
457442
V extends EnumValue = ValueTypeFromSingleInit<T[K], K>,
458443
> {
@@ -462,73 +447,16 @@ export interface ToListConfig<
462447
*
463448
* **CN:** 输出对象的value字段名,或者获取字段名的函数,默认为 `value`
464449
*/
465-
valueField?: string | ((item: EnumItemClass<T[K], K, V>[]) => string);
450+
valueField?: OV;
466451
/**
467452
* **EN:** The name of the label field in the output object, or a function to get the field name,
468453
* default is `label`
469454
*
470455
* **CN:** 输出对象的label字段名,或者获取字段名的函数,默认为 `label`
471456
*/
472-
labelField?: string | ((item: EnumItemClass<T[K], K, V>[]) => string);
457+
labelField?: OL;
473458
}
474459

475-
export interface BooleanFirstOptionConfig<V> {
476-
/**
477-
* **EN:** Add a default option at the top
478-
*
479-
* - `true`: the option uses the default value, `value` is `''`, `label` is `'All'`;
480-
* - `false`: the default option is not added;
481-
*
482-
* **CN:** 在头部添加一个默认选项
483-
*
484-
* - `true`:选项使用默认值,`value`为`''`,`label`为`'全部'`
485-
* - `false`:不添加默认选项
486-
*
487-
* @default false
488-
*/
489-
firstOption: boolean;
490-
/**
491-
* **EN:** Default option value, default is `''`
492-
*
493-
* **CN:** 默认选项的值,默认为`''`
494-
*
495-
* @default ''
496-
*/
497-
firstOptionValue?: V;
498-
/**
499-
* **EN:** Default option label, default is `'All'`. If a localization method is set, the
500-
* localization method will be automatically called
501-
*
502-
* **CN:** 默认选项的显示文本,默认为`'All'`。如果设置了本地化方法,则会自动调用本地化方法
503-
*/
504-
firstOptionLabel?: string;
505-
}
506-
507-
export interface ObjectFirstOptionConfig<K, V> {
508-
/**
509-
* **EN:** Configuration of the first option
510-
*
511-
* **CN:** 首行选项的配置
512-
*/
513-
firstOption?: EnumOptionConfig<K, V>;
514-
/**
515-
* **EN:** Add a default option at the top
516-
*
517-
* **CN:** 默认选项的值,默认为`''`
518-
*/
519-
firstOptionValue?: never;
520-
/**
521-
* **EN:** Default option label, default is `'All'`. If a localization method is set, the
522-
* localization method will be automatically called
523-
*
524-
* **CN:** 默认选项的显示文本,默认为`'All'`。如果设置了本地化方法,则会自动调用本地化方法
525-
*/
526-
firstOptionLabel?: never;
527-
}
528-
529-
export type EnumOptionConfig<K, V> = Omit<EnumItemOptionData<K, V>, 'key'> &
530-
Partial<Pick<EnumItemOptionData<K, V>, 'key'>>;
531-
532460
/** Infer the value type from the initialization object of the enumeration item */
533461
export type ValueTypeFromSingleInit<T, Key = string, Fallback = Key> = T extends EnumValue // literal类型
534462
? T

0 commit comments

Comments
 (0)