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