forked from react-component/select
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInput.tsx
More file actions
162 lines (145 loc) · 4.42 KB
/
Input.tsx
File metadata and controls
162 lines (145 loc) · 4.42 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
import * as React from 'react';
import classNames from 'classnames';
import { composeRef } from '@rc-component/util/lib/ref';
import { warning } from '@rc-component/util/lib/warning';
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,
disabled,
tabIndex,
autoFocus,
autoComplete,
editable,
activeDescendantId,
value,
maxLength,
onKeyDown,
onMouseDown,
onChange,
onPaste,
onCompositionStart,
onCompositionEnd,
onBlur,
open,
attrs,
} = props;
const { classNames: contextClassNames, styles: contextStyles } = useBaseProps() || {};
let inputNode: React.ComponentElement<any, any> = inputElement || <input />;
const { ref: originRef, props: originProps } = inputNode;
const {
onKeyDown: onOriginKeyDown,
onChange: onOriginChange,
onMouseDown: onOriginMouseDown,
onCompositionStart: onOriginCompositionStart,
onCompositionEnd: onOriginCompositionEnd,
onBlur: onOriginBlur,
style,
} = originProps;
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',
...originProps,
// Override over origin props
id,
ref: composeRef(ref, originRef as any),
disabled,
tabIndex,
autoComplete: autoComplete || 'off',
autoFocus,
className: classNames(
`${prefixCls}-selection-search-input`,
inputNode?.props?.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 : '',
maxLength,
readOnly: !editable,
unselectable: !editable ? 'on' : null,
style: { ...style, opacity: editable ? null : 0, ...contextStyles?.input },
onKeyDown: (event: React.KeyboardEvent<HTMLElement>) => {
onKeyDown(event);
if (onOriginKeyDown) {
onOriginKeyDown(event);
}
},
onMouseDown: (event: React.MouseEvent<HTMLElement>) => {
onMouseDown(event);
if (onOriginMouseDown) {
onOriginMouseDown(event);
}
},
onChange: (event: React.ChangeEvent<HTMLElement>) => {
onChange(event);
if (onOriginChange) {
onOriginChange(event);
}
},
onCompositionStart(event: React.CompositionEvent<HTMLElement>) {
onCompositionStart(event);
if (onOriginCompositionStart) {
onOriginCompositionStart(event);
}
},
onCompositionEnd(event: React.CompositionEvent<HTMLElement>) {
onCompositionEnd(event);
if (onOriginCompositionEnd) {
onOriginCompositionEnd(event);
}
},
onPaste,
onBlur(event: React.FocusEvent<HTMLElement>) {
onBlur(event);
if (onOriginBlur) {
onOriginBlur(event);
}
},
});
return inputNode;
};
const RefInput = React.forwardRef<InputRef, InputProps>(Input);
if (process.env.NODE_ENV !== 'production') {
RefInput.displayName = 'Input';
}
export default RefInput;