simon | 26e79f7 | 2022-10-05 22:16:08 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2022 Savoir-faire Linux Inc. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU Affero General Public License as |
| 6 | * published by the Free Software Foundation; either version 3 of the |
| 7 | * License, or (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU Affero General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU Affero General Public |
| 15 | * License along with this program. If not, see |
| 16 | * <https://www.gnu.org/licenses/>. |
| 17 | */ |
Michelle Sepkap Sime | f5ebc2e | 2022-10-27 18:30:53 -0400 | [diff] [blame] | 18 | import { GppMaybe, Warning } from '@mui/icons-material'; |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 19 | import { IconButtonProps, Stack, TextField, TextFieldProps } from '@mui/material'; |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 20 | import { styled } from '@mui/material/styles'; |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 21 | import { ChangeEvent, ReactElement, useCallback, useEffect, useMemo, useState } from 'react'; |
Michelle Sepkap Sime | 559cc80 | 2022-11-05 12:06:40 -0400 | [diff] [blame] | 22 | import { useTranslation } from 'react-i18next'; |
simon | 07b4eb0 | 2022-09-29 17:50:26 -0400 | [diff] [blame] | 23 | |
simon | ab4eec8 | 2022-11-08 20:32:43 -0500 | [diff] [blame] | 24 | import { StrengthValueCode } from '../utils/auth'; |
simon | 3537869 | 2022-10-02 23:25:57 -0400 | [diff] [blame] | 25 | import { InfoButton, ToggleVisibilityButton } from './Button'; |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 26 | import { DialogContentList, InfosDialog, useDialogHandler } from './Dialog'; |
simon | 3537869 | 2022-10-02 23:25:57 -0400 | [diff] [blame] | 27 | import { CheckedIcon, LockIcon, PenIcon, PersonIcon, RoundSaltireIcon } from './SvgIcon'; |
idillon-sfl | 37c18df | 2022-08-26 18:44:27 -0400 | [diff] [blame] | 28 | |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 29 | const iconsHeight = '16px'; |
| 30 | const StyledCheckedIconSuccess = styled(CheckedIcon)(({ theme }) => ({ |
| 31 | height: iconsHeight, |
| 32 | color: theme.palette.success.main, |
| 33 | })); |
| 34 | const StyledRoundSaltireIconError = styled(RoundSaltireIcon)(({ theme }) => ({ |
| 35 | height: iconsHeight, |
| 36 | color: theme.palette.error.main, |
| 37 | })); |
| 38 | const StyledPenIconLight = styled(PenIcon)({ height: iconsHeight, color: '#03B9E9' }); |
| 39 | const StyledPenIconDark = styled(PenIcon)(({ theme }) => ({ height: iconsHeight, color: theme.palette.primary.dark })); |
| 40 | const StyledPersonIconLight = styled(PersonIcon)({ height: iconsHeight, color: '#03B9E9' }); |
| 41 | const StyledLockIcon = styled(LockIcon)({ height: iconsHeight, color: '#03B9E9' }); |
idillon-sfl | 37c18df | 2022-08-26 18:44:27 -0400 | [diff] [blame] | 42 | |
simon | ab4eec8 | 2022-11-08 20:32:43 -0500 | [diff] [blame] | 43 | export type NameStatus = 'default' | 'success' | 'taken' | 'invalid' | 'registration_failed'; |
| 44 | export type PasswordStatus = StrengthValueCode | 'registration_failed'; |
| 45 | |
| 46 | export type InputProps<StatusType extends NameStatus | PasswordStatus> = TextFieldProps & { |
simon | 4e7445c | 2022-11-16 21:18:46 -0500 | [diff] [blame] | 47 | status?: StatusType; |
simon | 3537869 | 2022-10-02 23:25:57 -0400 | [diff] [blame] | 48 | infoButtonProps?: IconButtonProps; |
| 49 | success?: boolean; |
Michelle Sepkap Sime | f5ebc2e | 2022-10-27 18:30:53 -0400 | [diff] [blame] | 50 | tooltipTitle: string; |
simon | 3537869 | 2022-10-02 23:25:57 -0400 | [diff] [blame] | 51 | }; |
| 52 | |
Michelle Sepkap Sime | e580f42 | 2022-10-31 23:27:04 -0400 | [diff] [blame] | 53 | export const UsernameInput = ({ |
| 54 | infoButtonProps, |
| 55 | onChange: _onChange, |
| 56 | success, |
simon | 4e7445c | 2022-11-16 21:18:46 -0500 | [diff] [blame] | 57 | status = 'default', |
Michelle Sepkap Sime | e580f42 | 2022-10-31 23:27:04 -0400 | [diff] [blame] | 58 | tooltipTitle, |
| 59 | ...props |
simon | ab4eec8 | 2022-11-08 20:32:43 -0500 | [diff] [blame] | 60 | }: InputProps<NameStatus>) => { |
Michelle Sepkap Sime | 559cc80 | 2022-11-05 12:06:40 -0400 | [diff] [blame] | 61 | const { t } = useTranslation(); |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 62 | const [isSelected, setIsSelected] = useState(false); |
| 63 | const [input, setInput] = useState(props.defaultValue); |
simon | 3537869 | 2022-10-02 23:25:57 -0400 | [diff] [blame] | 64 | const [startAdornment, setStartAdornment] = useState<ReactElement | undefined>(); |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 65 | const dialogHandler = useDialogHandler(); |
idillon-sfl | 37c18df | 2022-08-26 18:44:27 -0400 | [diff] [blame] | 66 | |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 67 | const onChange = useCallback( |
simon | 3537869 | 2022-10-02 23:25:57 -0400 | [diff] [blame] | 68 | (event: ChangeEvent<HTMLInputElement>) => { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 69 | setInput(event.target.value); |
simon | 80b7b3b | 2022-09-28 17:50:10 -0400 | [diff] [blame] | 70 | _onChange?.(event); |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 71 | }, |
simon | 80b7b3b | 2022-09-28 17:50:10 -0400 | [diff] [blame] | 72 | [_onChange] |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 73 | ); |
idillon-sfl | 37c18df | 2022-08-26 18:44:27 -0400 | [diff] [blame] | 74 | |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 75 | useEffect(() => { |
| 76 | /* Handle startAdornment */ |
| 77 | let Icon = StyledPersonIconLight; |
| 78 | let visibility = 'visible'; |
| 79 | if (props.error) { |
| 80 | Icon = StyledRoundSaltireIconError; |
Michelle Sepkap Sime | e580f42 | 2022-10-31 23:27:04 -0400 | [diff] [blame] | 81 | } else if (success) { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 82 | Icon = StyledCheckedIconSuccess; |
| 83 | } else if (!isSelected && !input) { |
| 84 | visibility = 'hidden'; // keep icon's space so text does not move |
idillon-sfl | 37c18df | 2022-08-26 18:44:27 -0400 | [diff] [blame] | 85 | } |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 86 | setStartAdornment(<Icon sx={{ visibility }} />); |
Michelle Sepkap Sime | e580f42 | 2022-10-31 23:27:04 -0400 | [diff] [blame] | 87 | }, [props.error, success, isSelected, input]); |
idillon-sfl | 37c18df | 2022-08-26 18:44:27 -0400 | [diff] [blame] | 88 | |
simon | ab4eec8 | 2022-11-08 20:32:43 -0500 | [diff] [blame] | 89 | /* |
| 90 | t('username_input_helper_text_success') |
| 91 | t('username_input_helper_text_taken') |
| 92 | t('username_input_helper_text_invalid') |
| 93 | t('username_input_helper_text_registration_failed') |
| 94 | */ |
| 95 | const helperText = t('username_input_helper_text', { context: `${status}` }); |
| 96 | |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 97 | return ( |
Michelle Sepkap Sime | f5ebc2e | 2022-10-27 18:30:53 -0400 | [diff] [blame] | 98 | <> |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 99 | <InfosDialog {...dialogHandler.props} title={t('username_rules_dialog_title')} content={<UsernameRules />} /> |
Michelle Sepkap Sime | f5ebc2e | 2022-10-27 18:30:53 -0400 | [diff] [blame] | 100 | <TextField |
Michelle Sepkap Sime | e580f42 | 2022-10-31 23:27:04 -0400 | [diff] [blame] | 101 | color={inputColor(props.error, success)} |
Michelle Sepkap Sime | 559cc80 | 2022-11-05 12:06:40 -0400 | [diff] [blame] | 102 | label={t('username_input_label')} |
Michelle Sepkap Sime | f5ebc2e | 2022-10-27 18:30:53 -0400 | [diff] [blame] | 103 | variant="standard" |
simon | ab4eec8 | 2022-11-08 20:32:43 -0500 | [diff] [blame] | 104 | helperText={status !== 'default' ? helperText : ''} |
Michelle Sepkap Sime | f5ebc2e | 2022-10-27 18:30:53 -0400 | [diff] [blame] | 105 | onChange={onChange} |
simon | ab4eec8 | 2022-11-08 20:32:43 -0500 | [diff] [blame] | 106 | onFocus={() => setIsSelected(true)} |
| 107 | onBlur={() => setIsSelected(false)} |
| 108 | {...props} |
| 109 | InputLabelProps={{ |
| 110 | shrink: !!(isSelected || input), |
| 111 | ...props.InputLabelProps, |
| 112 | }} |
Michelle Sepkap Sime | f5ebc2e | 2022-10-27 18:30:53 -0400 | [diff] [blame] | 113 | InputProps={{ |
| 114 | startAdornment, |
| 115 | endAdornment: ( |
simon | 841c6bd | 2022-11-24 00:53:40 -0500 | [diff] [blame] | 116 | <InfoButton |
| 117 | tabIndex={-1} |
| 118 | tooltipTitle={tooltipTitle} |
| 119 | {...infoButtonProps} |
| 120 | onClick={dialogHandler.openDialog} |
| 121 | /> |
Michelle Sepkap Sime | f5ebc2e | 2022-10-27 18:30:53 -0400 | [diff] [blame] | 122 | ), |
simon | ab4eec8 | 2022-11-08 20:32:43 -0500 | [diff] [blame] | 123 | ...props.InputProps, |
Michelle Sepkap Sime | f5ebc2e | 2022-10-27 18:30:53 -0400 | [diff] [blame] | 124 | }} |
Michelle Sepkap Sime | f5ebc2e | 2022-10-27 18:30:53 -0400 | [diff] [blame] | 125 | /> |
| 126 | </> |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 127 | ); |
| 128 | }; |
idillon-sfl | 37c18df | 2022-08-26 18:44:27 -0400 | [diff] [blame] | 129 | |
Michelle Sepkap Sime | e580f42 | 2022-10-31 23:27:04 -0400 | [diff] [blame] | 130 | export const PasswordInput = ({ |
| 131 | infoButtonProps, |
| 132 | onChange: _onChange, |
| 133 | success, |
| 134 | tooltipTitle, |
simon | 4e7445c | 2022-11-16 21:18:46 -0500 | [diff] [blame] | 135 | status = 'default', |
Michelle Sepkap Sime | e580f42 | 2022-10-31 23:27:04 -0400 | [diff] [blame] | 136 | ...props |
simon | ab4eec8 | 2022-11-08 20:32:43 -0500 | [diff] [blame] | 137 | }: InputProps<PasswordStatus>) => { |
Michelle Sepkap Sime | 559cc80 | 2022-11-05 12:06:40 -0400 | [diff] [blame] | 138 | const { t } = useTranslation(); |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 139 | const [showPassword, setShowPassword] = useState(false); |
| 140 | const [isSelected, setIsSelected] = useState(false); |
| 141 | const [input, setInput] = useState(props.defaultValue); |
simon | 3537869 | 2022-10-02 23:25:57 -0400 | [diff] [blame] | 142 | const [startAdornment, setStartAdornment] = useState<ReactElement | undefined>(); |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 143 | const dialogHandler = useDialogHandler(); |
idillon-sfl | 37c18df | 2022-08-26 18:44:27 -0400 | [diff] [blame] | 144 | |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 145 | const toggleShowPassword = () => { |
| 146 | setShowPassword((showPassword) => !showPassword); |
| 147 | }; |
| 148 | |
| 149 | const onChange = useCallback( |
simon | 3537869 | 2022-10-02 23:25:57 -0400 | [diff] [blame] | 150 | (event: ChangeEvent<HTMLInputElement>) => { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 151 | setInput(event.target.value); |
simon | 80b7b3b | 2022-09-28 17:50:10 -0400 | [diff] [blame] | 152 | _onChange?.(event); |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 153 | }, |
simon | 80b7b3b | 2022-09-28 17:50:10 -0400 | [diff] [blame] | 154 | [_onChange] |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 155 | ); |
| 156 | |
| 157 | useEffect(() => { |
| 158 | /* Handle startAdornment */ |
| 159 | let Icon = StyledLockIcon; |
| 160 | let visibility = 'visible'; |
| 161 | if (props.error) { |
| 162 | Icon = StyledRoundSaltireIconError; |
Michelle Sepkap Sime | e580f42 | 2022-10-31 23:27:04 -0400 | [diff] [blame] | 163 | } else if (success) { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 164 | Icon = StyledCheckedIconSuccess; |
| 165 | } else if (!isSelected && !input) { |
| 166 | visibility = 'hidden'; // keep icon's space so text does not move |
| 167 | } |
| 168 | setStartAdornment(<Icon sx={{ visibility }} />); |
Michelle Sepkap Sime | e580f42 | 2022-10-31 23:27:04 -0400 | [diff] [blame] | 169 | }, [props.error, success, isSelected, input]); |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 170 | |
simon | ab4eec8 | 2022-11-08 20:32:43 -0500 | [diff] [blame] | 171 | /* |
| 172 | t('password_input_helper_text_too_weak') |
| 173 | t('password_input_helper_text_weak') |
| 174 | t('password_input_helper_text_medium') |
| 175 | t('password_input_helper_text_strong') |
| 176 | t('password_input_helper_text_registration_failed') |
| 177 | */ |
| 178 | const helperText = t('password_input_helper_text', { context: `${status}` }); |
| 179 | |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 180 | return ( |
Michelle Sepkap Sime | f5ebc2e | 2022-10-27 18:30:53 -0400 | [diff] [blame] | 181 | <> |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 182 | <InfosDialog {...dialogHandler.props} title={t('password_rules_dialog_title')} content={<PasswordRules />} /> |
Michelle Sepkap Sime | f5ebc2e | 2022-10-27 18:30:53 -0400 | [diff] [blame] | 183 | <TextField |
Michelle Sepkap Sime | e580f42 | 2022-10-31 23:27:04 -0400 | [diff] [blame] | 184 | color={inputColor(props.error, success)} |
Michelle Sepkap Sime | 559cc80 | 2022-11-05 12:06:40 -0400 | [diff] [blame] | 185 | label={t('password_input_label')} |
Michelle Sepkap Sime | f5ebc2e | 2022-10-27 18:30:53 -0400 | [diff] [blame] | 186 | type={showPassword ? 'text' : 'password'} |
| 187 | variant="standard" |
| 188 | autoComplete="current-password" |
simon | ab4eec8 | 2022-11-08 20:32:43 -0500 | [diff] [blame] | 189 | helperText={status !== 'default' ? helperText : ''} |
Michelle Sepkap Sime | f5ebc2e | 2022-10-27 18:30:53 -0400 | [diff] [blame] | 190 | onChange={onChange} |
simon | ab4eec8 | 2022-11-08 20:32:43 -0500 | [diff] [blame] | 191 | onFocus={() => setIsSelected(true)} |
| 192 | onBlur={() => setIsSelected(false)} |
| 193 | {...props} |
| 194 | InputLabelProps={{ shrink: !!(isSelected || input), ...props.InputLabelProps }} |
Michelle Sepkap Sime | f5ebc2e | 2022-10-27 18:30:53 -0400 | [diff] [blame] | 195 | InputProps={{ |
| 196 | startAdornment, |
| 197 | endAdornment: ( |
| 198 | <Stack direction="row" spacing="14px" alignItems="center"> |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 199 | <InfoButton tooltipTitle={tooltipTitle} {...infoButtonProps} onClick={dialogHandler.openDialog} /> |
Michelle Sepkap Sime | f5ebc2e | 2022-10-27 18:30:53 -0400 | [diff] [blame] | 200 | <ToggleVisibilityButton visible={showPassword} onClick={toggleShowPassword} /> |
| 201 | </Stack> |
| 202 | ), |
simon | ab4eec8 | 2022-11-08 20:32:43 -0500 | [diff] [blame] | 203 | ...props.InputProps, |
Michelle Sepkap Sime | f5ebc2e | 2022-10-27 18:30:53 -0400 | [diff] [blame] | 204 | }} |
Michelle Sepkap Sime | f5ebc2e | 2022-10-27 18:30:53 -0400 | [diff] [blame] | 205 | /> |
| 206 | </> |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 207 | ); |
| 208 | }; |
idillon-sfl | 37c18df | 2022-08-26 18:44:27 -0400 | [diff] [blame] | 209 | |
simon | 3537869 | 2022-10-02 23:25:57 -0400 | [diff] [blame] | 210 | export const NickNameInput = ({ onChange: _onChange, ...props }: TextFieldProps) => { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 211 | const [isSelected, setIsSelected] = useState(false); |
| 212 | const [input, setInput] = useState(props.defaultValue); |
simon | 3537869 | 2022-10-02 23:25:57 -0400 | [diff] [blame] | 213 | const [startAdornmentVisibility, setStartAdornmentVisibility] = useState<'visible' | 'hidden'>('hidden'); |
idillon-sfl | 37c18df | 2022-08-26 18:44:27 -0400 | [diff] [blame] | 214 | |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 215 | const onChange = useCallback( |
simon | 3537869 | 2022-10-02 23:25:57 -0400 | [diff] [blame] | 216 | (event: ChangeEvent<HTMLInputElement>) => { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 217 | setInput(event.target.value); |
simon | 80b7b3b | 2022-09-28 17:50:10 -0400 | [diff] [blame] | 218 | _onChange?.(event); |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 219 | }, |
simon | 80b7b3b | 2022-09-28 17:50:10 -0400 | [diff] [blame] | 220 | [_onChange] |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 221 | ); |
idillon-sfl | 37c18df | 2022-08-26 18:44:27 -0400 | [diff] [blame] | 222 | |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 223 | useEffect(() => { |
| 224 | setStartAdornmentVisibility(isSelected || input ? 'visible' : 'hidden'); |
| 225 | }, [isSelected, input]); |
idillon-sfl | 37c18df | 2022-08-26 18:44:27 -0400 | [diff] [blame] | 226 | |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 227 | return ( |
| 228 | <TextField |
| 229 | {...props} |
| 230 | label="Nickname, surname..." |
| 231 | variant="standard" |
| 232 | InputLabelProps={{ shrink: !!(isSelected || input) }} |
| 233 | onChange={onChange} |
| 234 | InputProps={{ |
| 235 | startAdornment: <StyledPenIconLight sx={{ visibility: startAdornmentVisibility }} />, |
| 236 | }} |
| 237 | onFocus={() => setIsSelected(true)} |
| 238 | onBlur={() => setIsSelected(false)} |
| 239 | /> |
| 240 | ); |
| 241 | }; |
idillon-sfl | 37c18df | 2022-08-26 18:44:27 -0400 | [diff] [blame] | 242 | |
simon | 3537869 | 2022-10-02 23:25:57 -0400 | [diff] [blame] | 243 | export const RegularInput = ({ onChange: _onChange, ...props }: TextFieldProps) => { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 244 | const [isSelected, setIsSelected] = useState(false); |
| 245 | const [input, setInput] = useState(props.defaultValue); |
simon | 3537869 | 2022-10-02 23:25:57 -0400 | [diff] [blame] | 246 | const [startAdornmentVisibility, setStartAdornmentVisibility] = useState<'visible' | 'hidden'>('hidden'); |
| 247 | const [endAdornmentVisibility, setEndAdornmentVisibility] = useState<'visible' | 'hidden'>('visible'); |
idillon-sfl | 37c18df | 2022-08-26 18:44:27 -0400 | [diff] [blame] | 248 | |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 249 | const onChange = useCallback( |
simon | 3537869 | 2022-10-02 23:25:57 -0400 | [diff] [blame] | 250 | (event: ChangeEvent<HTMLInputElement>) => { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 251 | setInput(event.target.value); |
simon | 80b7b3b | 2022-09-28 17:50:10 -0400 | [diff] [blame] | 252 | _onChange?.(event); |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 253 | }, |
simon | 80b7b3b | 2022-09-28 17:50:10 -0400 | [diff] [blame] | 254 | [_onChange] |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 255 | ); |
idillon-sfl | 37c18df | 2022-08-26 18:44:27 -0400 | [diff] [blame] | 256 | |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 257 | useEffect(() => { |
| 258 | setStartAdornmentVisibility(isSelected || input ? 'visible' : 'hidden'); |
| 259 | setEndAdornmentVisibility(isSelected || input ? 'hidden' : 'visible'); |
| 260 | }, [isSelected, input]); |
idillon-sfl | 37c18df | 2022-08-26 18:44:27 -0400 | [diff] [blame] | 261 | |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 262 | return ( |
| 263 | <TextField |
| 264 | {...props} |
| 265 | variant="standard" |
| 266 | InputLabelProps={{ shrink: !!(isSelected || input) }} |
| 267 | onChange={onChange} |
| 268 | InputProps={{ |
| 269 | startAdornment: <StyledPenIconLight sx={{ visibility: startAdornmentVisibility }} />, |
| 270 | endAdornment: <StyledPenIconDark sx={{ visibility: endAdornmentVisibility }} />, |
| 271 | }} |
| 272 | onFocus={() => setIsSelected(true)} |
| 273 | onBlur={() => setIsSelected(false)} |
| 274 | /> |
| 275 | ); |
| 276 | }; |
Michelle Sepkap Sime | f5ebc2e | 2022-10-27 18:30:53 -0400 | [diff] [blame] | 277 | |
Michelle Sepkap Sime | 51c0045 | 2022-10-31 21:26:38 -0400 | [diff] [blame] | 278 | function inputColor( |
Michelle Sepkap Sime | f5ebc2e | 2022-10-27 18:30:53 -0400 | [diff] [blame] | 279 | error?: boolean, |
| 280 | success?: boolean |
| 281 | ): 'success' | 'error' | 'primary' | 'secondary' | 'info' | 'warning' | undefined { |
| 282 | return error ? 'error' : success ? 'success' : 'primary'; |
| 283 | } |
| 284 | |
| 285 | const PasswordRules = () => { |
Michelle Sepkap Sime | 559cc80 | 2022-11-05 12:06:40 -0400 | [diff] [blame] | 286 | const { t } = useTranslation(); |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 287 | const items = useMemo( |
| 288 | () => [ |
| 289 | { |
| 290 | Icon: GppMaybe, |
idillon | 6e8ca41 | 2022-12-16 11:22:42 -0500 | [diff] [blame] | 291 | value: t('password_rule_1'), |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 292 | }, |
| 293 | { |
| 294 | Icon: GppMaybe, |
idillon | 6e8ca41 | 2022-12-16 11:22:42 -0500 | [diff] [blame] | 295 | value: t('password_rule_2'), |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 296 | }, |
| 297 | { |
| 298 | Icon: GppMaybe, |
idillon | 6e8ca41 | 2022-12-16 11:22:42 -0500 | [diff] [blame] | 299 | value: t('password_rule_3'), |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 300 | }, |
| 301 | { |
| 302 | Icon: GppMaybe, |
idillon | 6e8ca41 | 2022-12-16 11:22:42 -0500 | [diff] [blame] | 303 | value: t('password_rule_4'), |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 304 | }, |
| 305 | { |
| 306 | Icon: GppMaybe, |
idillon | 6e8ca41 | 2022-12-16 11:22:42 -0500 | [diff] [blame] | 307 | value: t('password_rule_5'), |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 308 | }, |
| 309 | ], |
| 310 | [t] |
Michelle Sepkap Sime | f5ebc2e | 2022-10-27 18:30:53 -0400 | [diff] [blame] | 311 | ); |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 312 | return <DialogContentList items={items} />; |
Michelle Sepkap Sime | f5ebc2e | 2022-10-27 18:30:53 -0400 | [diff] [blame] | 313 | }; |
| 314 | |
| 315 | const UsernameRules = () => { |
Michelle Sepkap Sime | 559cc80 | 2022-11-05 12:06:40 -0400 | [diff] [blame] | 316 | const { t } = useTranslation(); |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 317 | const items = useMemo( |
| 318 | () => [ |
| 319 | { |
| 320 | Icon: Warning, |
idillon | 6e8ca41 | 2022-12-16 11:22:42 -0500 | [diff] [blame] | 321 | value: t('username_rule_1'), |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 322 | }, |
| 323 | { |
| 324 | Icon: Warning, |
idillon | 6e8ca41 | 2022-12-16 11:22:42 -0500 | [diff] [blame] | 325 | value: t('username_rule_2'), |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 326 | }, |
| 327 | { |
| 328 | Icon: Warning, |
idillon | 6e8ca41 | 2022-12-16 11:22:42 -0500 | [diff] [blame] | 329 | value: t('username_rule_3'), |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 330 | }, |
| 331 | { |
| 332 | Icon: Warning, |
idillon | 6e8ca41 | 2022-12-16 11:22:42 -0500 | [diff] [blame] | 333 | value: t('username_rule_4'), |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 334 | }, |
| 335 | ], |
| 336 | [t] |
Michelle Sepkap Sime | f5ebc2e | 2022-10-27 18:30:53 -0400 | [diff] [blame] | 337 | ); |
idillon | ef9ab81 | 2022-11-18 13:46:24 -0500 | [diff] [blame] | 338 | return <DialogContentList items={items} />; |
Michelle Sepkap Sime | f5ebc2e | 2022-10-27 18:30:53 -0400 | [diff] [blame] | 339 | }; |