From 82fcb1e08e1439e6f62054670a46bb03885cb04d Mon Sep 17 00:00:00 2001 From: Tsu-ba-me Date: Fri, 5 Aug 2022 17:38:59 -0400 Subject: [PATCH] fix(striker-ui): add onPasswordVisibilityAppend to OutlinedInput --- .../OutlinedInput/OutlinedInput.tsx | 56 +++++++++++-------- striker-ui/lib/consts/INPUT_TYPES.ts | 11 ++++ 2 files changed, 44 insertions(+), 23 deletions(-) create mode 100644 striker-ui/lib/consts/INPUT_TYPES.ts diff --git a/striker-ui/components/OutlinedInput/OutlinedInput.tsx b/striker-ui/components/OutlinedInput/OutlinedInput.tsx index 301bdb4a..a2986f65 100644 --- a/striker-ui/components/OutlinedInput/OutlinedInput.tsx +++ b/striker-ui/components/OutlinedInput/OutlinedInput.tsx @@ -4,6 +4,7 @@ import { } from '@mui/icons-material'; import { IconButton as MUIIconButton, + IconButtonProps as MUIIconButtonProps, OutlinedInput as MUIOutlinedInput, outlinedInputClasses as muiOutlinedInputClasses, OutlinedInputProps as MUIOutlinedInputProps, @@ -11,21 +12,29 @@ import { import { cloneElement, FC, ReactElement, useMemo, useState } from 'react'; import { GREY, TEXT, UNSELECTED } from '../../lib/consts/DEFAULT_THEME'; +import INPUT_TYPES from '../../lib/consts/INPUT_TYPES'; -type OutlinedInputProps = MUIOutlinedInputProps; +type OutlinedInputOptionalProps = { + onPasswordVisibilityAppend?: ( + inputType: string, + ...restArgs: Parameters> + ) => void; +}; + +type OutlinedInputProps = MUIOutlinedInputProps & OutlinedInputOptionalProps; -const INPUT_TYPES: Record< - Exclude, - string +const OUTLINED_INPUT_DEFAULT_PROPS: Pick< + OutlinedInputOptionalProps, + 'onPasswordVisibilityAppend' > = { - password: 'password', - text: 'text', + onPasswordVisibilityAppend: undefined, }; const OutlinedInput: FC = (outlinedInputProps) => { const { endAdornment, label, + onPasswordVisibilityAppend, sx, inputProps: { type: baseType, ...inputRestProps } = {}, ...outlinedInputRestProps @@ -33,30 +42,29 @@ const OutlinedInput: FC = (outlinedInputProps) => { const [type, setType] = useState(baseType); - const additionalEndAdornment = useMemo( - () => ( + const additionalEndAdornment = useMemo(() => { + const isBaseTypePassword = baseType === INPUT_TYPES.password; + const isTypePassword = type === INPUT_TYPES.password; + + return ( <> - {baseType === INPUT_TYPES.password && ( + {isBaseTypePassword && ( { - setType((previous) => - previous === INPUT_TYPES.password - ? INPUT_TYPES.text - : INPUT_TYPES.password, - ); + onClick={(...args) => { + const newType = isTypePassword + ? INPUT_TYPES.text + : INPUT_TYPES.password; + + setType(newType); + onPasswordVisibilityAppend?.call(null, newType, ...args); }} > - {type === INPUT_TYPES.password ? ( - - ) : ( - - )} + {isTypePassword ? : } )} - ), - [baseType, type], - ); + ); + }, [baseType, onPasswordVisibilityAppend, type]); const combinedSx = useMemo( () => ({ color: GREY, @@ -126,6 +134,8 @@ const OutlinedInput: FC = (outlinedInputProps) => { ); }; +OutlinedInput.defaultProps = OUTLINED_INPUT_DEFAULT_PROPS; + export type { OutlinedInputProps }; export default OutlinedInput; diff --git a/striker-ui/lib/consts/INPUT_TYPES.ts b/striker-ui/lib/consts/INPUT_TYPES.ts new file mode 100644 index 00000000..6b21e71d --- /dev/null +++ b/striker-ui/lib/consts/INPUT_TYPES.ts @@ -0,0 +1,11 @@ +import { OutlinedInputProps as MUIOutlinedInputProps } from '@mui/material'; + +const INPUT_TYPES: Record< + Exclude, + string +> = { + password: 'password', + text: 'text', +}; + +export default INPUT_TYPES;