ant-design-vue/components/vc-select/utils/warningPropsUtil.ts

136 lines
4.2 KiB
TypeScript
Raw Normal View History

2020-10-07 14:49:01 +00:00
import warning, { noteOnce } from '../../vc-util/warning';
2021-06-26 01:35:40 +00:00
import type { SelectProps } from '..';
2020-10-07 14:49:01 +00:00
import { convertChildrenToData } from './legacyUtil';
import { toArray } from './commonUtil';
2021-06-26 01:35:40 +00:00
import type { RawValueType, LabelValueType } from '../interface/generator';
2020-10-07 14:49:01 +00:00
import { isValidElement } from '../../_util/props-util';
2021-06-26 01:35:40 +00:00
import type { VNode } from 'vue';
2020-10-07 14:49:01 +00:00
function warningProps(props: SelectProps) {
const {
mode,
options,
children,
backfill,
allowClear,
placeholder,
getInputElement,
showSearch,
onSearch,
defaultOpen,
autofocus,
labelInValue,
value,
inputValue,
optionLabelProp,
} = props;
const multiple = mode === 'multiple' || mode === 'tags';
const mergedShowSearch = showSearch !== undefined ? showSearch : multiple || mode === 'combobox';
const mergedOptions = options || convertChildrenToData(children);
// `tags` should not set option as disabled
warning(
2020-10-10 09:01:39 +00:00
mode !== 'tags' || mergedOptions.every((opt: any) => !opt.disabled),
2020-10-07 14:49:01 +00:00
'Please avoid setting option to disabled in tags mode since user can always type text as tag.',
);
// `combobox` should not use `optionLabelProp`
warning(
mode !== 'combobox' || !optionLabelProp,
'`combobox` mode not support `optionLabelProp`. Please set `value` on Option directly.',
);
// Only `combobox` support `backfill`
warning(mode === 'combobox' || !backfill, '`backfill` only works with `combobox` mode.');
// Only `combobox` support `getInputElement`
warning(
mode === 'combobox' || !getInputElement,
'`getInputElement` only work with `combobox` mode.',
);
// Customize `getInputElement` should not use `allowClear` & `placeholder`
noteOnce(
mode !== 'combobox' || !getInputElement || !allowClear || !placeholder,
'Customize `getInputElement` should customize clear and placeholder logic instead of configuring `allowClear` and `placeholder`.',
);
// `onSearch` should use in `combobox` or `showSearch`
if (onSearch && !mergedShowSearch && mode !== 'combobox' && mode !== 'tags') {
warning(false, '`onSearch` should work with `showSearch` instead of use alone.');
}
noteOnce(
!defaultOpen || autofocus,
'`defaultOpen` makes Select open without focus which means it will not close by click outside. You can set `autofocus` if needed.',
);
if (value !== undefined && value !== null) {
const values = toArray<RawValueType | LabelValueType>(value);
warning(
!labelInValue ||
values.every(val => typeof val === 'object' && ('key' in val || 'value' in val)),
2020-11-19 15:03:31 +00:00
'`value` should in shape of `{ value: string | number, label?: any }` when you set `labelInValue` to `true`',
2020-10-07 14:49:01 +00:00
);
warning(
!multiple || Array.isArray(value),
'`value` should be array when `mode` is `multiple` or `tags`',
);
}
// Syntactic sugar should use correct children type
if (children) {
let invalidateChildType = null;
2020-10-10 09:01:39 +00:00
children.some((node: any) => {
if (!isValidElement(node) || !node.type) {
return false;
}
2020-10-07 14:49:01 +00:00
2020-10-10 09:01:39 +00:00
const { type } = node as { type: { isSelectOption?: boolean; isSelectOptGroup?: boolean } };
2020-10-07 14:49:01 +00:00
2020-10-10 09:01:39 +00:00
if (type.isSelectOption) {
return false;
}
if (type.isSelectOptGroup) {
const childs = node.children?.default() || [];
const allChildrenValid = childs.every((subNode: VNode) => {
if (
!isValidElement(subNode) ||
!node.type ||
(subNode.type as { isSelectOption?: boolean }).isSelectOption
) {
return true;
2020-10-07 14:49:01 +00:00
}
2020-10-10 09:01:39 +00:00
invalidateChildType = subNode.type;
return false;
});
if (allChildrenValid) {
return false;
2020-10-07 14:49:01 +00:00
}
return true;
2020-10-10 09:01:39 +00:00
}
invalidateChildType = type;
return true;
});
2020-10-07 14:49:01 +00:00
if (invalidateChildType) {
warning(
false,
2021-06-23 15:08:16 +00:00
`\`children\` should be \`Select.Option\` or \`Select.OptGroup\` instead of \`${
invalidateChildType.displayName || invalidateChildType.name || invalidateChildType
}\`.`,
2020-10-07 14:49:01 +00:00
);
}
warning(
inputValue === undefined,
'`inputValue` is deprecated, please use `searchValue` instead.',
);
}
}
export default warningProps;