blob: 81333de1c122b4b9ea76bb9a3a77b8de5d9c426e [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
58interface SettingSelectProps extends Setting, CustomSelectProps {}
59
60export const SettingSelect = ({ options, value, onChange, ...settingBaseProps }: SettingSelectProps) => {
61 return (
62 <SettingBase {...settingBaseProps}>
63 <CustomSelect value={value} options={options} onChange={onChange} />
64 </SettingBase>
65 );
66};
67
68interface SettingSwitchProps extends Setting {
69 checked: SwitchProps['checked'];
70 onChange: SwitchProps['onChange'];
71}
72
73export const SettingSwitch = ({ checked, onChange, ...settingBaseProps }: SettingSwitchProps) => {
74 return (
75 <SettingBase {...settingBaseProps}>
76 <Switch checked={checked} onChange={onChange} />
77 </SettingBase>
78 );
79};
80
81interface SettingGroupProps {
82 label: string;
83 children: ReactElement[];
84}
85
86export const SettingsGroup = ({ label, children }: SettingGroupProps) => {
87 return <List subheader={<ListSubheader>{label}</ListSubheader>}>{children}</List>;
88};