Skip to content

Commit

Permalink
fix(Select): hight first item with remote datasource. Close #654
Browse files Browse the repository at this point in the history
  • Loading branch information
bindoon committed May 14, 2019
1 parent ebfc1a2 commit d784ee0
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 33 deletions.
24 changes: 23 additions & 1 deletion src/select/auto-complete.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,31 @@ class AutoComplete extends Base {
componentWillReceiveProps(nextProps) {
if ('value' in nextProps) {
this.dataStore.setOptions({ key: nextProps.value });
this.setState({
value: nextProps.value,
});
}

super.componentWillReceiveProps(nextProps);
if ('visible' in nextProps) {
this.setState({
visible: nextProps.visible,
});
}

this.dataStore.setOptions({
filter: nextProps.filter,
filterLocal: nextProps.filterLocal,
});

if (
nextProps.children !== this.props.children ||
nextProps.dataSource !== this.props.dataSource
) {
const dataSource = this.setDataSource(nextProps);
this.setState({
dataSource,
});
}

// remote dataSource and focused
// 因为autoComplete没有下拉数据不展示,搜索并且有数据了需要自动展示下拉
Expand Down
30 changes: 0 additions & 30 deletions src/select/base.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -175,36 +175,6 @@ export default class Base extends React.Component {
events.on(window, 'resize', this.handleResize);
}

componentWillReceiveProps(nextProps) {
if ('value' in nextProps) {
this.setState({
value: nextProps.value,
});
}

if ('visible' in nextProps) {
// this.state.visible = nextProps.visible;
this.setState({
visible: nextProps.visible,
});
}

this.dataStore.setOptions({
filter: nextProps.filter,
filterLocal: nextProps.filterLocal,
});

if (
nextProps.children !== this.props.children ||
nextProps.dataSource !== this.props.dataSource
) {
const dataSource = this.setDataSource(nextProps);
this.setState({
dataSource,
});
}
}

componentDidUpdate(prevProps, prevState) {
if (
prevProps.label !== this.props.label ||
Expand Down
40 changes: 38 additions & 2 deletions src/select/select.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,10 @@ class Select extends Base {
* 焦点事件
*/
onFocus: PropTypes.func,
/**
* 是否自动高亮第一个选项
*/
// highlightFirstItem: PropTypes.bool,
/**
* 失去焦点事件
*/
Expand All @@ -151,6 +155,7 @@ class Select extends Base {
onSearchClear: noop,
hasArrow: true,
onRemove: noop,
// highlightFirstItem: true,
valueRender: item => {
return item.label || item.value;
},
Expand Down Expand Up @@ -223,10 +228,35 @@ class Select extends Base {
});
}

super.componentWillReceiveProps(nextProps);
this.dataStore.setOptions({
filter: nextProps.filter,
filterLocal: nextProps.filterLocal,
});

if (
nextProps.children !== this.props.children ||
nextProps.dataSource !== this.props.dataSource
) {
const dataSource = this.setDataSource(nextProps);
this.setState({
dataSource,
});

// 远程数据有更新,并且还有搜索框
if (
nextProps.showSearch &&
!nextProps.filterLocal &&
!nextProps.popupContent
) {
this.setFirstHightLightKeyForMenu();
}
}

if ('value' in nextProps) {
// under controll
this.setState({
value: nextProps.value,
});

this.valueDataSource = getValueDataSource(
nextProps.value,
this.valueDataSource.mapValueDS,
Expand All @@ -246,6 +276,12 @@ class Select extends Base {
this.dataStore.getMapDS()
);
}

if ('visible' in nextProps) {
this.setState({
visible: nextProps.visible,
});
}
}

componentDidMount() {
Expand Down

0 comments on commit d784ee0

Please sign in to comment.