Files
mayfly-go/mayfly_go_web/src/common/Enum.ts

95 lines
1.9 KiB
TypeScript
Raw Normal View History

export interface EnumValueTag {
color?: string;
type?: string;
}
/**
*
*/
export class EnumValue {
/**
*
*/
value: any;
/**
*
*/
label: string;
/**
*
*/
tag: EnumValueTag;
constructor(value: any, label: string) {
this.value = value;
this.label = label;
}
setTagType(type: string = 'primary'): EnumValue {
this.tag = { type };
return this;
}
tagTypeInfo(): EnumValue {
return this.setTagType('info');
}
tagTypeSuccess(): EnumValue {
return this.setTagType('success');
}
tagTypeDanger(): EnumValue {
return this.setTagType('danger');
}
tagTypeWarning(): EnumValue {
return this.setTagType('warning');
}
setTagColor(color: string): EnumValue {
this.tag = { color };
return this;
}
public static of(value: any, label: string): EnumValue {
return new EnumValue(value, label);
}
/**
*
*
* @param enumValues
* @param value
* @returns
*/
static getEnumByValue(enumValues: EnumValue[], value: any): EnumValue | null {
for (let enumValue of enumValues) {
if (enumValue.value == value) {
return enumValue;
}
}
return null;
}
/**
*
*
* @param enums
* @param value
* @returns
*/
static getLabelByValue(enums: any, value: any) {
const enumValues = Object.values(enums) as any;
for (let enumValue of enumValues) {
if (enumValue['value'] == value) {
return enumValue['label'];
}
}
return '';
}
}
export default EnumValue;