mirror of
https://github.com/ant-design/ant-design.git
synced 2025-08-06 16:06:28 +08:00
chore: Add warn of value
not provided in Cascader (#17511)
* add warn of `value` not provided * adjust desc & logic
This commit is contained in:
parent
4f288489ec
commit
4a1d68c87f
@ -429,4 +429,13 @@ describe('Cascader', () => {
|
|||||||
);
|
);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should warning if not find `value` in `options`', () => {
|
||||||
|
const errorSpy = jest.spyOn(console, 'error').mockImplementation(() => {});
|
||||||
|
mount(<Cascader options={[{ label: 'a', value: 'a', children: [{ label: 'b' }] }]} />);
|
||||||
|
expect(errorSpy).toHaveBeenCalledWith(
|
||||||
|
'Warning: [antd: Cascader] Not found `value` in `options`.',
|
||||||
|
);
|
||||||
|
errorSpy.mockRestore();
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
@ -204,6 +204,14 @@ function flattenTree(
|
|||||||
|
|
||||||
const defaultDisplayRender = (label: string[]) => label.join(' / ');
|
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<CascaderProps, CascaderState> {
|
class Cascader extends React.Component<CascaderProps, CascaderState> {
|
||||||
static defaultProps = {
|
static defaultProps = {
|
||||||
placeholder: 'Please select',
|
placeholder: 'Please select',
|
||||||
@ -229,6 +237,10 @@ class Cascader extends React.Component<CascaderProps, CascaderState> {
|
|||||||
newState.flattenOptions = flattenTree(nextProps.options, nextProps);
|
newState.flattenOptions = flattenTree(nextProps.options, nextProps);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (process.env.NODE_ENV !== 'production' && nextProps.options) {
|
||||||
|
warningValueNotExist(nextProps.options, getFieldNames(nextProps));
|
||||||
|
}
|
||||||
|
|
||||||
return newState;
|
return newState;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user