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 | */ |
simon | 3537869 | 2022-10-02 23:25:57 -0400 | [diff] [blame] | 18 | import { IconButtonProps, Stack, TextField, TextFieldProps } from '@mui/material'; |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 19 | import { styled } from '@mui/material/styles'; |
simon | 3537869 | 2022-10-02 23:25:57 -0400 | [diff] [blame] | 20 | import { ChangeEvent, ReactElement, useCallback, useEffect, useState } from 'react'; |
simon | 07b4eb0 | 2022-09-29 17:50:26 -0400 | [diff] [blame] | 21 | |
simon | 3537869 | 2022-10-02 23:25:57 -0400 | [diff] [blame] | 22 | import { InfoButton, ToggleVisibilityButton } from './Button'; |
| 23 | import { CheckedIcon, LockIcon, PenIcon, PersonIcon, RoundSaltireIcon } from './SvgIcon'; |
idillon-sfl | 37c18df | 2022-08-26 18:44:27 -0400 | [diff] [blame] | 24 | |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 25 | const iconsHeight = '16px'; |
| 26 | const StyledCheckedIconSuccess = styled(CheckedIcon)(({ theme }) => ({ |
| 27 | height: iconsHeight, |
| 28 | color: theme.palette.success.main, |
| 29 | })); |
| 30 | const StyledRoundSaltireIconError = styled(RoundSaltireIcon)(({ theme }) => ({ |
| 31 | height: iconsHeight, |
| 32 | color: theme.palette.error.main, |
| 33 | })); |
| 34 | const StyledPenIconLight = styled(PenIcon)({ height: iconsHeight, color: '#03B9E9' }); |
| 35 | const StyledPenIconDark = styled(PenIcon)(({ theme }) => ({ height: iconsHeight, color: theme.palette.primary.dark })); |
| 36 | const StyledPersonIconLight = styled(PersonIcon)({ height: iconsHeight, color: '#03B9E9' }); |
| 37 | const StyledLockIcon = styled(LockIcon)({ height: iconsHeight, color: '#03B9E9' }); |
idillon-sfl | 37c18df | 2022-08-26 18:44:27 -0400 | [diff] [blame] | 38 | |
simon | 3537869 | 2022-10-02 23:25:57 -0400 | [diff] [blame] | 39 | type InputProps = TextFieldProps & { |
| 40 | infoButtonProps?: IconButtonProps; |
| 41 | success?: boolean; |
| 42 | }; |
| 43 | |
| 44 | export const UsernameInput = ({ infoButtonProps, onChange: _onChange, ...props }: InputProps) => { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 45 | const [isSelected, setIsSelected] = useState(false); |
| 46 | const [input, setInput] = useState(props.defaultValue); |
simon | 3537869 | 2022-10-02 23:25:57 -0400 | [diff] [blame] | 47 | const [startAdornment, setStartAdornment] = useState<ReactElement | undefined>(); |
idillon-sfl | 37c18df | 2022-08-26 18:44:27 -0400 | [diff] [blame] | 48 | |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 49 | const onChange = useCallback( |
simon | 3537869 | 2022-10-02 23:25:57 -0400 | [diff] [blame] | 50 | (event: ChangeEvent<HTMLInputElement>) => { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 51 | setInput(event.target.value); |
simon | 80b7b3b | 2022-09-28 17:50:10 -0400 | [diff] [blame] | 52 | _onChange?.(event); |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 53 | }, |
simon | 80b7b3b | 2022-09-28 17:50:10 -0400 | [diff] [blame] | 54 | [_onChange] |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 55 | ); |
idillon-sfl | 37c18df | 2022-08-26 18:44:27 -0400 | [diff] [blame] | 56 | |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 57 | useEffect(() => { |
| 58 | /* Handle startAdornment */ |
| 59 | let Icon = StyledPersonIconLight; |
| 60 | let visibility = 'visible'; |
| 61 | if (props.error) { |
| 62 | Icon = StyledRoundSaltireIconError; |
| 63 | } else if (props.success) { |
| 64 | Icon = StyledCheckedIconSuccess; |
| 65 | } else if (!isSelected && !input) { |
| 66 | visibility = 'hidden'; // keep icon's space so text does not move |
idillon-sfl | 37c18df | 2022-08-26 18:44:27 -0400 | [diff] [blame] | 67 | } |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 68 | setStartAdornment(<Icon sx={{ visibility }} />); |
| 69 | }, [props.error, props.success, isSelected, input]); |
idillon-sfl | 37c18df | 2022-08-26 18:44:27 -0400 | [diff] [blame] | 70 | |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 71 | return ( |
| 72 | <TextField |
| 73 | {...props} |
| 74 | label="Choose an identifier" |
| 75 | variant="standard" |
| 76 | InputLabelProps={{ shrink: !!(isSelected || input) }} |
| 77 | onChange={onChange} |
| 78 | InputProps={{ |
| 79 | startAdornment, |
| 80 | endAdornment: <InfoButton {...infoButtonProps} />, |
| 81 | }} |
| 82 | onFocus={() => setIsSelected(true)} |
| 83 | onBlur={() => setIsSelected(false)} |
| 84 | /> |
| 85 | ); |
| 86 | }; |
idillon-sfl | 37c18df | 2022-08-26 18:44:27 -0400 | [diff] [blame] | 87 | |
simon | 3537869 | 2022-10-02 23:25:57 -0400 | [diff] [blame] | 88 | export const PasswordInput = ({ infoButtonProps, onChange: _onChange, ...props }: InputProps) => { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 89 | const [showPassword, setShowPassword] = useState(false); |
| 90 | const [isSelected, setIsSelected] = useState(false); |
| 91 | const [input, setInput] = useState(props.defaultValue); |
simon | 3537869 | 2022-10-02 23:25:57 -0400 | [diff] [blame] | 92 | const [startAdornment, setStartAdornment] = useState<ReactElement | undefined>(); |
idillon-sfl | 37c18df | 2022-08-26 18:44:27 -0400 | [diff] [blame] | 93 | |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 94 | const toggleShowPassword = () => { |
| 95 | setShowPassword((showPassword) => !showPassword); |
| 96 | }; |
| 97 | |
| 98 | const onChange = useCallback( |
simon | 3537869 | 2022-10-02 23:25:57 -0400 | [diff] [blame] | 99 | (event: ChangeEvent<HTMLInputElement>) => { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 100 | setInput(event.target.value); |
simon | 80b7b3b | 2022-09-28 17:50:10 -0400 | [diff] [blame] | 101 | _onChange?.(event); |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 102 | }, |
simon | 80b7b3b | 2022-09-28 17:50:10 -0400 | [diff] [blame] | 103 | [_onChange] |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 104 | ); |
| 105 | |
| 106 | useEffect(() => { |
| 107 | /* Handle startAdornment */ |
| 108 | let Icon = StyledLockIcon; |
| 109 | let visibility = 'visible'; |
| 110 | if (props.error) { |
| 111 | Icon = StyledRoundSaltireIconError; |
| 112 | } else if (props.success) { |
| 113 | Icon = StyledCheckedIconSuccess; |
| 114 | } else if (!isSelected && !input) { |
| 115 | visibility = 'hidden'; // keep icon's space so text does not move |
| 116 | } |
| 117 | setStartAdornment(<Icon sx={{ visibility }} />); |
| 118 | }, [props.error, props.success, isSelected, input]); |
| 119 | |
| 120 | return ( |
| 121 | <TextField |
| 122 | {...props} |
| 123 | label="Password" |
| 124 | type={showPassword ? 'text' : 'password'} |
| 125 | variant="standard" |
| 126 | autoComplete="current-password" |
| 127 | InputLabelProps={{ shrink: !!(isSelected || input) }} |
| 128 | onChange={onChange} |
| 129 | InputProps={{ |
| 130 | startAdornment, |
| 131 | endAdornment: ( |
| 132 | <Stack direction="row" spacing="14px"> |
| 133 | <InfoButton {...infoButtonProps} /> |
| 134 | <ToggleVisibilityButton visible={showPassword} onClick={toggleShowPassword} /> |
| 135 | </Stack> |
| 136 | ), |
| 137 | }} |
| 138 | onFocus={() => setIsSelected(true)} |
| 139 | onBlur={() => setIsSelected(false)} |
| 140 | /> |
| 141 | ); |
| 142 | }; |
idillon-sfl | 37c18df | 2022-08-26 18:44:27 -0400 | [diff] [blame] | 143 | |
simon | 3537869 | 2022-10-02 23:25:57 -0400 | [diff] [blame] | 144 | export const NickNameInput = ({ onChange: _onChange, ...props }: TextFieldProps) => { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 145 | const [isSelected, setIsSelected] = useState(false); |
| 146 | const [input, setInput] = useState(props.defaultValue); |
simon | 3537869 | 2022-10-02 23:25:57 -0400 | [diff] [blame] | 147 | const [startAdornmentVisibility, setStartAdornmentVisibility] = useState<'visible' | 'hidden'>('hidden'); |
idillon-sfl | 37c18df | 2022-08-26 18:44:27 -0400 | [diff] [blame] | 148 | |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 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 | ); |
idillon-sfl | 37c18df | 2022-08-26 18:44:27 -0400 | [diff] [blame] | 156 | |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 157 | useEffect(() => { |
| 158 | setStartAdornmentVisibility(isSelected || input ? 'visible' : 'hidden'); |
| 159 | }, [isSelected, input]); |
idillon-sfl | 37c18df | 2022-08-26 18:44:27 -0400 | [diff] [blame] | 160 | |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 161 | return ( |
| 162 | <TextField |
| 163 | {...props} |
| 164 | label="Nickname, surname..." |
| 165 | variant="standard" |
| 166 | InputLabelProps={{ shrink: !!(isSelected || input) }} |
| 167 | onChange={onChange} |
| 168 | InputProps={{ |
| 169 | startAdornment: <StyledPenIconLight sx={{ visibility: startAdornmentVisibility }} />, |
| 170 | }} |
| 171 | onFocus={() => setIsSelected(true)} |
| 172 | onBlur={() => setIsSelected(false)} |
| 173 | /> |
| 174 | ); |
| 175 | }; |
idillon-sfl | 37c18df | 2022-08-26 18:44:27 -0400 | [diff] [blame] | 176 | |
simon | 3537869 | 2022-10-02 23:25:57 -0400 | [diff] [blame] | 177 | export const RegularInput = ({ onChange: _onChange, ...props }: TextFieldProps) => { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 178 | const [isSelected, setIsSelected] = useState(false); |
| 179 | const [input, setInput] = useState(props.defaultValue); |
simon | 3537869 | 2022-10-02 23:25:57 -0400 | [diff] [blame] | 180 | const [startAdornmentVisibility, setStartAdornmentVisibility] = useState<'visible' | 'hidden'>('hidden'); |
| 181 | const [endAdornmentVisibility, setEndAdornmentVisibility] = useState<'visible' | 'hidden'>('visible'); |
idillon-sfl | 37c18df | 2022-08-26 18:44:27 -0400 | [diff] [blame] | 182 | |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 183 | const onChange = useCallback( |
simon | 3537869 | 2022-10-02 23:25:57 -0400 | [diff] [blame] | 184 | (event: ChangeEvent<HTMLInputElement>) => { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 185 | setInput(event.target.value); |
simon | 80b7b3b | 2022-09-28 17:50:10 -0400 | [diff] [blame] | 186 | _onChange?.(event); |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 187 | }, |
simon | 80b7b3b | 2022-09-28 17:50:10 -0400 | [diff] [blame] | 188 | [_onChange] |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 189 | ); |
idillon-sfl | 37c18df | 2022-08-26 18:44:27 -0400 | [diff] [blame] | 190 | |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 191 | useEffect(() => { |
| 192 | setStartAdornmentVisibility(isSelected || input ? 'visible' : 'hidden'); |
| 193 | setEndAdornmentVisibility(isSelected || input ? 'hidden' : 'visible'); |
| 194 | }, [isSelected, input]); |
idillon-sfl | 37c18df | 2022-08-26 18:44:27 -0400 | [diff] [blame] | 195 | |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 196 | return ( |
| 197 | <TextField |
| 198 | {...props} |
| 199 | variant="standard" |
| 200 | InputLabelProps={{ shrink: !!(isSelected || input) }} |
| 201 | onChange={onChange} |
| 202 | InputProps={{ |
| 203 | startAdornment: <StyledPenIconLight sx={{ visibility: startAdornmentVisibility }} />, |
| 204 | endAdornment: <StyledPenIconDark sx={{ visibility: endAdornmentVisibility }} />, |
| 205 | }} |
| 206 | onFocus={() => setIsSelected(true)} |
| 207 | onBlur={() => setIsSelected(false)} |
| 208 | /> |
| 209 | ); |
| 210 | }; |