From c007f903cd59a207f0b85953fd93325005c90e3d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=AF=B8=E5=B2=B3?= Date: Tue, 9 Jul 2019 11:19:55 +0800 Subject: [PATCH] fix(Cascader): Should show not found content when options.length is 0, close #17513 --- .../__snapshots__/index.test.js.snap | 291 ++++++++++++++++++ components/cascader/__tests__/index.test.js | 13 + components/cascader/index.tsx | 18 +- 3 files changed, 319 insertions(+), 3 deletions(-) diff --git a/components/cascader/__tests__/__snapshots__/index.test.js.snap b/components/cascader/__tests__/__snapshots__/index.test.js.snap index 908299371233..0e25e061ab66 100644 --- a/components/cascader/__tests__/__snapshots__/index.test.js.snap +++ b/components/cascader/__tests__/__snapshots__/index.test.js.snap @@ -1256,6 +1256,297 @@ exports[`Cascader should render not found content 1`] = ` `; +exports[`Cascader should show not found content when options.length is 0 1`] = ` + +
+ + + + +
+ + + } + expandTrigger="click" + fieldNames={ + Object { + "children": "children", + "label": "label", + "value": "value", + } + } + loadingIcon={ + + + + } + onChange={[Function]} + onItemDoubleClick={[Function]} + onPopupVisibleChange={[Function]} + onSelect={[Function]} + options={ + Array [ + Object { + "disabled": true, + "label": + [Function] + , + "value": "ANT_CASCADER_NOT_FOUND", + }, + ] + } + placeholder="Please select" + popupClassName="" + popupPlacement="bottomLeft" + popupVisible={true} + prefixCls="ant-cascader" + transitionName="slide-up" + value={Array []} + visible={true} + > +
+
    +
  • + + +
    +
    + No Data +
    +

    + No Data +

    +
    +
    +
    +
  • +
+
+
+
+
+
+
+
+
+
+
+`; + exports[`Cascader support controlled mode 1`] = ` { errorSpy.mockReset(); }); + it('should show not found content when options.length is 0', () => { + const customerOptions = []; + const wrapper = mount(); + wrapper.find('input').simulate('click'); + const popupWrapper = mount( + wrapper + .find('Trigger') + .instance() + .getComponent(), + ); + expect(popupWrapper).toMatchSnapshot(); + }); + describe('limit filtered item count', () => { const errorSpy = jest.spyOn(console, 'error').mockImplementation(() => {}); diff --git a/components/cascader/index.tsx b/components/cascader/index.tsx index 4ae394d9813f..5f94f628ae99 100644 --- a/components/cascader/index.tsx +++ b/components/cascader/index.tsx @@ -436,6 +436,7 @@ class Cascader extends React.Component { allowClear, showSearch = false, suffixIcon, + notFoundContent, ...otherProps } = props; @@ -493,8 +494,19 @@ class Cascader extends React.Component { ]); let options = props.options; - if (state.inputValue) { - options = this.generateFilteredOptions(prefixCls, renderEmpty); + if (options.length > 0) { + if (state.inputValue) { + options = this.generateFilteredOptions(prefixCls, renderEmpty); + } + } else { + const names: FilledFieldNamesType = getFilledFieldNames(this.props); + options = [ + { + [names.label]: notFoundContent || renderEmpty('Cascader'), + [names.value]: 'ANT_CASCADER_NOT_FOUND', + disabled: true, + }, + ]; } // Dropdown menu should keep previous status until it is fully closed. if (!state.popupVisible) { @@ -512,7 +524,7 @@ class Cascader extends React.Component { // The default value of `matchInputWidth` is `true` const resultListMatchInputWidth = (showSearch as ShowSearchType).matchInputWidth === false ? false : true; - if (resultListMatchInputWidth && state.inputValue && this.input) { + if (resultListMatchInputWidth && (state.inputValue || isNotFound) && this.input) { dropdownMenuColumnStyle.width = this.input.input.offsetWidth; }