diff --git a/components/cascader/__tests__/index.test.js b/components/cascader/__tests__/index.test.js index 973c991338..57be6c141f 100644 --- a/components/cascader/__tests__/index.test.js +++ b/components/cascader/__tests__/index.test.js @@ -429,4 +429,13 @@ describe('Cascader', () => { ); }); }); + + it('should warning if not find `value` in `options`', () => { + const errorSpy = jest.spyOn(console, 'error').mockImplementation(() => {}); + mount(); + expect(errorSpy).toHaveBeenCalledWith( + 'Warning: [antd: Cascader] Not found `value` in `options`.', + ); + errorSpy.mockRestore(); + }); }); diff --git a/components/cascader/index.tsx b/components/cascader/index.tsx index 17fc1dbbe5..8b617ae33c 100644 --- a/components/cascader/index.tsx +++ b/components/cascader/index.tsx @@ -204,6 +204,14 @@ function flattenTree( const defaultDisplayRender = (label: string[]) => label.join(' / '); +function warningValueNotExist(list: CascaderOptionType[], fieldNames: FieldNamesType = {}) { + (list || []).forEach(item => { + const valueFieldName = fieldNames.value || 'value'; + warning(valueFieldName in item, 'Cascader', 'Not found `value` in `options`.'); + warningValueNotExist(item[fieldNames.children || 'children'], fieldNames); + }); +} + class Cascader extends React.Component { static defaultProps = { placeholder: 'Please select', @@ -229,6 +237,10 @@ class Cascader extends React.Component { newState.flattenOptions = flattenTree(nextProps.options, nextProps); } + if (process.env.NODE_ENV !== 'production' && nextProps.options) { + warningValueNotExist(nextProps.options, getFieldNames(nextProps)); + } + return newState; }