add NumberInput, Slider to widget edit modal

This commit is contained in:
ajnart
2023-01-05 22:43:56 +09:00
parent a43780719e
commit f14552f664

View File

@@ -1,4 +1,15 @@
import { Alert, Button, Group, MultiSelect, Stack, Switch, TextInput, Text } from '@mantine/core'; import {
Alert,
Button,
Group,
MultiSelect,
Stack,
Switch,
TextInput,
Text,
NumberInput,
Slider,
} from '@mantine/core';
import { ContextModalProps } from '@mantine/modals'; import { ContextModalProps } from '@mantine/modals';
import { IconAlertTriangle } from '@tabler/icons'; import { IconAlertTriangle } from '@tabler/icons';
import { Trans, useTranslation } from 'next-i18next'; import { Trans, useTranslation } from 'next-i18next';
@@ -8,6 +19,7 @@ import type { IWidgetOptionValue } from '../../../../widgets/widgets';
import { useConfigContext } from '../../../../config/provider'; import { useConfigContext } from '../../../../config/provider';
import { useConfigStore } from '../../../../config/store'; import { useConfigStore } from '../../../../config/store';
import { IWidget } from '../../../../widgets/widgets'; import { IWidget } from '../../../../widgets/widgets';
import { useColorTheme } from '../../../../tools/color';
export type WidgetEditModalInnerProps = { export type WidgetEditModalInnerProps = {
widgetId: string; widgetId: string;
@@ -83,7 +95,38 @@ export const WidgetsEditModal = ({
</Alert> </Alert>
); );
} }
return WidgetOptionTypeSwitch(
option,
index,
t,
key,
value,
handleChange,
getMutliselectData
);
})}
<Group position="right">
<Button onClick={() => context.closeModal(id)} variant="light">
{t('common:cancel')}
</Button>
<Button onClick={handleSave}>{t('common:save')}</Button>
</Group>
</Stack>
);
};
// Widget options are computed based on their type.
// here you can define new types for options (along with editing the widgets.d.ts file)
function WidgetOptionTypeSwitch(
option: IWidgetOptionValue,
index: number,
t: any,
key: string,
value: string | number | boolean | string[],
handleChange: (key: string, value: IntegrationOptionsValueType) => void,
getMutliselectData: (option: string) => any
) {
const { primaryColor, secondaryColor } = useColorTheme();
switch (option.type) { switch (option.type) {
case 'switch': case 'switch':
return ( return (
@@ -97,6 +140,7 @@ export const WidgetsEditModal = ({
case 'text': case 'text':
return ( return (
<TextInput <TextInput
color={primaryColor}
key={`${option.type}-${index}`} key={`${option.type}-${index}`}
label={t(`descriptor.settings.${key}.label`)} label={t(`descriptor.settings.${key}.label`)}
value={value as string} value={value as string}
@@ -106,6 +150,7 @@ export const WidgetsEditModal = ({
case 'multi-select': case 'multi-select':
return ( return (
<MultiSelect <MultiSelect
color={primaryColor}
key={`${option.type}-${index}`} key={`${option.type}-${index}`}
data={getMutliselectData(key)} data={getMutliselectData(key)}
label={t(`descriptor.settings.${key}.label`)} label={t(`descriptor.settings.${key}.label`)}
@@ -113,16 +158,30 @@ export const WidgetsEditModal = ({
onChange={(v) => handleChange(key, v)} onChange={(v) => handleChange(key, v)}
/> />
); );
case 'number':
return (
<NumberInput
color={primaryColor}
key={`${option.type}-${index}`}
label={t(`descriptor.settings.${key}.label`)}
value={value as number}
onChange={(v) => handleChange(key, v!)}
/>
);
case 'slider':
return (
<Slider
color={primaryColor}
key={`${option.type}-${index}`}
label={t(`descriptor.settings.${key}.label`)}
value={value as number}
min={option.min}
max={option.max}
step={option.step}
onChange={(v) => handleChange(key, v)}
/>
);
default: default:
return null; return null;
} }
})} }
<Group position="right">
<Button onClick={() => context.closeModal(id)} variant="light">
{t('common:cancel')}
</Button>
<Button onClick={handleSave}>{t('common:save')}</Button>
</Group>
</Stack>
);
};