Skip to content

Commit ab05705

Browse files
committed
feat(externalOptionSet) added method to externally set option on the component via ref
1 parent 8e75c69 commit ab05705

1 file changed

Lines changed: 12 additions & 12 deletions

File tree

src/SimpleSelect.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ class SimpleSelect extends React.Component {
2020
this.handleKeyboard = this.handleKeyboard.bind(this);
2121
this.renderOption = this.renderOption.bind(this);
2222
this.renderOptionLabel = this.renderOptionLabel.bind(this);
23+
this.setOption = this.setOption.bind(this);
2324

2425
const optionValidation = this.checkOptionType(props.options);
2526

@@ -136,13 +137,7 @@ class SimpleSelect extends React.Component {
136137
if (this.props.onChange) {
137138
this.props.onChange(e, optionByIndex.value, optionByIndex);
138139
}
139-
this.setState({
140-
...this.state,
141-
currentOptionSelected: optionByIndex,
142-
currentOptionIndex: optionIndex,
143-
focusedOptionIndex: 0,
144-
selectOpen: false
145-
});
140+
this.setOption(optionByIndex, optionIndex);
146141
}
147142
}
148143
}
@@ -159,12 +154,17 @@ class SimpleSelect extends React.Component {
159154
if (this.props.onChange) {
160155
this.props.onChange(e, optionByIndex.value, optionByIndex);
161156
}
157+
this.setOption(optionByIndex, optionIndex);
158+
}
159+
160+
setOption(option, knownIndex) {
161+
const index = (knownIndex) ? knownIndex : this.state.options.findIndex(opt => opt.value === option.value && opt.label === option.label);
162162
this.setState({
163163
...this.state,
164-
currentOptionSelected: optionByIndex,
165-
currentOptionIndex: optionIndex,
166-
focusedOptionIndex: 0,
167-
selectOpen: false
164+
currentOptionSelected: option,
165+
currentOptionIndex: index,
166+
focusedOptionIndex: 0, //reset focus
167+
selectOpen: false //resetOpen
168168
});
169169
}
170170

@@ -190,7 +190,7 @@ class SimpleSelect extends React.Component {
190190
}
191191

192192
renderOptionLabel(opt) {
193-
if (this.props.renderOptionLabel && opt != this.state.blankValue) {
193+
if (this.props.renderOptionLabel && opt !== this.state.blankValue) {
194194
return this.props.renderOptionLabel(opt);
195195
} else {
196196
return opt.label;

0 commit comments

Comments
 (0)