Skip to content

Commit 281c339

Browse files
authored
Merge pull request #65 from jordanbyron/fix-allow-clear
Fix allowClear option [Closes #63]
2 parents 3df210d + 1595364 commit 281c339

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

examples/src/components/Tags.js

+30-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,13 @@ export default class Tags extends Component {
2626
{ text: 'documents', id: 3 },
2727
{ text: 'discussion', id: 4 },
2828
],
29+
value11: 1,
30+
data11: [
31+
{ text: 'bug', id: 1 },
32+
{ text: 'feature', id: 2 },
33+
{ text: 'documents', id: 3 },
34+
{ text: 'discussion', id: 4 },
35+
],
2936
placeholder9: 'search by tags',
3037
};
3138
}
@@ -287,6 +294,28 @@ export default class Tags extends Component {
287294
);
288295
}
289296

297+
example11() {
298+
const { value11, data11 } = this.state;
299+
return (
300+
<div>
301+
Add a new item and set a new `onChange` event<br/>
302+
<Select2
303+
multiple={false}
304+
data={data11}
305+
defaultValue={ 1 }
306+
value={ value11 }
307+
onChange={(e) => { this.setState({ value11: e.target.value }); }}
308+
options={{
309+
placeholder: 'search by tags',
310+
allowClear: true,
311+
}}
312+
/>
313+
314+
Click "x" to clear
315+
</div>
316+
);
317+
}
318+
290319
render() {
291320
return (
292321
<div>
@@ -299,7 +328,7 @@ export default class Tags extends Component {
299328
{this.example7()}<br/>
300329
{this.example8()}<br/>
301330
{this.example9()}<br/>
302-
{this.example10()}<br/>
331+
{this.example11()}<br/>
303332
</div>
304333
);
305334
}

src/components/Select2.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ export default class Select2 extends Component {
105105
const newValue = this.prepareValue(value, defaultValue);
106106
const currentValue = multiple ? this.el.val() || [] : this.el.val();
107107

108-
if (!shallowEqualFuzzy(currentValue, newValue) || this.forceUpdateValue) {
108+
if (!this.fuzzyValuesEqual(currentValue, newValue) || this.forceUpdateValue) {
109109
const onChange = this.props.onChange;
110110

111111
if (this.initialRender && onChange) {
@@ -121,6 +121,11 @@ export default class Select2 extends Component {
121121
}
122122
}
123123

124+
fuzzyValuesEqual(currentValue, newValue) {
125+
return (currentValue === null && newValue === '') ||
126+
shallowEqualFuzzy(currentValue, newValue);
127+
}
128+
124129
destroySelect2(withCallbacks = true) {
125130
if (withCallbacks) {
126131
this.detachEventHandlers();

0 commit comments

Comments
 (0)