ruoyi-plus-soybean/src/utils/common.ts

59 lines
1.1 KiB
TypeScript
Raw Normal View History

2024-01-28 00:44:21 +08:00
import { $t } from '@/locales';
2023-11-17 08:45:00 +08:00
/**
2023-12-14 21:45:29 +08:00
* Transform record to option
*
2023-11-17 08:45:00 +08:00
* @example
2023-12-14 21:45:29 +08:00
* ```ts
* const record = {
* key1: 'label1',
* key2: 'label2'
* };
* const options = transformRecordToOption(record);
* // [
* // { value: 'key1', label: 'label1' },
* // { value: 'key2', label: 'label2' }
* // ]
* ```;
*
* @param record
2023-11-17 08:45:00 +08:00
*/
export function transformRecordToOption<T extends Record<string, string>>(record: T) {
return Object.entries(record).map(([value, label]) => ({
value,
label
2024-01-28 00:44:21 +08:00
})) as CommonType.Option<keyof T>[];
}
/**
* Translate options
*
* @param options
*/
export function translateOptions(options: CommonType.Option<string>[]) {
return options.map(option => ({
...option,
label: $t(option.label as App.I18n.I18nKey)
}));
2023-11-17 08:45:00 +08:00
}
/**
* Toggle html class
*
* @param className
*/
export function toggleHtmlClass(className: string) {
function add() {
document.documentElement.classList.add(className);
}
function remove() {
document.documentElement.classList.remove(className);
}
return {
add,
remove
};
}