Skip to content

Commit 4bf2fe4

Browse files
committed
feat: implement toList method
1 parent 2463801 commit 4bf2fe4

File tree

6 files changed

+195
-89
lines changed

6 files changed

+195
-89
lines changed

.github/ISSUE_TEMPLATE/config.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
blank_issues_enabled: false
1+
blank_issues_enabled: true

src/enum-values.ts

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { Enum } from './enum';
21
import type { EnumItemClass } from './enum-item';
32
import type {
43
ColumnFilterItem,
@@ -38,7 +37,6 @@ export class EnumItemsArray<
3837
implements IEnumItems<T, K, V>
3938
{
4039
private _raw: T;
41-
private _options: EnumItemOptions | undefined;
4240
/**
4341
* **EN:** A boolean value indicates that this is an enum items array.
4442
*
@@ -57,9 +55,7 @@ export class EnumItemsArray<
5755
constructor(raw: T, options: EnumItemOptions | undefined, ...items: EnumItemClass<T[K], K, V>[]) {
5856
super(...items);
5957
this._raw = raw;
60-
this._options = options;
6158
}
62-
name?: string | undefined;
6359

6460
label<KV extends V | K | NonNullable<PrimitiveOf<V>> | NonNullable<PrimitiveOf<K>> | undefined>(
6561
keyOrValue: KV
@@ -159,7 +155,10 @@ export class EnumItemsArray<
159155
const labelFieldName = typeof labelField === 'function' ? labelField(item) : (labelField as string);
160156
return {
161157
[valueFieldName]: item.value,
162-
[labelFieldName]: item.label,
158+
// should use getter to preserve the localized label, as it may be dynamic content
159+
get [labelFieldName]() {
160+
return item.label;
161+
},
163162
} as ListItem<
164163
V,
165164
FOV extends (item: EnumItemClass<T[K], K, V>) => infer R ? R : FOV,
@@ -203,11 +202,4 @@ export class EnumItemsArray<
203202
private _runtimeError(name: string) {
204203
return `The ${name} property of the enumeration is only allowed to be used to declare the ts type, and cannot be accessed at runtime! Please use the typeof operator in the ts type, for example: typeof Week.${name}`;
205204
}
206-
_localize(content: string | undefined) {
207-
const localize = this._options?.localize ?? Enum.localize;
208-
if (typeof localize === 'function') {
209-
return localize(content);
210-
}
211-
return content;
212-
}
213205
}

src/utils.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ export const ENUM_ITEMS = Symbol.for('[EnumItems]');
5252
*/
5353
export const defaultLocalize: NonNullable<EnumItemOptions['localize']> = (content) => {
5454
// todo: 移除该资源
55-
if (content === 'enum-plus.options.all') {
56-
return 'All';
57-
}
55+
// if (content === 'enum-plus.options.all') {
56+
// return 'All';
57+
// }
5858
return content;
5959
};

test/test-suites/enum-values.ts

Lines changed: 76 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { defaultLocalize, ENUM_ITEMS as NODE_ENUM_ITEMS } from '@enum-plus';
22
import { getLocales, localizeConfigData, StandardWeekConfig } from '../data/week-config';
33
import { getStandardWeekData } from '../data/week-data';
44
import type TestEngineBase from '../engines/base';
5-
import { getOptionsData, pickArray } from '../utils';
5+
import { copyList, pickArray } from '../utils';
66

77
const testEnumValues = (engine: TestEngineBase) => {
88
engine.describe('The EnumItemsArray api', () => {
@@ -55,19 +55,14 @@ export function addEnumValuesTestSuite(engine: TestEngineBase) {
5555
);
5656

5757
engine.test(
58-
'enums.toList should generate an object array for AntDesign Select',
58+
'enums.toList should generate an object array',
5959
({ EnumPlus: { Enum }, WeekConfig: { StandardWeekConfig, locales } }) => {
6060
const weekEnum = Enum(StandardWeekConfig);
61-
return { weekEnum, locales };
61+
const defaultListItems = weekEnum.toList();
62+
return { locales, defaultListItems };
6263
},
63-
({ weekEnum, locales }) => {
64-
engine
65-
.expect(getOptionsData(weekEnum.toList()))
66-
.toEqual(pickArray(getStandardWeekData(locales), ['label', 'value']));
67-
// todo: 测试自定义字段名
68-
engine
69-
.expect(getOptionsData(weekEnum.toList({})))
70-
.toEqual(pickArray(getStandardWeekData(locales), ['label', 'value']));
64+
({ locales, defaultListItems }) => {
65+
engine.expect(copyList(defaultListItems)).toEqual(pickArray(getStandardWeekData(locales), ['value', 'label']));
7166

7267
// Add first-option by boolean flag
7368
// const withDefaultFirstOption = weekEnum.toList({ firstOption: true });
@@ -106,6 +101,76 @@ export function addEnumValuesTestSuite(engine: TestEngineBase) {
106101
}
107102
);
108103

104+
engine.test(
105+
'enums.toList should generate an object array with custom field names',
106+
({ EnumPlus: { Enum }, WeekConfig: { StandardWeekConfig, locales } }) => {
107+
const weekEnum = Enum(StandardWeekConfig);
108+
const idNameListItems = weekEnum.toList({
109+
valueField: 'id',
110+
labelField: 'name',
111+
});
112+
const valueNameListItems = weekEnum.toList({
113+
valueField: 'value',
114+
labelField: 'name',
115+
});
116+
const idLabelListItems = weekEnum.toList({
117+
valueField: 'id',
118+
labelField: 'label',
119+
});
120+
return { locales, idNameListItems, valueNameListItems, idLabelListItems };
121+
},
122+
({ locales, idNameListItems, valueNameListItems, idLabelListItems }) => {
123+
engine.expect(Array.from(idNameListItems)).toEqual(
124+
getStandardWeekData(locales).map((item) => ({
125+
id: item.value,
126+
name: item.label,
127+
}))
128+
);
129+
engine.expect(Array.from(valueNameListItems)).toEqual(
130+
getStandardWeekData(locales).map((item) => ({
131+
value: item.value,
132+
name: item.label,
133+
}))
134+
);
135+
engine.expect(Array.from(idLabelListItems)).toEqual(
136+
getStandardWeekData(locales).map((item) => ({
137+
id: item.value,
138+
label: item.label,
139+
}))
140+
);
141+
}
142+
);
143+
144+
engine.test(
145+
'enums.toList should generate an object array with custom field names in function style',
146+
({ EnumPlus: { Enum }, WeekConfig: { StandardWeekConfig, locales } }) => {
147+
const weekEnum = Enum(StandardWeekConfig);
148+
const staticFieldListItems = weekEnum.toList({
149+
valueField: () => 'id' as const,
150+
labelField: () => 'name' as const,
151+
});
152+
const dynamicFieldListItems = weekEnum.toList({
153+
valueField: (item) => `id-${item.value}`,
154+
labelField: (item) => `name-${item.value}`,
155+
});
156+
return { locales, staticFieldListItems, dynamicFieldListItems };
157+
},
158+
({ locales, staticFieldListItems, dynamicFieldListItems }) => {
159+
engine.expect(Array.from(staticFieldListItems)).toEqual(
160+
getStandardWeekData(locales).map((item) => ({
161+
id: item.value,
162+
name: item.label,
163+
}))
164+
);
165+
engine.expect(Array.from(dynamicFieldListItems)).toEqual(
166+
getStandardWeekData(locales).map((item) => ({
167+
['id-' + item.value]: item.value,
168+
['name-' + item.value]: item.label,
169+
}))
170+
);
171+
}
172+
);
173+
109174
engine.test(
110175
'enums.toValueMap should generate an object array for AntDesignPro',
111176
({ EnumPlus: { Enum }, WeekConfig: { StandardWeekConfig } }) => {

0 commit comments

Comments
 (0)