Skip to content

Commit

Permalink
fix(Select): adjust auto-complete menu length issue, close alibaba-fu…
Browse files Browse the repository at this point in the history
  • Loading branch information
zizairufengLT committed Aug 23, 2024
1 parent b300a60 commit 0674cc6
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 7 deletions.
17 changes: 16 additions & 1 deletion components/select/__tests__/index-spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -893,7 +893,7 @@ describe('AutoComplete', () => {
visible: true,
});
cy.get('input').should('have.value', 'yyy');
cy.get('.next-menu-item').should('have.length', 2);
cy.get('.next-menu-item').should('have.length', 1);
});

it('should support empty value from dataSource', () => {
Expand Down Expand Up @@ -1084,6 +1084,21 @@ describe('AutoComplete', () => {
});
});

it('should render currect dataSource when value changed', () => {
cy.rerender('Demo', {
value: 'xxx',
visible: true,
});
cy.get('input').should('have.value', 'xxx');
cy.get('.next-menu-item').should('have.length', 1);
cy.rerender('Demo', {
value: undefined,
visible: true,
});
cy.get('input').should('have.value', '');
cy.get('.next-menu-item').should('have.length', 2);
});

describe('react api', () => {
it('calls componentWillReceiveProps', () => {
cy.rerender('Demo', { value: '30' });
Expand Down
16 changes: 11 additions & 5 deletions components/select/auto-complete.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,6 @@ class AutoComplete extends Base<AutoCompleteProps, AutoCompleteState> {
componentDidUpdate(prevProps: AutoCompleteProps) {
const props = this.props;

if ('value' in props) {
this.dataStore.setOptions({ key: props.value });
}

if (props.filter !== prevProps.filter) {
this.dataStore.setOptions({
filter: props.filter,
Expand All @@ -103,9 +99,19 @@ class AutoComplete extends Base<AutoCompleteProps, AutoCompleteState> {
});
}

let dataSource;

if (prevProps.children !== props.children || prevProps.dataSource !== props.dataSource) {
dataSource = this.setDataSource(props);
}
if ('value' in props) {
if (prevProps.value !== props.value) {
dataSource = this.dataStore.updateByKey(props.value);
}
}
if (dataSource) {
this.setState({
dataSource: this.setDataSource(props),
dataSource,
});

// remote dataSource and focused
Expand Down
2 changes: 1 addition & 1 deletion components/select/data-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class DataStore {
return this.updateAll();
}

updateByKey(key: string) {
updateByKey(key?: string | number | null) {
if (key === this.options.key) {
return this.getMenuDS();
}
Expand Down

0 comments on commit 0674cc6

Please sign in to comment.