-
Notifications
You must be signed in to change notification settings - Fork 34
/
textbox.tsx
209 lines (191 loc) · 7.58 KB
/
textbox.tsx
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
import React, {
cloneElement, ComponentProps, FC, Ref,
ChangeEvent, FocusEvent, KeyboardEvent, MouseEvent, SyntheticEvent,
useEffect, useState
} from 'react'
import classNames from 'classnames'
import { findComponent, withForwardRef } from '../common/component-utils'
import EbayTextboxPostfixIcon from './postfix-icon'
import EbayTextboxPostfixText from './postfix-text'
import EbayTextboxPrefixIcon from './prefix-icon'
import EbayTextboxPrefixText from './prefix-text'
import { useFloatingLabel } from '../common/floating-label-utils/hooks'
import {
EbayChangeEventHandler,
EbayEventHandler,
EbayFocusEventHandler,
EbayKeyboardEventHandler, EbayMouseEventHandler
} from '../common/event-utils/types'
import type { Size } from './types'
export const isControlled = (value?: unknown): boolean => typeof value !== 'undefined'
type TextInputProps = ComponentProps<'input'> & ComponentProps<'textarea'>
export type TextboxEventProps = { value: string }
export type EbayTextboxProps = {
fluid?: boolean;
invalid?: boolean;
multiline?: boolean;
defaultValue?: string;
inputSize?: Size;
floatingLabel?: string;
onChange?: EbayChangeEventHandler<HTMLTextAreaElement & HTMLInputElement, TextboxEventProps>;
onInputChange?: EbayChangeEventHandler<HTMLTextAreaElement & HTMLInputElement, TextboxEventProps>;
onFocus?: EbayFocusEventHandler<HTMLTextAreaElement & HTMLInputElement, TextboxEventProps>;
onBlur?: EbayFocusEventHandler<HTMLTextAreaElement & HTMLInputElement, TextboxEventProps>;
onKeyPress?: EbayKeyboardEventHandler<HTMLTextAreaElement & HTMLInputElement, TextboxEventProps>;
onKeyUp?: EbayKeyboardEventHandler<HTMLTextAreaElement & HTMLInputElement, TextboxEventProps>;
onKeyDown?: EbayKeyboardEventHandler<HTMLTextAreaElement & HTMLInputElement, TextboxEventProps>;
onInvalid?: EbayEventHandler<HTMLTextAreaElement & HTMLInputElement, TextboxEventProps>;
onFloatingLabelInit?: () => void;
onButtonClick?: EbayKeyboardEventHandler<HTMLInputElement, TextboxEventProps> &
EbayMouseEventHandler<HTMLInputElement, TextboxEventProps>;
forwardedRef?: Ref<HTMLTextAreaElement | HTMLInputElement>;
opaqueLabel?: boolean;
} & Omit<TextInputProps, 'onFocus' | 'onBlur' | 'onChange' | 'onKeyPress' | 'onKeyUp' | 'onKeyDown' | 'onInvalid'>;
const EbayTextbox: FC<EbayTextboxProps> = ({
type = 'text',
invalid,
fluid,
multiline,
onChange = () => {},
onInputChange = () => {},
onFocus = () => {},
onBlur = () => {},
onKeyPress = () => {},
onKeyUp = () => {},
onKeyDown = () => {},
onInvalid = () => {},
onFloatingLabelInit = () => {},
onButtonClick = () => {},
autoFocus,
defaultValue = '',
value: controlledValue,
forwardedRef,
inputSize = 'default',
floatingLabel,
children,
placeholder,
opaqueLabel,
...rest
}) => {
const [value, setValue] = useState(defaultValue)
const [inputValue, setInputValue] = useState(defaultValue)
const {
label,
Container,
onBlur: onFloatingLabelBlur,
onFocus: onFloatingLabelFocus,
ref,
placeholder: floatingLabelPlaceholder
} = useFloatingLabel({
ref: forwardedRef,
inputId: rest.id,
className: rest.className,
disabled: rest.disabled,
label: floatingLabel,
inputSize,
inputValue: controlledValue || inputValue,
placeholder,
invalid,
type,
opaqueLabel,
onMount: onFloatingLabelInit
})
const handleFocus = (event?: FocusEvent<HTMLInputElement & HTMLTextAreaElement>) => {
onFocus(event, { value: event?.target?.value || defaultValue })
onFloatingLabelFocus()
}
const handleBlur = (event: FocusEvent<HTMLInputElement & HTMLTextAreaElement>) => {
const newValue = event.target?.value
onBlur(event, { value: newValue })
if (newValue !== value) {
onChange(event, { value: newValue })
setValue(newValue)
}
onFloatingLabelBlur()
}
const handleKeyPress = (event?: KeyboardEvent<HTMLInputElement & HTMLTextAreaElement>) => {
const textbox = event.target as any
onKeyPress(event, { value: textbox?.value })
}
const handleKeyUp = (event?: KeyboardEvent<HTMLInputElement & HTMLTextAreaElement>) => {
const textbox = event.target as any
onKeyUp(event, { value: textbox?.value })
}
const handleKeyDown = (event?: KeyboardEvent<HTMLInputElement & HTMLTextAreaElement>) => {
const textbox = event.target as any
onKeyDown(event, { value: textbox?.value })
}
const handleInvalid = (event?: SyntheticEvent<HTMLInputElement & HTMLTextAreaElement>) => {
const textbox = event.target as any
onInvalid(event, { value: textbox?.value })
}
const handleButtonClick = (event?: KeyboardEvent<HTMLInputElement> & MouseEvent<HTMLInputElement>) => {
onButtonClick(event, { value })
}
useEffect(() => {
if (autoFocus) {
handleFocus()
}
}, [])
const handleInputChange = (e: ChangeEvent<HTMLTextAreaElement & HTMLInputElement>) => {
const newValue = e.target?.value
if (!isControlled(controlledValue)) {
setInputValue(newValue)
}
onInputChange(e, { value: newValue })
}
const Input = multiline ? 'textarea' : 'input'
const Wrapper = fluid ? 'div' : 'span'
const prefixIcon = findComponent(children, EbayTextboxPrefixIcon)
const prefixText = findComponent(children, EbayTextboxPrefixText)
const prefixId = prefixText?.props?.id
const postfixIcon = findComponent(children, EbayTextboxPostfixIcon)
const postfixText = findComponent(children, EbayTextboxPostfixText)
const postfixId = postfixText?.props?.id
const wrapperClassName = classNames('textbox', rest.className, {
'textbox--fluid': fluid,
'textbox--large': inputSize === 'large',
/** start remove after `:has` support */
'textbox--disabled': rest.disabled,
'textbox--invalid': invalid,
'textbox--readonly': rest.readOnly
/** end remove after `:has` support */
})
return (
<Container>
{label}
<Wrapper className={wrapperClassName}>
{prefixIcon}
{prefixText}
<Input
aria-describedby={[prefixId, postfixId].filter(Boolean).join(' ') || undefined}
{...rest}
className="textbox__control"
type={type}
aria-invalid={invalid}
value={isControlled(controlledValue) ? controlledValue : inputValue}
onChange={handleInputChange}
onBlur={handleBlur}
onFocus={handleFocus}
onKeyPress={handleKeyPress}
onKeyUp={handleKeyUp}
onKeyDown={handleKeyDown}
onInvalid={handleInvalid}
autoFocus={autoFocus}
ref={ref}
placeholder={floatingLabelPlaceholder}
/>
{postfixText}
{postfixIcon && cloneElement(postfixIcon, {
...postfixIcon.props,
onClick: (e) => {
const { onClick: iconClick = () => {} } = postfixIcon.props
iconClick(e)
handleButtonClick(e)
}
})}
</Wrapper>
</Container>
)
}
export default withForwardRef<EbayTextboxProps>(EbayTextbox)