Skip to content

Commit 634d051

Browse files
committed
feat: add toSelectPlugin
1 parent 28746fd commit 634d051

File tree

6 files changed

+36
-106
lines changed

6 files changed

+36
-106
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 & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -239,56 +239,44 @@ export interface IEnumItems<
239239
has(keyOrValue?: string | V): boolean;
240240

241241
/**
242-
* **EN:** Generate an array of objects that can be bound to those `options like` of components
243-
* such as Select, Radio, and Checkbox, following the data specification of ant-design
242+
* **EN:** Generate an object array containing all enumeration items
244243
*
245-
* **CN:** 生成一个对象数组,可以绑定到 Select、Radio、Checkbox 等组件的`options`,遵循 ant-design 的数据规范
244+
* **CN:** 生成一个对象数组,包含所有的枚举项
246245
*
247246
* @example
248247
* [
249248
* { value: 0, label: 'Sunday' },
250249
* { value: 1, label: 'Monday' },
251250
* ];
252-
*
253-
* @see https://ant.design/components/checkbox-cn#option
254251
*/
255-
toList(): EnumItemOptionData<K, V>[];
252+
toList(): EnumListItem<'label', 'value'>[];
256253
/**
257-
* **EN:** Generate an array of objects that can be bound to those `options like` of components
258-
* such as Select, Radio, and Checkbox, following the data specification of ant-design
254+
* **EN:** Generate an object array containing all enumeration items, with customizable value and
255+
* label field names
259256
*
260-
* **CN:** 生成一个对象数组,可以绑定到 Select、Radio、Checkbox 等组件的`options`,遵循 ant-design 的数据规范
257+
* **CN:** 生成一个对象数组,包含所有的枚举项,可自定义值和标签字段名
261258
*
262259
* @example
263260
* [
264261
* { value: 0, label: 'Sunday' },
265262
* { value: 1, label: 'Monday' },
266263
* ];
267264
*
268-
* @param config Custom options | 自定义选项
269-
*
270-
* @see https://ant.design/components/checkbox-cn#option
265+
* @param config Custom options, supports customizing value and label field names |
266+
* 自定义选项,支持自定义值和标签字段名
271267
*/
272-
toList(config: ToListConfig & BooleanFirstOptionConfig<V>): EnumItemOptionData<K | '', V | ''>[];
273-
/**
274-
* **EN:** Generate an array of objects that can be bound to those `options like` of components
275-
* such as Select, Radio, and Checkbox, following the data specification of ant-design
276-
*
277-
* **CN:** 生成一个对象数组,可以绑定到 Select、Radio、Checkbox 等组件的`options`,遵循 ant-design 的数据规范
278-
*
279-
* @example
280-
* [
281-
* { value: 0, label: 'Sunday' },
282-
* { value: 1, label: 'Monday' },
283-
* ];
284-
*
285-
* @param config Custom options | 自定义选项
286-
*
287-
* @see https://ant.design/components/checkbox-cn#option
288-
*/
289-
toList<FK, FV>(
290-
config: ToListConfig & ObjectFirstOptionConfig<FK, FV>
291-
): EnumItemOptionData<K | (FK extends never ? FV : FK), V | (FV extends never ? V : FV)>[];
268+
toList<
269+
OV extends string | ((item: EnumItemClass<T[K], K, V>) => string) = string,
270+
OL extends string | ((item: EnumItemClass<T[K], K, V>) => string) = string,
271+
>(
272+
config: ToListConfig<T, OL, OV, K, V>
273+
): EnumListItem<
274+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
275+
OL extends (...args: any) => any ? ReturnType<OL> : OL,
276+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
277+
OV extends (...args: any) => any ? ReturnType<OV> : OV,
278+
V
279+
>[];
292280

293281
/**
294282
* **EN:** Generate an object array that can be bound to the data source of components such as
@@ -454,15 +442,13 @@ export interface LabelOnlyEnumItemInit {
454442
}
455443
export type CompactEnumItemInit = Record<string, never>; // 等价于{}
456444

457-
/** Data structure of ant-design Select options */
458-
export interface EnumItemOptionData<K, V> {
459-
/** Option value */
460-
value: V;
461-
/** Option label */
462-
label: string;
463-
/** Option key, default is `value` */
464-
key: K;
465-
}
445+
/**
446+
* **EN:** Data structure of enumeration item options, used in `toList` method
447+
*
448+
* **CN:** 枚举项选项的数据结构,用于`toList`方法
449+
*/
450+
export type EnumListItem<FL extends string = string, FV extends string = string, V = EnumValue> = Record<FV, V> &
451+
Record<FL, string>;
466452

