From 4a1d68c87f3cc034a2edcea85ae4468ce5764a36 Mon Sep 17 00:00:00 2001 From: zombieJ Date: Mon, 8 Jul 2019 10:57:34 +0800 Subject: [PATCH] chore: Add warn of `value` not provided in Cascader (#17511) * add warn of `value` not provided * adjust desc & logic --- components/cascader/__tests__/index.test.js | 9 +++++++++ components/cascader/index.tsx | 12 ++++++++++++ 2 files changed, 21 insertions(+) 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; }