Skip to content

Commit 6e138bd

Browse files
committed
Fix IE and Edge search input width, close #65
1 parent 0e526f2 commit 6e138bd

File tree

2 files changed

+32
-7
lines changed

2 files changed

+32
-7
lines changed

assets/select.less

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,14 @@
136136
color: #aaa;
137137
}
138138

139+
&-search__field__mirror {
140+
position: absolute;
141+
top: 0;
142+
left: -9999px;
143+
white-space: pre;
144+
pointer-events: none;
145+
}
146+
139147
&-search--inline {
140148
float: left;
141149
width: 100%;

src/Select.jsx

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ class Select extends Component {
114114
// inputValue = value.length ? String(value[0].value) : '';
115115
// }
116116
this.saveInputRef = saveRef.bind(this, 'inputInstance');
117+
this.saveInputMirrorRef = saveRef.bind(this, 'inputMirrorInstance');
117118
this.state = {
118119
value,
119120
inputValue,
@@ -123,11 +124,13 @@ class Select extends Component {
123124
}
124125

125126
componentDidMount() {
126-
if (this.state.inputValue) {
127+
if (isMultipleOrTags(this.props)) {
127128
const inputNode = this.getInputDOMNode();
128-
if (inputNode && inputNode.value) {
129+
if (inputNode.value) {
130+
inputNode.style.width = '';
131+
inputNode.style.width = `${this.inputMirrorInstance.clientWidth}px`;
132+
} else {
129133
inputNode.style.width = '';
130-
inputNode.style.width = `${inputNode.scrollWidth}px`;
131134
}
132135
}
133136
}
@@ -189,7 +192,7 @@ class Select extends Component {
189192
const inputNode = this.getInputDOMNode();
190193
if (inputNode.value) {
191194
inputNode.style.width = '';
192-
inputNode.style.width = `${inputNode.scrollWidth}px`;
195+
inputNode.style.width = `${this.inputMirrorInstance.clientWidth}px`;
193196
} else {
194197
inputNode.style.width = '';
195198
}
@@ -441,17 +444,31 @@ class Select extends Component {
441444

442445
getInputElement() {
443446
const { inputValue } = this.state;
447+
const { prefixCls, disabled } = this.props;
444448
return (
445-
<span className={`${this.props.prefixCls}-search__field__wrap`}>
449+
<span className={`${prefixCls}-search__field__wrap`}>
446450
<input
447451
ref={this.saveInputRef}
448452
onChange={this.onInputChange}
449453
onKeyDown={this.onInputKeyDown}
450454
value={inputValue}
451-
disabled={this.props.disabled}
452-
className={`${this.props.prefixCls}-search__field`}
455+
disabled={disabled}
456+
className={`${prefixCls}-search__field`}
453457
role="textbox"
454458
/>
459+
<span
460+
ref={this.saveInputMirrorRef}
461+
className={`${prefixCls}-search__field__mirror`}
462+
style={{
463+
position: 'absolute',
464+
top: '0',
465+
left: '-9999px',
466+
whiteSpace: 'pre',
467+
pointerEvents: 'none',
468+
}}
469+
>
470+
{inputValue}&nbsp;
471+
</span>
455472
{isMultipleOrTags(this.props) ? null : this.getSearchPlaceholderElement(!!inputValue)}
456473
</span>
457474
);

0 commit comments

Comments
 (0)