bazarr/frontend/src/pages/Settings/components/forms.tsx

196 lines
4.6 KiB
TypeScript
Raw Normal View History

import {
2022-05-31 15:49:04 +00:00
Action as GlobalAction,
FileBrowser,
FileBrowserProps,
2022-05-31 15:49:04 +00:00
MultiSelector as GlobalMultiSelector,
MultiSelectorProps as GlobalMultiSelectorProps,
Selector as GlobalSelector,
SelectorProps as GlobalSelectorProps,
} from "@/components";
2022-05-31 15:49:04 +00:00
import { ActionProps as GlobalActionProps } from "@/components/inputs/Action";
import ChipInput, { ChipInputProps } from "@/components/inputs/ChipInput";
import { useSliderMarks } from "@/utilities";
2021-03-25 14:22:43 +00:00
import {
2022-05-31 15:49:04 +00:00
InputWrapper,
NumberInput,
NumberInputProps,
PasswordInput,
PasswordInputProps,
Slider as MantineSlider,
SliderProps as MantineSliderProps,
Switch,
TextInput,
TextInputProps,
} from "@mantine/core";
import { FunctionComponent, ReactText } from "react";
import { BaseInput, useBaseInput } from "../utilities/hooks";
2021-03-25 14:22:43 +00:00
2022-05-31 15:49:04 +00:00
export type NumberProps = BaseInput<number> & NumberInputProps;
export const Number: FunctionComponent<NumberProps> = (props) => {
const { value, update, rest } = useBaseInput(props);
2022-05-31 15:49:04 +00:00
return (
<NumberInput
{...rest}
2022-05-31 15:49:04 +00:00
value={value ?? undefined}
onChange={(val = 0) => {
update(val);
2022-05-31 15:49:04 +00:00
}}
></NumberInput>
);
};
export type TextProps = BaseInput<ReactText> & TextInputProps;
2021-03-25 14:22:43 +00:00
export const Text: FunctionComponent<TextProps> = (props) => {
const { value, update, rest } = useBaseInput(props);
2021-03-25 14:22:43 +00:00
return (
2022-05-31 15:49:04 +00:00
<TextInput
{...rest}
2022-05-31 15:49:04 +00:00
value={value ?? undefined}
2021-03-25 14:22:43 +00:00
onChange={(e) => {
update(e.currentTarget.value);
2022-05-31 15:49:04 +00:00
}}
></TextInput>
);
};
export type PasswordProps = BaseInput<string> & PasswordInputProps;
export const Password: FunctionComponent<PasswordProps> = (props) => {
const { value, update, rest } = useBaseInput(props);
2022-05-31 15:49:04 +00:00
return (
<PasswordInput
{...rest}
2022-05-31 15:49:04 +00:00
value={value ?? undefined}
onChange={(e) => {
update(e.currentTarget.value);
2021-03-25 14:22:43 +00:00
}}
2022-05-31 15:49:04 +00:00
></PasswordInput>
2021-03-25 14:22:43 +00:00
);
};
export interface CheckProps extends BaseInput<boolean> {
label?: string;
inline?: boolean;
}
export const Check: FunctionComponent<CheckProps> = ({
label,
inline,
...props
2021-03-25 14:22:43 +00:00
}) => {
const { value, update, rest } = useBaseInput(props);
2021-03-25 14:22:43 +00:00
return (
2022-05-31 15:49:04 +00:00
<Switch
2021-03-25 14:22:43 +00:00
label={label}
onChange={(e) => {
update(e.currentTarget.checked);
2021-03-25 14:22:43 +00:00
}}
disabled={rest.disabled}
2022-05-31 15:49:04 +00:00
checked={value ?? false}
></Switch>
2021-03-25 14:22:43 +00:00
);
};
2022-05-31 15:49:04 +00:00
export type SelectorProps<T extends string | number> = BaseInput<T> &
GlobalSelectorProps<T>;
2021-03-25 14:22:43 +00:00
2022-05-31 15:49:04 +00:00
export function Selector<T extends string | number>(props: SelectorProps<T>) {
const { value, update, rest } = useBaseInput(props);
2021-03-25 14:22:43 +00:00
2022-05-31 15:49:04 +00:00
return (
<GlobalSelector {...rest} value={value} onChange={update}></GlobalSelector>
2021-03-25 14:22:43 +00:00
);
2022-05-31 15:49:04 +00:00
}
2021-03-25 14:22:43 +00:00
2022-05-31 15:49:04 +00:00
export type MultiSelectorProps<T extends string | number> = BaseInput<T[]> &
GlobalMultiSelectorProps<T>;
export function MultiSelector<T extends string | number>(
props: MultiSelectorProps<T>
) {
const { value, update, rest } = useBaseInput(props);
2021-03-25 14:22:43 +00:00
return (
2022-05-31 15:49:04 +00:00
<GlobalMultiSelector
{...rest}
2022-05-31 15:49:04 +00:00
value={value ?? []}
onChange={update}
2022-05-31 15:49:04 +00:00
></GlobalMultiSelector>
2021-03-25 14:22:43 +00:00
);
}
type SliderProps = BaseInput<number> &
2022-05-31 15:49:04 +00:00
Omit<MantineSliderProps, "onChange" | "onChangeEnd" | "marks">;
2021-03-25 14:22:43 +00:00
export const Slider: FunctionComponent<SliderProps> = (props) => {
const { value, update, rest } = useBaseInput(props);
2022-07-04 12:59:40 +00:00
const { label, ...sliderProps } = rest;
2021-03-25 14:22:43 +00:00
const { min = 0, max = 100 } = props;
2021-03-25 14:22:43 +00:00
const marks = useSliderMarks([min, max]);
2021-03-25 14:22:43 +00:00
return (
2022-07-04 12:59:40 +00:00
<InputWrapper label={label}>
2022-05-31 15:49:04 +00:00
<MantineSlider
2022-07-04 12:59:40 +00:00
{...sliderProps}
2022-05-31 15:49:04 +00:00
marks={marks}
onChange={update}
2022-05-31 15:49:04 +00:00
value={value ?? 0}
></MantineSlider>
</InputWrapper>
2021-03-25 14:22:43 +00:00
);
};
type ChipsProp = BaseInput<string[]> &
2022-05-31 15:49:04 +00:00
Omit<ChipInputProps, "onChange" | "data">;
2021-03-25 14:22:43 +00:00
export const Chips: FunctionComponent<ChipsProp> = (props) => {
const { value, update, rest } = useBaseInput(props);
2021-03-25 14:22:43 +00:00
return (
<ChipInput {...rest} value={value ?? []} onChange={update}></ChipInput>
2021-03-25 14:22:43 +00:00
);
};
2022-05-31 15:49:04 +00:00
type ActionProps = {
onClick?: (update: (v: string) => void, value?: string) => void;
} & Omit<BaseInput<string>, "modification">;
2021-03-25 14:22:43 +00:00
2022-05-31 15:49:04 +00:00
export const Action: FunctionComponent<
Override<ActionProps, GlobalActionProps>
> = (props) => {
const { value, update, rest } = useBaseInput(props);
2021-03-25 14:22:43 +00:00
return (
2022-05-31 15:49:04 +00:00
<GlobalAction
{...rest}
2021-03-25 14:22:43 +00:00
onClick={() => {
props.onClick?.(update, (value as string) ?? undefined);
2021-03-25 14:22:43 +00:00
}}
2022-05-31 15:49:04 +00:00
></GlobalAction>
2021-03-25 14:22:43 +00:00
);
};
interface FileProps extends BaseInput<string> {}
export const File: FunctionComponent<Override<FileProps, FileBrowserProps>> = (
props
) => {
const { value, update, rest } = useBaseInput(props);
return (
<FileBrowser
{...rest}
defaultValue={value ?? undefined}
onChange={update}
></FileBrowser>
);
};