-
Notifications
You must be signed in to change notification settings - Fork 221
Expand file tree
/
Copy pathinternal.tsx
More file actions
259 lines (234 loc) · 8.52 KB
/
internal.tsx
File metadata and controls
259 lines (234 loc) · 8.52 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
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
import React, { Ref, useRef } from 'react';
import clsx from 'clsx';
import { useMergeRefs } from '@cloudscape-design/component-toolkit/internal';
import {
copyAnalyticsMetadataAttribute,
getAnalyticsMetadataAttribute,
} from '@cloudscape-design/component-toolkit/internal/analytics-metadata';
import InternalButton from '../button/internal';
import { useInternalI18n } from '../i18n/context';
import { IconProps } from '../icon/interfaces';
import InternalIcon from '../icon/internal';
import { BaseComponentProps, getBaseProps } from '../internal/base-component';
import { FormFieldValidationControlProps, useFormFieldContext } from '../internal/context/form-field-context';
import { fireKeyboardEvent, fireNonCancelableEvent, NonCancelableEventHandler } from '../internal/events';
import { InternalBaseComponentProps } from '../internal/hooks/use-base-component';
import { useDebounceCallback } from '../internal/hooks/use-debounce-callback';
import { useIMEComposition } from '../internal/hooks/use-ime-composition';
import WithNativeAttributes, { SkipWarnings } from '../internal/utils/with-native-attributes';
import {
GeneratedAnalyticsMetadataInputClearInput,
GeneratedAnalyticsMetadataInputComponent,
} from './analytics-metadata/interfaces';
import { BaseChangeDetail, BaseInputProps, InputAutoCorrect, InputProps } from './interfaces';
import { getInputStyles } from './styles';
import { convertAutoComplete, useSearchProps } from './utils';
import styles from './styles.css.js';
export interface InternalInputProps
extends BaseComponentProps,
BaseInputProps,
Omit<InputProps, 'type'>,
InputAutoCorrect,
FormFieldValidationControlProps,
InternalBaseComponentProps {
type?: InputProps['type'] | 'visualSearch';
__leftIcon?: IconProps['name'];
__leftIconVariant?: IconProps['variant'];
__onLeftIconClick?: () => void;
__rightIcon?: IconProps['name'];
__onRightIconClick?: () => void;
__noBorderRadius?: boolean;
__onDelayedInput?: NonCancelableEventHandler<BaseChangeDetail>;
__onBlurWithDetail?: NonCancelableEventHandler<{ relatedTarget: Node | null }>;
__inheritFormFieldProps?: boolean;
__injectAnalyticsComponentMetadata?: boolean;
__skipNativeAttributesWarnings?: SkipWarnings;
}
function InternalInput(
{
type = 'text',
step,
inputMode,
autoComplete = true,
ariaLabel,
clearAriaLabel: clearAriaLabelOverride,
name,
value,
placeholder,
autoFocus,
disabled,
readOnly,
disableBrowserAutocorrect,
spellcheck,
__noBorderRadius,
__leftIcon,
__leftIconVariant = 'subtle',
__onLeftIconClick,
ariaRequired,
__rightIcon,
__onRightIconClick,
onKeyDown,
onKeyUp,
onChange,
__onDelayedInput,
__onBlurWithDetail,
onBlur,
onFocus,
nativeInputAttributes,
__internalRootRef,
__inheritFormFieldProps,
__injectAnalyticsComponentMetadata,
__skipNativeAttributesWarnings,
style,
...rest
}: InternalInputProps,
ref: Ref<HTMLInputElement>
) {
const baseProps = getBaseProps(rest);
const i18n = useInternalI18n('input');
const fireDelayedInput = useDebounceCallback((value: string) => fireNonCancelableEvent(__onDelayedInput, { value }));
const handleChange = (value: string) => {
fireDelayedInput(value);
fireNonCancelableEvent(onChange, { value });
};
const inputRef = useRef<HTMLInputElement>(null);
const { isComposing } = useIMEComposition(inputRef);
const searchProps = useSearchProps(type, disabled, readOnly, value, inputRef, handleChange);
__leftIcon = __leftIcon ?? searchProps.__leftIcon;
__rightIcon = __rightIcon ?? searchProps.__rightIcon;
__onRightIconClick = __onRightIconClick ?? searchProps.__onRightIconClick;
const formFieldContext = useFormFieldContext(rest);
const { ariaLabelledby, ariaDescribedby, controlId, invalid, warning } = __inheritFormFieldProps
? formFieldContext
: rest;
const attributes: React.InputHTMLAttributes<HTMLInputElement> = {
'aria-label': ariaLabel,
// aria-labelledby has precedence over aria-label in accessible name calculation.
// When aria-label is provided for Input, it should override aria-labelledBy from form-field context.
// If both aria-label and aria-labelledby come from Input props, aria-labelledby will be used in accessible name
'aria-labelledby': ariaLabel && !rest.ariaLabelledby ? undefined : ariaLabelledby,
'aria-describedby': ariaDescribedby,
name,
placeholder,
autoFocus,
id: controlId,
className: clsx(
styles.input,
type && styles[`input-type-${type}`],
__rightIcon && styles['input-has-icon-right'],
__leftIcon && styles['input-has-icon-left'],
__noBorderRadius && styles['input-has-no-border-radius'],
{
[styles['input-readonly']]: readOnly,
[styles['input-invalid']]: invalid,
[styles['input-warning']]: warning && !invalid,
}
),
autoComplete: convertAutoComplete(autoComplete),
disabled,
readOnly,
type,
step,
inputMode,
spellCheck: spellcheck,
onKeyDown:
onKeyDown &&
(event => {
// Prevent keydown event during IME composition to avoid race conditions
if (isComposing() && event.key === 'Enter') {
event.preventDefault();
return;
}
fireKeyboardEvent(onKeyDown, event);
}),
onKeyUp: onKeyUp && (event => fireKeyboardEvent(onKeyUp, event)),
// We set a default value on the component in order to force it into the controlled mode.
value: value ?? '',
onChange: onChange && (event => handleChange(event.target.value)),
onBlur: e => {
fireNonCancelableEvent(onBlur);
fireNonCancelableEvent(__onBlurWithDetail, { relatedTarget: e.relatedTarget });
},
onFocus: onFocus && (() => fireNonCancelableEvent(onFocus)),
};
if (type === 'number') {
// Chrome and Safari have a weird built-in behavior of letting focused
// number inputs be controlled by scrolling on them. However, they don't
// lock the browser's scroll, so it's very easy to accidentally increment
// the input while scrolling down the page.
attributes.onWheel = event => event.currentTarget.blur();
}
if (disableBrowserAutocorrect) {
attributes.autoCorrect = 'off';
attributes.autoCapitalize = 'off';
}
// ensure aria properties are string literal "true"
if (ariaRequired) {
attributes['aria-required'] = 'true';
}
if (invalid) {
attributes['aria-invalid'] = 'true';
}
const mergedRef = useMergeRefs(ref, inputRef);
// type = "visualSearch" renders a type="text' input
if (attributes.type === 'visualSearch') {
attributes.type = 'text';
}
const componentAnalyticsMetadata: GeneratedAnalyticsMetadataInputComponent = {
name: 'awsui.Input',
label: 'input',
properties: {
value: value || '',
},
};
return (
<div
{...baseProps}
className={clsx(baseProps.className, styles['input-container'])}
ref={__internalRootRef}
dir={type === 'email' ? 'ltr' : undefined}
{...(__injectAnalyticsComponentMetadata
? getAnalyticsMetadataAttribute({ component: componentAnalyticsMetadata })
: copyAnalyticsMetadataAttribute(rest))}
>
{__leftIcon && (
<span onClick={__onLeftIconClick} className={styles['input-icon-left']}>
<InternalIcon name={__leftIcon} variant={disabled || readOnly ? 'disabled' : __leftIconVariant} />
</span>
)}
<WithNativeAttributes
{...attributes}
tag="input"
componentName="Input"
nativeAttributes={nativeInputAttributes}
skipWarnings={__skipNativeAttributesWarnings}
ref={mergedRef}
style={getInputStyles(style)}
/>
{__rightIcon && (
<span
className={styles['input-icon-right']}
{...(__rightIcon === 'close'
? getAnalyticsMetadataAttribute({
action: 'clearInput',
} as Partial<GeneratedAnalyticsMetadataInputClearInput>)
: {})}
>
<InternalButton
// Used for test utils
className={styles['input-button-right']}
variant="inline-icon-pointer-target"
formAction="none"
iconName={__rightIcon}
onClick={__onRightIconClick}
ariaLabel={i18n('clearAriaLabel', clearAriaLabelOverride)}
disabled={disabled}
/>
</span>
)}
</div>
);
}
export default React.forwardRef(InternalInput);