blob: 02aef259dee9768386b95d739d9c8d49e9f25bf3 [file] [log] [blame]
idillon3470d072022-11-22 15:22:34 -05001/*
2 * Copyright (C) 2022 Savoir-faire Linux Inc.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Affero General Public License as
6 * published by the Free Software Foundation; either version 3 of the
7 * License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU Affero General Public License for more details.
13 *
14 * You should have received a copy of the GNU Affero General Public
15 * License along with this program. If not, see
16 * <https://www.gnu.org/licenses/>.
17 */
18import { Button, FormLabel, List, ListItem, ListSubheader, Stack, Switch, SwitchProps } from '@mui/material';
19import { Box } from '@mui/system';
20import { MouseEventHandler, ReactElement } from 'react';
21
22import { CustomSelect, CustomSelectProps } from './CustomSelect';
23
24interface Setting {
25 label: string;
26}
27
28interface SettingBaseProps {
29 label: string;
30 children: ReactElement;
31}
32
33const SettingBase = ({ label, children }: SettingBaseProps) => {
34 return (
35 <ListItem sx={{ padding: 0, margin: 0 }}>
36 <FormLabel sx={{ width: '100%' }}>
37 <Stack direction="row">
38 <Box flexGrow={1}>{label}</Box>
39 {children}
40 </Stack>
41 </FormLabel>
42 </ListItem>
43 );
44};
45
46interface SettingButtonProps extends Setting {
47 onClick: MouseEventHandler;
48}
49
50export const SettingButton = ({ onClick, ...settingBaseProps }: SettingButtonProps) => {
51 return (
52 <SettingBase {...settingBaseProps}>
53 <Button variant="contained" size="small" onClick={onClick} />
54 </SettingBase>
55 );
56};
57
idillon4012a702022-11-24 13:45:43 -050058export interface SettingSelectProps<OptionPayload> extends Setting, CustomSelectProps<OptionPayload> {}
idillon3470d072022-11-22 15:22:34 -050059
idillon4012a702022-11-24 13:45:43 -050060export const SettingSelect = <OptionPayload,>({
61 options,
62 option,
63 onChange,
64 ...settingBaseProps
65}: SettingSelectProps<OptionPayload>) => {
idillon3470d072022-11-22 15:22:34 -050066 return (
67 <SettingBase {...settingBaseProps}>
idillon4012a702022-11-24 13:45:43 -050068 <CustomSelect<OptionPayload> option={option} options={options} onChange={onChange} size="small" />
idillon3470d072022-11-22 15:22:34 -050069 </SettingBase>
70 );
71};
72
73interface SettingSwitchProps extends Setting {
74 checked: SwitchProps['checked'];
75 onChange: SwitchProps['onChange'];
76}
77
78export const SettingSwitch = ({ checked, onChange, ...settingBaseProps }: SettingSwitchProps) => {
79 return (
80 <SettingBase {...settingBaseProps}>
81 <Switch checked={checked} onChange={onChange} />
82 </SettingBase>
83 );
84};
85
86interface SettingGroupProps {
87 label: string;
idillona3c2fad2022-12-18 23:49:10 -050088 children: ReactElement[] | ReactElement;
idillon3470d072022-11-22 15:22:34 -050089}
90
91export const SettingsGroup = ({ label, children }: SettingGroupProps) => {
92 return <List subheader={<ListSubheader>{label}</ListSubheader>}>{children}</List>;
93};