Repository navigation
Expand file tree
/
Copy pathtextbox.tsx
More file actions
293 lines (264 loc) · 9.19 KB
/
Copy pathtextbox.tsx
File metadata and controls
293 lines (264 loc) · 9.19 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
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
import { useState, useCallback, useEffect, forwardRef, useImperativeHandle, useRef, useMemo, ReactNode, InputHTMLAttributes,
ForwardRefExoticComponent, RefAttributes, ForwardedRef, RefObject, type ChangeEvent, type FocusEvent
} from 'react';
import { preRender, useProviderContext, Variant, Size, useStableId } from '@syncfusion/react-base';
import { CLASS_NAMES, LabelMode, InputBase, renderClearButton, renderFloatLabelElement, inputBaseProps, renderAdornmentElement } from '../common/inputbase';
import { HelperText } from '../common/helper-text';
export { LabelMode, Variant, Size };
export interface TextBoxChangeEvent {
/**
* Specifies the initial event object received from the input element.
*/
event?: ChangeEvent<HTMLInputElement>;
/**
* Specifies the current value of the TextBox.
*/
value?: string;
}
/**
* Specifies the available color schemes for the component.
*
* @enum {string}
*/
export enum Color {
/** Success color scheme (typically green) for positive actions or status */
Success = 'Success',
/** Warning color scheme (typically yellow/amber) for cautionary actions or status */
Warning = 'Warning',
/** Error color scheme (typically red) for negative actions or status */
Error = 'Error'
}
export interface TextBoxProps extends inputBaseProps {
/**
* Specifies the value of the component. When provided, the component will be controlled.
*
* @default -
*/
value?: string;
/**
* Specifies the default value of the component. Used for uncontrolled mode.
*
* @default -
*/
defaultValue?: string;
/**
* Specifies the floating label type for the component.
*
* @default 'Never'
*/
labelMode?: LabelMode;
/**
* Specifies the placeholder text for the component.
*
* @default -
*/
placeholder?: string;
/**
* Specifies whether to display a clear button within the TextBox.
* When enabled, a clear icon appears in the TextBox that allows users
* to clear the input value with a single click.
*
* @default false
*/
clearButton?: ReactNode;
/**
* Specifies the Callback that fired when the input value is changed.
*
* @event onChange
* @returns {void}
*/
onChange?: (event: TextBoxChangeEvent) => void;
/**
* Specifies the Color style of the TextBox. Options include 'Warning', 'Success' and 'Error'.
*
* @default -
*/
color?: Color;
}
export interface ITextBox extends TextBoxProps {
/**
* Specifies the TextBox component element.
*
* @private
* @default null
*/
element?: HTMLInputElement | null;
}
type ITextBoxProps = TextBoxProps & Omit<InputHTMLAttributes<HTMLInputElement>, keyof TextBoxProps>;
/**
* TextBox component that provides a standard text input with extended functionality.
* Supports both controlled and uncontrolled modes based on presence of value or defaultValue prop.
*
* ```typescript
* import { TextBox } from "@syncfusion/react-inputs";
*
* export default function App() {
* return (
* <TextBox defaultValue="Initial text" placeholder="Enter text" />
* );
* }
* ```
*/
export const TextBox: ForwardRefExoticComponent<ITextBoxProps & RefAttributes<ITextBox>> =
forwardRef<ITextBox, ITextBoxProps>((props: ITextBoxProps, ref: ForwardedRef<ITextBox>) => {
const {
disabled = false,
onChange,
onBlur,
onFocus,
clearButton = false,
labelMode = 'Never',
className = '',
id,
readOnly = false,
value,
defaultValue,
width,
placeholder = '',
variant,
size = Size.Medium,
color,
prefix,
suffix,
helperText,
helperTextOnFocus = false,
helperTextDirection = 'Left',
...rest
} = props;
const reactId: string = useStableId('sf-textbox');
const textboxId: string = id ?? reactId;
const isControlled: boolean = value !== undefined;
const [inputValue, setValue] = useState<string | undefined>(
isControlled ? value : (defaultValue || '')
);
const [isFocused, setIsFocused] = useState(false);
const inputRef: RefObject<HTMLInputElement | null> = useRef<HTMLInputElement>(null);
const { locale, dir } = useProviderContext();
const publicAPI: Partial<ITextBoxProps> = {
clearButton,
labelMode,
disabled,
readOnly,
variant,
size,
color,
value,
helperText,
helperTextOnFocus,
helperTextDirection
};
const classNames: (...classes: string[]) => string = (...classes: string[]) => {
return classes.filter(Boolean).join(' ');
};
const containerClassNames: string = useMemo(() => classNames(
CLASS_NAMES.INPUTGROUP,
CLASS_NAMES.WRAPPER,
labelMode !== 'Never' ? CLASS_NAMES.FLOATINPUT : '',
className,
(dir === 'rtl') ? CLASS_NAMES.RTL : '',
disabled ? CLASS_NAMES.DISABLE : '',
readOnly ? CLASS_NAMES.READONLY : '',
isFocused ? CLASS_NAMES.TEXTBOX_FOCUS : '',
((inputValue) !== '') ? CLASS_NAMES.VALIDINPUT : '',
variant && variant.toLowerCase() !== 'standard' ? variant.toLowerCase() === 'outlined' ? 'sf-outline' : `sf-${variant.toLowerCase()}` : '',
size && size.toLowerCase() !== 'small' ? `sf-${size.toLowerCase()}` : '',
color ? `sf-${color.toLowerCase()}` : '',
prefix ? 'sf-has-prefix' : '',
suffix ? 'sf-has-suffix' : '',
'sf-control'
), [labelMode, className, dir, disabled, readOnly, isFocused, inputValue, variant, size, color, prefix, suffix]);
useEffect(() => {
preRender('textbox');
}, []);
useEffect(() => {
if (isControlled) {
setValue(value || '');
}
}, [value, isControlled]);
useImperativeHandle(ref, () => ({
...publicAPI as ITextBox,
element: inputRef.current
}));
const updateValue: (newValue: string, event?: ChangeEvent<HTMLInputElement>) => void =
useCallback((newValue: string, event?: ChangeEvent<HTMLInputElement>) => {
if (!isControlled) {
setValue(newValue);
}
if (onChange) {
onChange({ event: event, value: newValue });
}
}, [onChange, isControlled]);
const changeHandler: (event: ChangeEvent<HTMLInputElement>) => void =
useCallback((event: ChangeEvent<HTMLInputElement>) => {
const newValue: string = event.target.value;
updateValue(newValue, event);
}, [updateValue]);
const handleFocus: (event: FocusEvent<HTMLInputElement>) => void =
useCallback((event: FocusEvent<HTMLInputElement>) => {
setIsFocused(true);
if (onFocus) {
onFocus(event);
}
}, [onFocus]);
const clearInput: () => void = useCallback(() => {
const newValue: string = '';
if (!isControlled) {
setValue(newValue);
if (inputRef.current) {
inputRef.current.value = newValue;
}
}
if (onChange) {
onChange({ value: newValue, event: undefined });
}
}, [isControlled, onChange]);
const handleBlur: (event: FocusEvent<HTMLInputElement>) => void =
useCallback((event: FocusEvent<HTMLInputElement>) => {
setIsFocused(false);
if (onBlur) {
onBlur(event);
}
}, [onBlur]);
const displayValue: string | undefined = isControlled ? value : inputValue;
return (
<>
<div
className={containerClassNames}
style={{ width: width || '100%' }}
>
{renderAdornmentElement(prefix)}
<InputBase
id={textboxId}
floatLabelType={labelMode}
ref={inputRef as RefObject<HTMLInputElement>}
{...rest}
readOnly={readOnly}
value={isControlled ? (displayValue) : undefined}
defaultValue={!isControlled ? (inputValue || '') : undefined}
disabled={disabled}
placeholder={labelMode === 'Never' ? placeholder : undefined}
className={'sf-textbox sf-lib sf-ellipsis'}
onChange={changeHandler}
aria-label={labelMode === 'Never' ? 'textbox' : undefined}
onFocus={handleFocus}
onBlur={handleBlur}
/>
{renderFloatLabelElement(
labelMode || 'Never',
isFocused || (displayValue) !== '',
(displayValue as string),
placeholder,
textboxId
)}
{clearButton && renderClearButton((isFocused ? displayValue as string : ''), clearInput, clearButton, 'textbox', locale)}
{renderAdornmentElement(suffix)}
</div>
<HelperText
helperText={helperText}
helperTextOnFocus={helperTextOnFocus}
isFocused={isFocused}
helperTextDirection={helperTextDirection}
/>
</>
);
});