fix(striker-ui): add onPasswordVisibilityAppend to OutlinedInput

main
Tsu-ba-me 2 years ago
parent de73fe47c9
commit 82fcb1e08e
  1. 52
      striker-ui/components/OutlinedInput/OutlinedInput.tsx
  2. 11
      striker-ui/lib/consts/INPUT_TYPES.ts

@ -4,6 +4,7 @@ import {
} from '@mui/icons-material'; } from '@mui/icons-material';
import { import {
IconButton as MUIIconButton, IconButton as MUIIconButton,
IconButtonProps as MUIIconButtonProps,
OutlinedInput as MUIOutlinedInput, OutlinedInput as MUIOutlinedInput,
outlinedInputClasses as muiOutlinedInputClasses, outlinedInputClasses as muiOutlinedInputClasses,
OutlinedInputProps as MUIOutlinedInputProps, OutlinedInputProps as MUIOutlinedInputProps,
@ -11,21 +12,29 @@ import {
import { cloneElement, FC, ReactElement, useMemo, useState } from 'react'; import { cloneElement, FC, ReactElement, useMemo, useState } from 'react';
import { GREY, TEXT, UNSELECTED } from '../../lib/consts/DEFAULT_THEME'; 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<Exclude<MUIIconButtonProps['onClick'], undefined>>
) => void;
};
type OutlinedInputProps = MUIOutlinedInputProps & OutlinedInputOptionalProps;
const INPUT_TYPES: Record< const OUTLINED_INPUT_DEFAULT_PROPS: Pick<
Exclude<MUIOutlinedInputProps['type'], undefined>, OutlinedInputOptionalProps,
string 'onPasswordVisibilityAppend'
> = { > = {
password: 'password', onPasswordVisibilityAppend: undefined,
text: 'text',
}; };
const OutlinedInput: FC<OutlinedInputProps> = (outlinedInputProps) => { const OutlinedInput: FC<OutlinedInputProps> = (outlinedInputProps) => {
const { const {
endAdornment, endAdornment,
label, label,
onPasswordVisibilityAppend,
sx, sx,
inputProps: { type: baseType, ...inputRestProps } = {}, inputProps: { type: baseType, ...inputRestProps } = {},
...outlinedInputRestProps ...outlinedInputRestProps
@ -33,30 +42,29 @@ const OutlinedInput: FC<OutlinedInputProps> = (outlinedInputProps) => {
const [type, setType] = useState<string>(baseType); const [type, setType] = useState<string>(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 && (
<MUIIconButton <MUIIconButton
onClick={() => { onClick={(...args) => {
setType((previous) => const newType = isTypePassword
previous === INPUT_TYPES.password
? INPUT_TYPES.text ? INPUT_TYPES.text
: INPUT_TYPES.password, : INPUT_TYPES.password;
);
setType(newType);
onPasswordVisibilityAppend?.call(null, newType, ...args);
}} }}
> >
{type === INPUT_TYPES.password ? ( {isTypePassword ? <MUIVisibilityIcon /> : <MUIVisibilityOffIcon />}
<MUIVisibilityIcon />
) : (
<MUIVisibilityOffIcon />
)}
</MUIIconButton> </MUIIconButton>
)} )}
</> </>
),
[baseType, type],
); );
}, [baseType, onPasswordVisibilityAppend, type]);
const combinedSx = useMemo( const combinedSx = useMemo(
() => ({ () => ({
color: GREY, color: GREY,
@ -126,6 +134,8 @@ const OutlinedInput: FC<OutlinedInputProps> = (outlinedInputProps) => {
); );
}; };
OutlinedInput.defaultProps = OUTLINED_INPUT_DEFAULT_PROPS;
export type { OutlinedInputProps }; export type { OutlinedInputProps };
export default OutlinedInput; export default OutlinedInput;

@ -0,0 +1,11 @@
import { OutlinedInputProps as MUIOutlinedInputProps } from '@mui/material';
const INPUT_TYPES: Record<
Exclude<MUIOutlinedInputProps['type'], undefined>,
string
> = {
password: 'password',
text: 'text',
};
export default INPUT_TYPES;
Loading…
Cancel
Save