-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSignupTextField.tsx
More file actions
131 lines (123 loc) · 3.5 KB
/
SignupTextField.tsx
File metadata and controls
131 lines (123 loc) · 3.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import type { SignupTextFieldProps } from './SignupTextField.types';
import * as styles from './SignupTextField.css';
import { validateField } from './validation';
import { useSignupTextFieldState } from './useSignupTextFieldState';
import { RenderInputContent } from './RenderInputContent';
import { formatBirthDate } from '@/common/util/format';
function getFieldState(
isFocused: boolean,
hasValue: boolean,
isHovered: boolean,
error: boolean,
isLocked: boolean,
): keyof typeof styles.fieldVariants {
if (isLocked) {
return 'locked';
}
if (error) {
return 'error';
}
if (isFocused && hasValue) {
return 'typing';
}
if (isFocused && !hasValue) {
return 'clicked';
}
if (!isFocused && hasValue) {
return 'completed';
}
if (isHovered) {
return 'clicked';
}
return 'default';
}
export default function SignupTextField({
type,
value,
onChange,
placeholder,
error: externalError,
onBlur,
onFocus,
}: SignupTextFieldProps) {
const {
state,
dispatch,
inputRef,
handleKeyDown,
handleClearClick,
handleWrapperClick,
handleWrapperKeyDown,
} = useSignupTextFieldState({ onChange });
const isLocked = type === 'email';
const error =
externalError ||
(type === 'email' ? undefined : validateField(type as 'name' | 'birth' | 'job', value));
const hasValue = Boolean(value);
const fieldState = getFieldState(state.isFocused, hasValue, state.isHovered, !!error, isLocked);
const wrapperProps = isLocked
? { tabIndex: -1 as const }
: {
role: 'button' as const,
tabIndex: 0 as const,
onClick: handleWrapperClick,
onKeyDown: handleWrapperKeyDown,
onMouseEnter: () => dispatch({ type: 'HOVER_ENTER' }),
onMouseLeave: () => dispatch({ type: 'HOVER_LEAVE' }),
};
function createInputProps() {
return {
ref: inputRef,
type: 'text' as const,
value,
onChange: (e: React.ChangeEvent<HTMLInputElement>) => {
if (!isLocked) {
if (type === 'birth') {
onChange(formatBirthDate(e.target.value));
} else {
onChange(e.target.value);
}
}
},
onFocus: () => {
dispatch({ type: 'FOCUS' });
onFocus?.();
},
onBlur: () => {
dispatch({ type: 'BLUR' });
onBlur?.();
},
onKeyDown: handleKeyDown,
onCompositionStart: () => dispatch({ type: 'COMPOSE_START' }),
onCompositionEnd: (e: React.CompositionEvent<HTMLInputElement>) => {
dispatch({ type: 'COMPOSE_END' });
if (type === 'name' || type === 'job') {
const filtered = e.currentTarget.value.replace(/[^a-zA-Z가-힣ㄱ-ㅎㅏ-ㅣ\s]/g, '');
onChange(filtered);
}
},
placeholder: placeholder ?? (type === 'job' ? '정보를 입력해주세요' : placeholder),
disabled: isLocked,
className: [styles.inputBase, styles.inputStyle].join(' '),
};
}
const inputProps = createInputProps();
return (
<div className={error ? styles.errorMessageWrapper : undefined}>
<div
className={[styles.baseClass, styles.fieldVariants[fieldState]].join(' ')}
{...wrapperProps}
>
<RenderInputContent
fieldState={fieldState}
inputProps={inputProps}
value={value}
isLocked={isLocked}
handleClearClick={handleClearClick}
styles={styles}
/>
</div>
{error && <div className={styles.errorMessage}>{error}</div>}
</div>
);
}