-
Notifications
You must be signed in to change notification settings - Fork 196
/
Copy pathBaseSelector.jsx
203 lines (174 loc) · 5.06 KB
/
BaseSelector.jsx
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
/**
* Input Box is in different position for different mode.
* This not the same design as `Select` cause it's followed by antd 0.x `Select`.
* We will not follow the new design immediately since antd 3.x is already released.
*
* So this file named as Selector to avoid confuse.
*/
import React from 'react';
import PropTypes from 'prop-types';
import classNames from 'classnames';
import { polyfill } from 'react-lifecycles-compat';
import { createRef } from '../util';
export const selectorPropTypes = {
prefixCls: PropTypes.string,
className: PropTypes.string,
style: PropTypes.object,
open: PropTypes.bool,
selectorValueList: PropTypes.array,
allowClear: PropTypes.bool,
showArrow: PropTypes.bool,
onClick: PropTypes.func,
onBlur: PropTypes.func,
onFocus: PropTypes.func,
removeSelected: PropTypes.func,
// Pass by component
ariaId: PropTypes.string,
inputIcon: PropTypes.oneOfType([PropTypes.node, PropTypes.func]),
clearIcon: PropTypes.oneOfType([PropTypes.node, PropTypes.func]),
};
export const selectorContextTypes = {
onSelectorFocus: PropTypes.func.isRequired,
onSelectorBlur: PropTypes.func.isRequired,
onSelectorKeyDown: PropTypes.func.isRequired,
onSelectorClear: PropTypes.func.isRequired,
};
export default function (modeName) {
class BaseSelector extends React.Component {
static propTypes = {
...selectorPropTypes,
// Pass by HOC
renderSelection: PropTypes.func.isRequired,
renderPlaceholder: PropTypes.func,
tabIndex: PropTypes.number,
};
static contextTypes = {
rcTreeSelect: PropTypes.shape({
...selectorContextTypes,
}),
};
static defaultProps = {
tabIndex: 0,
}
constructor() {
super();
this.domRef = createRef();
}
onFocus = (...args) => {
const { onFocus, focused } = this.props;
const { rcTreeSelect: { onSelectorFocus } } = this.context;
if (!focused) {
onSelectorFocus();
}
if (onFocus) {
onFocus(...args);
}
};
onBlur = (...args) => {
const { onBlur } = this.props;
const { rcTreeSelect: { onSelectorBlur } } = this.context;
// TODO: Not trigger when is inner component get focused
onSelectorBlur();
if (onBlur) {
onBlur(...args);
}
};
focus = () => {
this.domRef.current.focus();
}
blur = () => {
this.domRef.current.blur();
}
renderClear() {
const { prefixCls, allowClear, selectorValueList, clearIcon } = this.props;
const { rcTreeSelect: { onSelectorClear } } = this.context;
if (!allowClear || !selectorValueList.length || !selectorValueList[0].value) {
return null;
}
return (
<span
key="clear"
className={`${prefixCls}-selection__clear`}
onClick={onSelectorClear}
>
{typeof clearIcon === 'function' ?
React.createElement(clearIcon, { ...this.props }) : clearIcon}
</span>
);
}
renderArrow() {
const { prefixCls, showArrow, inputIcon } = this.props;
if (!showArrow) {
return null;
}
return (
<span
key="arrow"
className={`${prefixCls}-arrow`}
style={{ outline: 'none' }}
>
{typeof inputIcon === 'function' ?
React.createElement(inputIcon, { ...this.props }) : inputIcon}
</span>
);
}
render() {
const {
prefixCls, className, style,
open, focused, disabled, allowClear,
onClick,
ariaId,
renderSelection, renderPlaceholder,
tabIndex,
} = this.props;
const { rcTreeSelect: { onSelectorKeyDown } } = this.context;
let myTabIndex = tabIndex;
if (disabled) {
myTabIndex = null;
}
return (
<span
style={style}
onClick={onClick}
className={classNames(
className,
prefixCls,
{
[`${prefixCls}-open`]: open,
[`${prefixCls}-focused`]: open || focused,
[`${prefixCls}-disabled`]: disabled,
[`${prefixCls}-enabled`]: !disabled,
[`${prefixCls}-allow-clear`]: allowClear,
}
)}
ref={this.domRef}
role="combobox"
aria-expanded={open}
aria-owns={open ? ariaId : undefined}
aria-controls={open ? ariaId : undefined}
aria-haspopup="listbox"
aria-disabled={disabled}
tabIndex={myTabIndex}
onFocus={this.onFocus}
onBlur={this.onBlur}
onKeyDown={onSelectorKeyDown}
>
<span
key="selection"
className={classNames(
`${prefixCls}-selection`,
`${prefixCls}-selection--${modeName}`
)}
>
{renderSelection()}
{this.renderClear()}
{this.renderArrow()}
{renderPlaceholder && renderPlaceholder()}
</span>
</span>
);
}
}
polyfill(BaseSelector);
return BaseSelector;
}