-
-
Notifications
You must be signed in to change notification settings - Fork 489
Expand file tree
/
Copy pathSingleSelector.tsx
More file actions
136 lines (121 loc) · 3.5 KB
/
SingleSelector.tsx
File metadata and controls
136 lines (121 loc) · 3.5 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
import * as React from 'react';
import pickAttrs from 'rc-util/lib/pickAttrs';
import Input from './Input';
import type { InnerSelectorProps } from '.';
import { getTitle } from '../utils/commonUtil';
import SelectNativeInput from '../SelectNativeInput';
interface SelectorProps extends InnerSelectorProps {
inputElement: React.ReactElement;
activeValue: string;
}
const SingleSelector: React.FC<SelectorProps> = (props) => {
const {
inputElement,
prefixCls,
id,
inputRef,
disabled,
autoFocus,
autoComplete,
activeDescendantId,
mode,
open,
values,
placeholder,
tabIndex,
nativeInputProps,
showSearch,
searchValue,
activeValue,
maxLength,
onInputKeyDown,
onInputMouseDown,
onInputChange,
onInputPaste,
onInputCompositionStart,
onInputCompositionEnd,
title,
} = props;
const [inputChanged, setInputChanged] = React.useState(false);
const combobox = mode === 'combobox';
const inputEditable = combobox || showSearch;
const item = values[0];
let inputValue: string = searchValue || '';
if (combobox && activeValue && !inputChanged) {
inputValue = activeValue;
}
React.useEffect(() => {
if (combobox) {
setInputChanged(false);
}
}, [combobox, activeValue]);
// Not show text when closed expect combobox mode
const hasTextInput = mode !== 'combobox' && !open && !showSearch ? false : !!inputValue;
// Get title of selection item
const selectionTitle = title === undefined ? getTitle(item) : title;
const placeholderNode = React.useMemo<React.ReactNode>(() => {
if (item) {
return null;
}
return (
<span
className={`${prefixCls}-selection-placeholder`}
style={hasTextInput ? { visibility: 'hidden' } : undefined}
>
{placeholder}
</span>
);
}, [item, hasTextInput, placeholder, prefixCls]);
return (
<>
<span className={`${prefixCls}-selection-search`}>
<Input
ref={inputRef}
prefixCls={prefixCls}
id={id}
open={open}
inputElement={inputElement}
disabled={disabled}
autoFocus={autoFocus}
autoComplete={autoComplete}
editable={inputEditable}
activeDescendantId={activeDescendantId}
value={inputValue}
onKeyDown={onInputKeyDown}
onMouseDown={onInputMouseDown}
onChange={(e) => {
setInputChanged(true);
onInputChange(e as any);
}}
onPaste={onInputPaste}
onCompositionStart={onInputCompositionStart}
onCompositionEnd={onInputCompositionEnd}
tabIndex={tabIndex}
attrs={pickAttrs(props, true)}
maxLength={combobox ? maxLength : undefined}
/>
</span>
<SelectNativeInput
value={item?.value}
prefixCls={prefixCls}
{...nativeInputProps}
/>
{/* Display value */}
{!combobox && item ? (
<span
className={`${prefixCls}-selection-item`}
title={selectionTitle}
// 当 Select 已经选中选项时,还需 selection 隐藏但留在原地占位
// https://github.com/ant-design/ant-design/issues/27688
// https://github.com/ant-design/ant-design/issues/41530
style={hasTextInput ? { visibility: 'hidden' } : undefined}
>
{item.label}
</span>
) : null}
{/* Display placeholder */}
{placeholderNode}
</>
);
};
export default SingleSelector;