-
-
Notifications
You must be signed in to change notification settings - Fork 490
Expand file tree
/
Copy pathInput.tsx
More file actions
107 lines (90 loc) · 3.06 KB
/
Input.tsx
File metadata and controls
107 lines (90 loc) · 3.06 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
import * as React from 'react';
import { clsx } from 'clsx';
import { composeRef } from '@rc-component/util/lib/ref';
import { warning } from '@rc-component/util/lib/warning';
import composeProps from '@rc-component/util/lib/composeProps';
import useBaseProps from '../hooks/useBaseProps';
type InputRef = HTMLInputElement | HTMLTextAreaElement;
interface InputProps {
prefixCls: string;
id: string;
inputElement: React.ReactElement;
disabled: boolean;
autoFocus: boolean;
autoComplete: string;
editable: boolean;
activeDescendantId?: string;
value: string;
maxLength?: number;
open: boolean;
tabIndex: number;
/** Pass accessibility props to input */
attrs: Record<string, unknown>;
onKeyDown: React.KeyboardEventHandler<HTMLInputElement | HTMLTextAreaElement | HTMLElement>;
onMouseDown: React.MouseEventHandler<HTMLInputElement | HTMLTextAreaElement | HTMLElement>;
onChange: React.ChangeEventHandler<HTMLInputElement | HTMLTextAreaElement | HTMLElement>;
onPaste: React.ClipboardEventHandler<HTMLInputElement | HTMLTextAreaElement | HTMLElement>;
onBlur: React.FocusEventHandler<HTMLInputElement | HTMLTextAreaElement | HTMLElement>;
onCompositionStart: React.CompositionEventHandler<
HTMLInputElement | HTMLTextAreaElement | HTMLElement
>;
onCompositionEnd: React.CompositionEventHandler<
HTMLInputElement | HTMLTextAreaElement | HTMLElement
>;
}
const Input: React.ForwardRefRenderFunction<InputRef, InputProps> = (props, ref) => {
const {
prefixCls,
id,
inputElement,
autoComplete,
editable,
activeDescendantId,
value,
open,
attrs,
...restProps
} = props;
const { classNames: contextClassNames, styles: contextStyles } = useBaseProps() || {};
let inputNode: React.ComponentElement<any, any> = inputElement || <input />;
const { ref: originRef, props: originProps } = inputNode;
warning(
!('maxLength' in inputNode.props),
`Passing 'maxLength' to input element directly may not work because input in BaseSelect is controlled.`,
);
inputNode = React.cloneElement(inputNode, {
type: 'search',
...composeProps(restProps, originProps, true),
// Override over origin props
id,
ref: composeRef(ref, originRef as any),
autoComplete: autoComplete || 'off',
className: clsx(
`${prefixCls}-selection-search-input`,
originProps.className,
contextClassNames?.input,
),
role: 'combobox',
'aria-expanded': open || false,
'aria-haspopup': 'listbox',
'aria-owns': `${id}_list`,
'aria-autocomplete': 'list',
'aria-controls': `${id}_list`,
'aria-activedescendant': open ? activeDescendantId : undefined,
...attrs,
value: editable ? value : '',
readOnly: !editable,
unselectable: !editable ? 'on' : null,
style: {
...originProps.style,
opacity: editable ? null : 0,
...contextStyles?.input,
},
});
return inputNode;
};
const RefInput = React.forwardRef<InputRef, InputProps>(Input);
if (process.env.NODE_ENV !== 'production') {
RefInput.displayName = 'Input';
}
export default RefInput;