Files
Homarr/src/widgets/widgets.ts

107 lines
2.5 KiB
TypeScript
Raw Normal View History

2023-01-05 22:43:27 +09:00
import { TablerIcon } from '@tabler/icons';
2022-12-18 21:21:23 +01:00
import React from 'react';
import { AreaType } from '../types/area';
import { ShapeType } from '../types/shape';
2022-12-18 21:21:23 +01:00
// Type of widgets which are saved to config
2022-12-18 21:21:23 +01:00
export type IWidget<TKey extends string, TDefinition extends IWidgetDefinition> = {
id: TKey;
properties: {
[key in keyof TDefinition['options']]: MakeLessSpecific<
TDefinition['options'][key]['defaultValue']
>;
};
area: AreaType;
shape: ShapeType;
};
// Makes the type less specific
// For example when the type true is used as input the result is boolean
// By not using this type the definition would always be { property: true }
type MakeLessSpecific<T> = T extends boolean ? boolean : T;
2022-12-18 21:21:23 +01:00
// Types of options that can be specified for the widget edit modal
export type IWidgetOptionValue =
| IMultiSelectOptionValue
| ISwitchOptionValue
| ITextInputOptionValue
2023-01-05 22:43:27 +09:00
| ISliderInputOptionValue
2023-01-11 08:43:28 +09:00
| ISelectOptionValue
| INumberInputOptionValue
| IDraggableListInputValue;
2022-12-18 21:21:23 +01:00
2023-01-11 08:43:28 +09:00
// Interface for data type
interface DataType {
2023-01-13 09:49:29 +00:00
label: string;
value: string;
2023-01-11 08:43:28 +09:00
}
2022-12-18 21:21:23 +01:00
// will show a multi-select with specified data
export type IMultiSelectOptionValue = {
type: 'multi-select';
defaultValue: string[];
2023-01-11 08:43:28 +09:00
data: DataType[];
};
// will show a multi-select with specified data
export type ISelectOptionValue = {
type: 'select';
defaultValue: string;
data: DataType[];
2022-12-18 21:21:23 +01:00
};
// will show a switch
export type ISwitchOptionValue = {
type: 'switch';
defaultValue: boolean;
};
// will show a text-input
export type ITextInputOptionValue = {
type: 'text';
defaultValue: string;
};
// will show a number-input
export type INumberInputOptionValue = {
type: 'number';
2023-01-05 22:43:27 +09:00
defaultValue: number;
};
// will show a slider-input
export type ISliderInputOptionValue = {
type: 'slider';
defaultValue: number;
min: number;
max: number;
step: number;
2022-12-18 21:21:23 +01:00
};
export type IDraggableListInputValue = {
type: 'draggable-list';
defaultValue: {
key: string;
subValues?: Record<string, any>;
}[];
items: Record<
string,
Record<string, Omit<Exclude<IWidgetOptionValue, IDraggableListInputValue>, 'defaultValue'>>
>;
};
2022-12-18 21:21:23 +01:00
// is used to type the widget definitions which will be used to display all widgets
export type IWidgetDefinition<TKey extends string = string> = {
id: TKey;
icon: TablerIcon | string;
options: {
[key: string]: IWidgetOptionValue;
};
gridstack: {
minWidth: number;
minHeight: number;
maxWidth: number;
maxHeight: number;
};
2022-12-18 21:21:23 +01:00
component: React.ComponentType<any>;
};