467453
/** Data structure of column filter items of ant-design Table */
468454
export interface ColumnFilterItem<V> {
@@ -492,6 +478,8 @@ export type EnumKey<T> = keyof T;
492478
/** More options for the options method */
493479
export interface ToListConfig<
494480
T extends EnumInit<K, V>,
481+
OV extends string | ((item: EnumItemClass<T[K], K, V>) => string) = string,
482+
OL extends string | ((item: EnumItemClass<T[K], K, V>) => string) = string,
495483
K extends EnumKey<T> = EnumKey<T>,
496484
V extends EnumValue = ValueTypeFromSingleInit<T[K], K>,
497485
> {
@@ -501,73 +489,16 @@ export interface ToListConfig<
501489
*
502490
* **CN:** 输出对象的value字段名,或者获取字段名的函数,默认为 `value`
503491
*/
504-
valueField?: string | ((item: EnumItemClass<T[K], K, V>[]) => string);
492+
valueField?: OV;
505493
/**
506494
* **EN:** The name of the label field in the output object, or a function to get the field name,
507495
* default is `label`
508496
*
509497
* **CN:** 输出对象的label字段名,或者获取字段名的函数,默认为 `label`
510498
*/
511-
labelField?: string | ((item: EnumItemClass<T[K], K, V>[]) => string);
512-
}
513-
514-
export interface BooleanFirstOptionConfig<V> {
515-
/**
516-
* **EN:** Add a default option at the top
517-
*
518-
* - `true`: the option uses the default value, `value` is `''`, `label` is `'All'`;
519-
* - `false`: the default option is not added;
520-
*
521-
* **CN:** 在头部添加一个默认选项
522-
*
523-
* - `true`:选项使用默认值,`value`为`''`,`label`为`'全部'`
524-
* - `false`:不添加默认选项
525-
*
526-
* @default false
527-
*/
528-
firstOption: boolean;
529-
/**
530-
* **EN:** Default option value, default is `''`
531-
*
532-
* **CN:** 默认选项的值,默认为`''`
533-
*
534-
* @default ''
535-
*/
536-
firstOptionValue?: V;
537-
/**
538-
* **EN:** Default option label, default is `'All'`. If a localization method is set, the
539-
* localization method will be automatically called
540-
*
541-
* **CN:** 默认选项的显示文本,默认为`'All'`。如果设置了本地化方法,则会自动调用本地化方法
542-
*/
543-
firstOptionLabel?: string;
499+
labelField?: OL;
544500
}
545501

546-
export interface ObjectFirstOptionConfig<K, V> {
547-
/**
548-
* **EN:** Configuration of the first option
549-
*
550-
* **CN:** 首行选项的配置
551-
*/
552-
firstOption?: EnumOptionConfig<K, V>;
553-
/**
554-
* **EN:** Add a default option at the top
555-
*
556-
* **CN:** 默认选项的值,默认为`''`
557-
*/
558-
firstOptionValue?: never;
559-
/**
560-
* **EN:** Default option label, default is `'All'`. If a localization method is set, the
561-
* localization method will be automatically called
562-
*
563-
* **CN:** 默认选项的显示文本,默认为`'All'`。如果设置了本地化方法,则会自动调用本地化方法
564-
*/
565-
firstOptionLabel?: never;
566-
}
567-
568-
export type EnumOptionConfig<K, V> = Omit<EnumItemOptionData<K, V>, 'key'> &
569-
Partial<Pick<EnumItemOptionData<K, V>, 'key'>>;
570-
571502
/** Infer the value type from the initialization object of the enumeration item */
572503
export type ValueTypeFromSingleInit<T, Key = string, Fallback = Key> = T extends EnumValue // literal类型
573504
? T

0 commit comments

Comments
 (0)