idillon | 3470d07 | 2022-11-22 15:22:34 -0500 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 18 | import { Button, FormLabel, List, ListItem, ListSubheader, Stack, Switch, SwitchProps } from '@mui/material'; |
| 19 | import { Box } from '@mui/system'; |
| 20 | import { MouseEventHandler, ReactElement } from 'react'; |
| 21 | |
| 22 | import { CustomSelect, CustomSelectProps } from './CustomSelect'; |
| 23 | |
| 24 | interface Setting { |
| 25 | label: string; |
| 26 | } |
| 27 | |
| 28 | interface SettingBaseProps { |
| 29 | label: string; |
| 30 | children: ReactElement; |
| 31 | } |
| 32 | |
| 33 | const 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 | |
| 46 | interface SettingButtonProps extends Setting { |
| 47 | onClick: MouseEventHandler; |
| 48 | } |
| 49 | |
| 50 | export const SettingButton = ({ onClick, ...settingBaseProps }: SettingButtonProps) => { |
| 51 | return ( |
| 52 | <SettingBase {...settingBaseProps}> |
| 53 | <Button variant="contained" size="small" onClick={onClick} /> |
| 54 | </SettingBase> |
| 55 | ); |
| 56 | }; |
| 57 | |
| 58 | interface SettingSelectProps extends Setting, CustomSelectProps {} |
| 59 | |
| 60 | export 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 | |
| 68 | interface SettingSwitchProps extends Setting { |
| 69 | checked: SwitchProps['checked']; |
| 70 | onChange: SwitchProps['onChange']; |
| 71 | } |
| 72 | |
| 73 | export const SettingSwitch = ({ checked, onChange, ...settingBaseProps }: SettingSwitchProps) => { |
| 74 | return ( |
| 75 | <SettingBase {...settingBaseProps}> |
| 76 | <Switch checked={checked} onChange={onChange} /> |
| 77 | </SettingBase> |
| 78 | ); |
| 79 | }; |
| 80 | |
| 81 | interface SettingGroupProps { |
| 82 | label: string; |
| 83 | children: ReactElement[]; |
| 84 | } |
| 85 | |
| 86 | export const SettingsGroup = ({ label, children }: SettingGroupProps) => { |
| 87 | return <List subheader={<ListSubheader>{label}</ListSubheader>}>{children}</List>; |
| 88 | }; |