Skip to content

Commit

Permalink
Merge pull request #71 from oceanic815recovery/fix_nested_select
Browse files Browse the repository at this point in the history
Fix Select when it is in an array
  • Loading branch information
stevehu authored Oct 20, 2017
2 parents 4dfa187 + 37d059a commit e2be2bf
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
18 changes: 15 additions & 3 deletions lib/Select.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,9 @@ var Select = function (_React$Component) {
var _this = _possibleConstructorReturn(this, (Select.__proto__ || Object.getPrototypeOf(Select)).call(this, props));

_this.onSelected = _this.onSelected.bind(_this);
var possibleValue = _this.getModelKey(_this.props.model, _this.props.form.key);
_this.state = {
currentValue: _this.props.model !== undefined ? _this.props.model[_this.props.form.key] : _this.props.form.titleMap != null ? _this.props.form.titleMap[0].value : ''
currentValue: _this.props.model !== undefined && possibleValue ? possibleValue : _this.props.form.titleMap != null ? _this.props.form.titleMap[0].value : ''
};
return _this;
}
Expand All @@ -53,10 +54,21 @@ var Select = function (_React$Component) {
value: function componentWillReceiveProps(nextProps) {
if (nextProps.model && nextProps.form.key) {
this.setState({
currentValue: nextProps.model[nextProps.form.key] || (nextProps.form.titleMap != null ? nextProps.form.titleMap[0].value : '')
currentValue: this.getModelKey(nextProps.model, nextProps.form.key) || (nextProps.form.titleMap != null ? nextProps.form.titleMap[0].value : '')
});
}
}
}, {
key: 'getModelKey',
value: function getModelKey(model, key) {
if (Array.isArray(key)) {
return key.reduce(function (cur, nxt) {
return cur[nxt];
}, model);
} else {
return model[key];
}
}
}, {
key: 'onSelected',
value: function onSelected(event, selectedIndex, menuItem) {
Expand Down Expand Up @@ -115,4 +127,4 @@ var _temp = function () {
__REACT_HOT_LOADER__.register(_default, 'default', 'src/Select.js');
}();

;
;
13 changes: 11 additions & 2 deletions src/Select.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,29 @@ class Select extends React.Component {
constructor(props) {
super(props);
this.onSelected = this.onSelected.bind(this);
var possibleValue = this.getModelKey(this.props.model, this.props.form.key);
this.state = {
currentValue: this.props.model !== undefined ? this.props.model[this.props.form.key] : (this.props.form.titleMap != null ? this.props.form.titleMap[0].value : '')
currentValue: this.props.model !== undefined && possibleValue ? possibleValue : this.props.form.titleMap != null ? this.props.form.titleMap[0].value : ''
};
}

componentWillReceiveProps(nextProps) {
if (nextProps.model && nextProps.form.key) {
this.setState({
currentValue: nextProps.model[nextProps.form.key]
currentValue: this.getModelKey(nextProps.model, nextProps.form.key)
|| (nextProps.form.titleMap != null ? nextProps.form.titleMap[0].value : '')
});
}
}

getModelKey(model, key) {
if (Array.isArray(key)) {
return key.reduce((cur, nxt) => (cur[nxt]), model);
} else {
return model[key];
}
}

onSelected(event, selectedIndex, menuItem) {

this.setState({
Expand Down

0 comments on commit e2be2bf

Please sign in to comment.