-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
146 additions
and
69 deletions.
There are no files selected for viewing
23 changes: 23 additions & 0 deletions
23
govtool/frontend/src/components/atoms/FormErrorMessage.tsx
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,23 @@ | ||
import { Typography } from "@mui/material"; | ||
|
||
import { FormErrorMessageProps } from "./types"; | ||
|
||
export const FormErrorMessage = ({ | ||
errorMessage, | ||
errorStyles, | ||
}: FormErrorMessageProps) => { | ||
return ( | ||
errorMessage && ( | ||
<Typography | ||
color="red" | ||
data-testid={`${errorMessage.replace(/\s+/g, "-").toLowerCase()}-error`} | ||
fontSize={12} | ||
fontWeight={400} | ||
sx={{ mt: 0.25 }} | ||
{...errorStyles} | ||
> | ||
{errorMessage} | ||
</Typography> | ||
) | ||
); | ||
}; |
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 |
---|---|---|
@@ -1,30 +1,56 @@ | ||
import { useId } from "react"; | ||
import { | ||
forwardRef, | ||
useCallback, | ||
useId, | ||
useImperativeHandle, | ||
useRef, | ||
} from "react"; | ||
import { InputBase } from "@mui/material"; | ||
|
||
import { InputProps } from "./types"; | ||
|
||
export const Input = ({ | ||
errorMessage, | ||
dataTestId, | ||
sx, | ||
...rest | ||
}: InputProps) => { | ||
const id = useId(); | ||
export const Input = forwardRef<HTMLInputElement, InputProps>( | ||
({ errorMessage, dataTestId, onBlur, onFocus, sx, ...rest }, ref) => { | ||
const id = useId(); | ||
const inputRef = useRef<HTMLInputElement>(null); | ||
|
||
return ( | ||
<InputBase | ||
id={id} | ||
inputProps={{ "data-testid": dataTestId }} | ||
sx={{ | ||
backgroundColor: errorMessage ? "inputRed" : "transparent", | ||
border: 1, | ||
borderColor: errorMessage ? "red" : "secondaryBlue", | ||
borderRadius: 50, | ||
padding: "8px 16px", | ||
width: "100%", | ||
...sx, | ||
}} | ||
{...rest} | ||
/> | ||
); | ||
}; | ||
const handleFocus = useCallback((e: React.FocusEvent<HTMLInputElement>) => { | ||
onFocus?.(e); | ||
inputRef.current?.focus(); | ||
}, []); | ||
|
||
const handleBlur = useCallback((e: React.FocusEvent<HTMLInputElement>) => { | ||
onBlur?.(e); | ||
inputRef.current?.blur(); | ||
}, []); | ||
|
||
useImperativeHandle( | ||
ref, | ||
() => | ||
({ | ||
focus: handleFocus, | ||
blur: handleBlur, | ||
...inputRef.current, | ||
} as unknown as HTMLInputElement), | ||
[handleBlur, handleFocus] | ||
); | ||
|
||
return ( | ||
<InputBase | ||
id={id} | ||
inputRef={inputRef} | ||
inputProps={{ "data-testid": dataTestId }} | ||
sx={{ | ||
backgroundColor: errorMessage ? "inputRed" : "transparent", | ||
border: 1, | ||
borderColor: errorMessage ? "red" : "secondaryBlue", | ||
borderRadius: 50, | ||
padding: "8px 16px", | ||
width: "100%", | ||
...sx, | ||
}} | ||
{...rest} | ||
/> | ||
); | ||
} | ||
); |
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
88 changes: 55 additions & 33 deletions
88
govtool/frontend/src/components/molecules/Field/Input.tsx
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 |
---|---|---|
@@ -1,38 +1,60 @@ | ||
import { forwardRef, useCallback, useImperativeHandle, useRef } from "react"; | ||
import { Box } from "@mui/material"; | ||
|
||
import { Input as InputBase, Typography } from "@atoms"; | ||
import { FormErrorMessage, Input as InputBase, Typography } from "@atoms"; | ||
|
||
import { InputFieldProps } from "./types"; | ||
|
||
export const Input = ({ | ||
errorMessage, | ||
errorStyle, | ||
label, | ||
labelStyle, | ||
layoutStyle, | ||
...rest | ||
}: InputFieldProps) => { | ||
return ( | ||
<Box sx={{ width: "100%", ...layoutStyle }}> | ||
{label && ( | ||
<Typography fontWeight={400} variant="body2" {...labelStyle}> | ||
{label} | ||
</Typography> | ||
)} | ||
<InputBase errorMessage={errorMessage} {...rest} /> | ||
{errorMessage && ( | ||
<Typography | ||
variant="caption" | ||
color="red" | ||
data-testid={`${errorMessage | ||
.replace(/\s+/g, "-") | ||
.toLowerCase()}-error`} | ||
sx={{ mt: 0.25 }} | ||
{...errorStyle} | ||
> | ||
{errorMessage} | ||
</Typography> | ||
)} | ||
</Box> | ||
); | ||
}; | ||
export const Input = forwardRef<HTMLInputElement, InputFieldProps>( | ||
( | ||
{ | ||
errorMessage, | ||
errorStyles, | ||
label, | ||
labelStyles, | ||
layoutStyles, | ||
onBlur, | ||
onFocus, | ||
...rest | ||
}, | ||
ref | ||
) => { | ||
const inputRef = useRef<HTMLInputElement>(null); | ||
|
||
const handleFocus = useCallback((e: React.FocusEvent<HTMLInputElement>) => { | ||
onFocus?.(e); | ||
inputRef.current?.focus(); | ||
}, []); | ||
|
||
const handleBlur = useCallback((e: React.FocusEvent<HTMLInputElement>) => { | ||
onBlur?.(e); | ||
inputRef.current?.blur(); | ||
}, []); | ||
|
||
useImperativeHandle( | ||
ref, | ||
() => | ||
({ | ||
focus: handleFocus, | ||
blur: handleBlur, | ||
...inputRef.current, | ||
} as unknown as HTMLInputElement), | ||
[handleBlur, handleFocus] | ||
); | ||
|
||
return ( | ||
<Box sx={{ width: "100%", ...layoutStyles }}> | ||
{label && ( | ||
<Typography fontWeight={400} variant="body2" {...labelStyles}> | ||
{label} | ||
</Typography> | ||
)} | ||
<InputBase errorMessage={errorMessage} ref={inputRef} {...rest} /> | ||
<FormErrorMessage | ||
errorMessage={errorMessage} | ||
errorStyles={errorStyles} | ||
/> | ||
</Box> | ||
); | ||
} | ||
); |
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 |
---|---|---|
@@ -1,11 +1,11 @@ | ||
import { BoxProps } from "@mui/material"; | ||
import { BoxProps, TypographyProps as MUITypographyProps } from "@mui/material"; | ||
|
||
import { InputProps, TypographyProps } from "@atoms"; | ||
|
||
export type InputFieldProps = InputProps & { | ||
errorMessage?: string; | ||
errorStyle?: TypographyProps; | ||
errorStyles?: MUITypographyProps; | ||
label?: string; | ||
labelStyle?: TypographyProps; | ||
layoutStyle?: BoxProps; | ||
labelStyles?: TypographyProps; | ||
layoutStyles?: BoxProps; | ||
}; |
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