Skip to content

Commit 0aa5e34

Browse files
committed
docs: update README for custom label logic support
1 parent 658ab94 commit 0aa5e34

File tree

3 files changed

+59
-9
lines changed

3 files changed

+59
-9
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
### Features
1818

19-
- ✨ Enhance enum creation to support function labels and enum name.
19+
- ✨ Enhance enum creation to support `function` labels and enum name.
2020

2121
### Bug Fixes
2222

README.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ WeekEnum.items[0].label; // I love Sunday
184184
```
185185

186186
> Want to enable code completion when entering `label`? Please refer to the [Enable Code Intelligence for Enum Item Labels](#-enable-code-intelligence-for-enum-item-labels) section for more details.
187+
> Want to customize the logic of the `label` field? You can pass in a function. Please refer to the [Custom Label Logic](#custom-label-logic) section for more details.
187188
188189
### 3. Label-Only Format
189190

@@ -1135,6 +1136,30 @@ Enum.localize = (key) => {
11351136

11361137
> Once you have completed this feature, it is recommended that you consider publishing it as an npm package and share it in the [Awesome Plugins](#awesome-plugins) section, so that others can benefit from your work. If you believe that this project is very general, you can also consider submitting it to the official [enum-plus](https://github.com/shijistar/enum-plus/tree/master/packages) plugin repository. For specific development rules, please refer to the [Plugin Development Guide](./docs/plugin-development.md).
11371138
1139+
### Custom Label Logic
1140+
1141+
Of course, if you are not using any internationalization framework but want to control the localization rules of enum `label` yourself, or use different custom logic for each enum item, you can pass a function to the `label` field:
1142+
1143+
```js
1144+
const WeekEnum = Enum({
1145+
Sunday: { value: 0, label: () => 'Sunday' },
1146+
Monday: { value: 1, label: () => 'Monday' },
1147+
});
1148+
```
1149+
1150+
Additionally, `enum.name` also supports using a custom function.
1151+
1152+
```js
1153+
const WeekEnum = Enum(
1154+
{
1155+
//...
1156+
},
1157+
{
1158+
name: () => 'Week',
1159+
}
1160+
);
1161+
```
1162+
11381163
---
11391164

11401165
## Extensibility

README.zh-CN.md

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ WeekEnum.items[0].label; // 星期日
182182
```
183183

184184
> 想要输入 `label` 时启用代码智能提示?请参考 [启用枚举项标签的智能提示](#-启用枚举项标签的智能提示) 章节,了解更多详情。
185+
> 希望自定义 `label` 字段逻辑?可以传入一个函数,请参考 [自定义 label 逻辑](#自定义-label-逻辑) 章节,了解更多详情。
185186
186187
### 3. Key-Label 格式
187188

@@ -640,7 +641,7 @@ Enum.install(i18nextPlugin);
640641

641642
> 请注意,在创建枚举时也可以通过 `options.autoLabel` 参数覆盖全局配置,其用法与 `Enum.config.autoLabel` 相同。
642643
643-
## 应用案例
644+
## 使用案例
644645

645646
### 💡 基础用法,消除魔法数字
646647

@@ -1122,6 +1123,30 @@ Enum.localize = (key) => {
11221123

11231124
> 一旦你完成了这项功能,建议你考虑把它发布成一个 npm 包,并分享在[插件生态](#插件生态)章节中,这样其他人也可以受益于你的工作。如果你觉得这个项目非常通用,也可以考虑把它提交到 [enum-plus](https://github.com/shijistar/enum-plus/tree/master/packages) 官方插件库中,具体开发规则请参阅 [插件开发指南](./docs/plugin-development.zh-CN.md)
11241125
1126+
### 自定义 label 逻辑
1127+
1128+
当然,如果不使用任何国际化框架,而是希望自己控制枚举 `label` 的本地化规则,或者为每个枚举项使用不同的自定义逻辑,你可以为 `label` 字段传入一个函数:
1129+
1130+
```js
1131+
const WeekEnum = Enum({
1132+
Sunday: { value: 0, label: () => '星期日' },
1133+
Monday: { value: 1, label: () => '星期一' },
1134+
});
1135+
```
1136+
1137+
另外,enum\.name 也支持使用自定义函数。
1138+
1139+
````js
1140+
const WeekEnum = Enum(
1141+
{
1142+
//...
1143+
},
1144+
{
1145+
name: () => '',
1146+
}
1147+
);
1148+
```
1149+
11251150
---
11261151

11271152
## 全局扩展
@@ -1152,17 +1177,17 @@ Enum 提供了丰富的内置方法和属性,它们已经可以满足大多数
11521177
reversedItems: () => EnumItemClass<EnumItemInit<V>, K, V>[];
11531178
}
11541179
}
1155-
```
1180+
````
11561181
1157-
_index.ts_
1182+
_index.ts_
11581183
1159-
然后在项目的入口文件中导入这个文件:
1184+
然后在项目的入口文件中导入这个文件:
11601185
1161-
```ts
1162-
import './my-enum-extension';
1186+
```ts
1187+
import './my-enum-extension';
11631188

1164-
WeekEnum.toMySelect(); // [{ value: 0, title: '星期日' }, { value: 1, title: '星期一' }]
1165-
```
1189+
WeekEnum.toMySelect(); // [{ value: 0, title: '星期日' }, { value: 1, title: '星期一' }]
1190+
```
11661191

11671192
- **JavaScript 项目**
11681193

0 commit comments

Comments
 (0)