-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PIMS-2036: Map Filter - ERP Projects (#2733)
- Loading branch information
1 parent
6c9077c
commit 62411d0
Showing
5 changed files
with
63 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import React from 'react'; | ||
import { SxProps, Typography, Box, useTheme, Switch } from '@mui/material'; | ||
import { Controller, useFormContext } from 'react-hook-form'; | ||
|
||
interface ToggleSwitchProps { | ||
name: string; | ||
label: string; | ||
sx?: SxProps; | ||
required?: boolean; | ||
} | ||
|
||
const ToggleSwitch = (props: ToggleSwitchProps) => { | ||
const { control } = useFormContext(); | ||
const theme = useTheme(); | ||
const { name, label, required } = props; | ||
return ( | ||
<Controller | ||
name={name} | ||
control={control} | ||
rules={{ validate: (value) => !required || value || 'Required field.' }} | ||
render={({ field: { onChange, value, onBlur }, fieldState: { error } }) => ( | ||
<> | ||
<Box | ||
sx={{ | ||
display: 'flex', | ||
alignItems: 'center', | ||
}} | ||
> | ||
<Switch | ||
id={`single-checkbox-${name}`} | ||
onChange={(_, data) => onChange(data)} | ||
onBlur={onBlur} | ||
checked={!!value} | ||
required={required} | ||
/> | ||
<Typography> | ||
{label} {required ? <sup>{'*'}</sup> : <></>} | ||
</Typography> | ||
</Box> | ||
{!!error && !!error.message ? ( | ||
<Box> | ||
<Typography ml={'3.2em'} fontSize={'smaller'} color={theme.palette.error.main}> | ||
{error.message} | ||
</Typography> | ||
</Box> | ||
) : ( | ||
<></> | ||
)} | ||
</> | ||
)} | ||
/> | ||
); | ||
}; | ||
|
||
export default ToggleSwitch; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters