|
| 1 | +import React, { useState, useEffect, useRef } from 'react'; |
| 2 | +import styled, { css } from 'styled-components/macro'; |
| 3 | +import theme from '../../../App/ThemeStyles'; |
| 4 | + |
| 5 | +const InputWrapper = styled.div<{ width: string }>` |
| 6 | + position: relative; |
| 7 | + width: ${(props) => props.width}; |
| 8 | + display: flex; |
| 9 | + align-items: flex-start; |
| 10 | + flex-direction: column; |
| 11 | + position: relative; |
| 12 | + justify-content: center; |
| 13 | +
|
| 14 | + margin-bottom: 2.2rem; |
| 15 | + margin-top: 24px; |
| 16 | +`; |
| 17 | + |
| 18 | +const InputLabel = styled.span<{ width: string }>` |
| 19 | + width: ${(props) => props.width}; |
| 20 | + padding: 0.7rem !important; |
| 21 | + color: #c2c6dc; |
| 22 | + left: 0; |
| 23 | + top: 0; |
| 24 | + transition: all 0.2s ease; |
| 25 | + position: absolute; |
| 26 | + border-radius: 5px; |
| 27 | + overflow: hidden; |
| 28 | + font-size: 0.85rem; |
| 29 | + cursor: text; |
| 30 | + font-size: 12px; |
| 31 | + user-select: none; |
| 32 | + pointer-events: none; |
| 33 | +} |
| 34 | +`; |
| 35 | + |
| 36 | +const InputInput = styled.input<{ |
| 37 | + hasValue: boolean; |
| 38 | + hasIcon: boolean; |
| 39 | + width: string; |
| 40 | + focusBg: string; |
| 41 | + borderColor: string; |
| 42 | +}>` |
| 43 | + width: ${(props) => props.width}; |
| 44 | + font-size: 14px; |
| 45 | + border: 1px solid rgba(0, 0, 0, 0.2); |
| 46 | + border-color: ${(props) => props.borderColor}; |
| 47 | + background: #262c49; |
| 48 | + box-shadow: 0 0 0 0 rgba(0, 0, 0, 0.15); |
| 49 | + ${(props) => (props.hasIcon ? 'padding: 0.7rem 1rem 0.7rem 3rem;' : 'padding: 0.7rem;')} |
| 50 | +
|
| 51 | + &:-webkit-autofill, &:-webkit-autofill:hover, &:-webkit-autofill:focus, &:-webkit-autofill:active { |
| 52 | + -webkit-box-shadow: 0 0 0 30px #262c49 inset !important; |
| 53 | + } |
| 54 | + &:-webkit-autofill { |
| 55 | + -webkit-text-fill-color: #c2c6dc !important; |
| 56 | + } |
| 57 | + line-height: 16px; |
| 58 | + color: #c2c6dc; |
| 59 | + position: relative; |
| 60 | + border-radius: 5px; |
| 61 | + transition: all 0.3s ease; |
| 62 | + &:focus { |
| 63 | + box-shadow: 0 3px 10px 0 rgba(0, 0, 0, 0.15); |
| 64 | + border: 1px solid ${(props) => props.theme.colors.primary}; |
| 65 | + background: ${(props) => props.focusBg}; |
| 66 | + } |
| 67 | + &:focus ~ ${InputLabel} { |
| 68 | + color: ${(props) => props.theme.colors.primary}; |
| 69 | + transform: translate(-3px, -90%); |
| 70 | + } |
| 71 | + ${(props) => |
| 72 | + props.hasValue && |
| 73 | + css` |
| 74 | + & ~ ${InputLabel} { |
| 75 | + color: ${props.theme.colors.primary}; |
| 76 | + transform: translate(-3px, -90%); |
| 77 | + } |
| 78 | + `} |
| 79 | +`; |
| 80 | + |
| 81 | +const Icon = styled.div` |
| 82 | + display: flex; |
| 83 | + left: 16px; |
| 84 | + position: absolute; |
| 85 | +`; |
| 86 | + |
| 87 | +type FormInputProps = { |
| 88 | + variant?: 'normal' | 'alternate'; |
| 89 | + disabled?: boolean; |
| 90 | + label?: string; |
| 91 | + width?: string; |
| 92 | + floatingLabel?: boolean; |
| 93 | + placeholder?: string; |
| 94 | + icon?: JSX.Element; |
| 95 | + type?: string; |
| 96 | + autocomplete?: boolean; |
| 97 | + autoFocus?: boolean; |
| 98 | + autoSelect?: boolean; |
| 99 | + id?: string; |
| 100 | + name?: string; |
| 101 | + onChange: any; |
| 102 | + onBlur: any; |
| 103 | + className?: string; |
| 104 | + defaultValue?: string; |
| 105 | + value?: string; |
| 106 | + onClick?: (e: React.MouseEvent<HTMLInputElement>) => void; |
| 107 | +}; |
| 108 | + |
| 109 | +function useCombinedRefs(...refs: any) { |
| 110 | + const targetRef = React.useRef(); |
| 111 | + |
| 112 | + React.useEffect(() => { |
| 113 | + refs.forEach((ref: any) => { |
| 114 | + if (!ref) return; |
| 115 | + |
| 116 | + if (typeof ref === 'function') { |
| 117 | + ref(targetRef.current); |
| 118 | + } else { |
| 119 | + ref.current = targetRef.current; |
| 120 | + } |
| 121 | + }); |
| 122 | + }, [refs]); |
| 123 | + |
| 124 | + return targetRef; |
| 125 | +} |
| 126 | + |
| 127 | +const FormInput = React.forwardRef( |
| 128 | + ( |
| 129 | + { |
| 130 | + disabled = false, |
| 131 | + width = 'auto', |
| 132 | + variant = 'normal', |
| 133 | + type = 'text', |
| 134 | + autoFocus = false, |
| 135 | + autoSelect = false, |
| 136 | + autocomplete, |
| 137 | + label, |
| 138 | + placeholder, |
| 139 | + onBlur, |
| 140 | + onChange, |
| 141 | + icon, |
| 142 | + name, |
| 143 | + className, |
| 144 | + onClick, |
| 145 | + floatingLabel, |
| 146 | + defaultValue, |
| 147 | + value, |
| 148 | + id, |
| 149 | + }: FormInputProps, |
| 150 | + $ref: any, |
| 151 | + ) => { |
| 152 | + const [hasValue, setHasValue] = useState(defaultValue !== ''); |
| 153 | + const borderColor = variant === 'normal' ? 'rgba(0,0,0,0.2)' : theme.colors.alternate; |
| 154 | + const focusBg = variant === 'normal' ? theme.colors.bg.secondary : theme.colors.bg.primary; |
| 155 | + |
| 156 | + // Merge forwarded ref and internal ref in order to be able to access the ref in the useEffect |
| 157 | + // The forwarded ref is not accessible by itself, which is what the innerRef & combined ref is for |
| 158 | + // TODO(jordanknott): This is super ugly, find a better approach? |
| 159 | + const $innerRef = React.useRef<HTMLInputElement>(null); |
| 160 | + const combinedRef: any = useCombinedRefs($ref, $innerRef); |
| 161 | + useEffect(() => { |
| 162 | + if (combinedRef && combinedRef.current) { |
| 163 | + if (autoFocus) { |
| 164 | + combinedRef.current.focus(); |
| 165 | + } |
| 166 | + if (autoSelect) { |
| 167 | + combinedRef.current.select(); |
| 168 | + } |
| 169 | + } |
| 170 | + }, []); |
| 171 | + return ( |
| 172 | + <InputWrapper className={className} width={width}> |
| 173 | + <InputInput |
| 174 | + onChange={(e) => { |
| 175 | + setHasValue((e.currentTarget.value !== '' || floatingLabel) ?? false); |
| 176 | + onChange(e); |
| 177 | + }} |
| 178 | + disabled={disabled} |
| 179 | + hasValue={hasValue} |
| 180 | + ref={combinedRef} |
| 181 | + id={id} |
| 182 | + type={type} |
| 183 | + name={name} |
| 184 | + onClick={onClick} |
| 185 | + autoComplete={autocomplete ? 'on' : 'off'} |
| 186 | + defaultValue={defaultValue} |
| 187 | + onBlur={onBlur} |
| 188 | + value={value} |
| 189 | + hasIcon={typeof icon !== 'undefined'} |
| 190 | + width={width} |
| 191 | + placeholder={placeholder} |
| 192 | + focusBg={focusBg} |
| 193 | + borderColor={borderColor} |
| 194 | + /> |
| 195 | + {label && <InputLabel width={width}>{label}</InputLabel>} |
| 196 | + <Icon>{icon && icon}</Icon> |
| 197 | + </InputWrapper> |
| 198 | + ); |
| 199 | + }, |
| 200 | +); |
| 201 | + |
| 202 | +export default FormInput; |
0 commit comments