-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat(form): password field component * feat: visibilty icon in password input --------- Co-authored-by: mrbadri <mrbadri.dev@gmail.com>
- Loading branch information
1 parent
1bebcad
commit 03b1e98
Showing
11 changed files
with
263 additions
and
4 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
13 changes: 13 additions & 0 deletions
13
packages/core/src/modules/form/src/components/fields/password/password.loading.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,13 @@ | ||
import { Skeleton } from '@mui/material'; | ||
|
||
import { LoadingProps } from '@mui-builder/types/configs.type'; | ||
|
||
import usePasswordLoading from './usePassword.loading'; | ||
|
||
const PasswordLoading = (props: LoadingProps) => { | ||
const { getTextLoadingProps } = usePasswordLoading(props); | ||
|
||
return <Skeleton {...getTextLoadingProps()} />; | ||
}; | ||
|
||
export default PasswordLoading; |
38 changes: 38 additions & 0 deletions
38
packages/core/src/modules/form/src/components/fields/password/password.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,38 @@ | ||
import { FC } from 'react'; | ||
|
||
import { Visibility, VisibilityOff } from '@mui/icons-material'; | ||
import { IconButton, InputAdornment, TextField } from '@mui/material'; | ||
|
||
import { PasswordProps } from './password.types'; | ||
|
||
import usePassword from './usePassword'; | ||
|
||
const Password: FC<PasswordProps> = (props) => { | ||
const { | ||
getFieldProps, | ||
show, | ||
getInputAdornmentProps, | ||
getIconButtonProps, | ||
showPassword, | ||
} = usePassword(props); | ||
|
||
if (show) | ||
return ( | ||
<TextField | ||
{...getFieldProps()} | ||
InputProps={{ | ||
endAdornment: ( | ||
<InputAdornment {...getInputAdornmentProps()}> | ||
<IconButton {...getIconButtonProps()}> | ||
{showPassword ? <VisibilityOff /> : <Visibility />} | ||
</IconButton> | ||
</InputAdornment> | ||
), | ||
}} | ||
/> | ||
); | ||
|
||
return null; | ||
}; | ||
|
||
export default Password; |
18 changes: 18 additions & 0 deletions
18
packages/core/src/modules/form/src/components/fields/password/password.types.ts
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,18 @@ | ||
import { TextFieldProps } from '@mui/material'; | ||
|
||
import { Api } from '@mui-builder/types/api.types'; | ||
import { Script } from '@mui-builder/types/script.types'; | ||
|
||
import { Dependesies, FormId, Id } from '../../../types/public.types'; | ||
import { Rule } from '../../../types/validation.types'; | ||
|
||
export type PasswordProps = TextFieldProps & { | ||
id: Id; | ||
formId: FormId; | ||
script?: Script; | ||
dependesies?: Dependesies; | ||
propsController?: Record<string, any>; | ||
api?: Api; | ||
rule?: Rule; | ||
show?: boolean; | ||
}; |
22 changes: 22 additions & 0 deletions
22
packages/core/src/modules/form/src/components/fields/password/usePassword.loading.ts
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,22 @@ | ||
import { LoadingProps } from '@mui-builder/types/configs.type'; | ||
|
||
const usePasswordLoading = (props: LoadingProps) => { | ||
const { animation = 'wave', sx, ...otherProps } = props; | ||
|
||
const getPasswordLoadingProps = () => ({ | ||
sx: { | ||
display: 'inline-block', | ||
transform: 'unset', | ||
width: '255px', | ||
height: '56px', | ||
mx: 0.5, | ||
...sx, | ||
}, | ||
animation, | ||
...otherProps, | ||
}); | ||
|
||
return { getTextLoadingProps: getPasswordLoadingProps }; | ||
}; | ||
|
||
export default usePasswordLoading; |
107 changes: 107 additions & 0 deletions
107
packages/core/src/modules/form/src/components/fields/password/usePassword.ts
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,107 @@ | ||
import { useState } from 'react'; | ||
import { useController, useWatch } from 'react-hook-form'; | ||
|
||
import { IconButtonProps, InputAdornmentProps } from '@mui/material'; | ||
|
||
import useQueryBuilder from '@mui-builder/utils/useQueryBuilder/useQueryBuilder'; | ||
import UseScript from '@mui-builder/utils/useScript/useScript'; | ||
|
||
import axios from 'axios'; | ||
|
||
import { PasswordProps } from './password.types'; | ||
|
||
import useForms from '../../../hooks/useForms/useForms'; | ||
import usePropsController from '../../../hooks/usePropsController/usePropsController'; | ||
import useRule from '../../../hooks/useRule/useRule'; | ||
|
||
const UsePassword = (props: PasswordProps) => { | ||
const { | ||
formId, | ||
script, | ||
api, | ||
show = true, | ||
dependesies, | ||
helperText, | ||
defaultValue, | ||
...textFieldProps | ||
} = props; | ||
const { configs, queries } = api || {}; | ||
|
||
const { forms } = useForms(); | ||
const formMethod = forms?.[formId]; | ||
const { setProps, propsController } = usePropsController(); | ||
|
||
const newProps = propsController?.[textFieldProps?.id] || {}; | ||
|
||
// eye icon | ||
const [showPassword, setShowPassword] = useState(false); | ||
|
||
const handleClickShowPassword = () => { | ||
setShowPassword(!showPassword); | ||
}; | ||
|
||
// Handle Script | ||
const { scriptResult } = UseScript({ | ||
script, | ||
formMethod, | ||
forms, | ||
formId, | ||
setProps, | ||
}); | ||
|
||
// Handle Wtach Fields | ||
useWatch({ | ||
control: formMethod.control, | ||
name: dependesies ?? [], | ||
}); | ||
|
||
// API Call | ||
useQueryBuilder({ | ||
apiInstance: axios, | ||
apiConfigs: configs ?? {}, | ||
apiQuery: queries ?? {}, | ||
formMethod, | ||
formId, | ||
forms, | ||
}); | ||
|
||
// Controller | ||
const { | ||
field, | ||
formState: { errors }, | ||
} = useController({ | ||
name: textFieldProps.id, | ||
control: formMethod.control, | ||
disabled: textFieldProps.disabled, | ||
rules: useRule(textFieldProps?.rule), | ||
defaultValue, | ||
}); | ||
|
||
const error = errors?.[textFieldProps.id]; | ||
|
||
// Props | ||
const getFieldProps = () => ({ | ||
...field, | ||
...textFieldProps, | ||
helperText: error?.message ?? helperText, | ||
error: !!error, | ||
value: field.value ?? '', | ||
...scriptResult, | ||
...newProps, | ||
type: showPassword ? 'text' : 'password', | ||
}); | ||
|
||
const getInputAdornmentProps = (): InputAdornmentProps => ({ | ||
position: 'end', | ||
}); | ||
|
||
const getIconButtonProps = (): IconButtonProps => ({ | ||
'aria-label': 'toggle password visibility', | ||
onClick: handleClickShowPassword, | ||
edge: 'end', | ||
}); | ||
|
||
return { getFieldProps, show, getInputAdornmentProps, getIconButtonProps, showPassword }; | ||
}; | ||
|
||
export default UsePassword; |
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