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

136 lines
4.2 KiB
TypeScript
Raw Normal View History

2020-10-07 22:49:01 +08:00
import warning, { noteOnce } from '../../vc-util/warning';
import { convertChildrenToData } from './legacyUtil';
import { toArray } from './commonUtil';
import { isValidElement } from '../../_util/props-util';
2021-06-26 09:35:40 +08:00
import type { VNode } from 'vue';
import type { RawValueType, LabelInValueType, SelectProps } from '../Select';
import { isMultiple } from '../BaseSelect';
2020-10-07 22:49:01 +08:00
function warningProps(props: SelectProps) {
const {
mode,
options,
children,
backfill,
allowClear,
placeholder,
getInputElement,
showSearch,
onSearch,
defaultOpen,
autofocus,
labelInValue,
value,
inputValue,
optionLabelProp,
} = props;
const multiple = isMultiple(mode);
2020-10-07 22:49:01 +08:00
const mergedShowSearch = showSearch !== undefined ? showSearch : multiple || mode === 'combobox';
const mergedOptions = options || convertChildrenToData(children);
// `tags` should not set option as disabled
warning(
mode !== 'tags' || mergedOptions.every((opt: { disabled?: boolean }) => !opt.disabled),
2020-10-07 22:49:01 +08: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 | LabelInValueType>(value);
2020-10-07 22:49:01 +08:00
warning(
!labelInValue ||
values.every(val => typeof val === 'object' && ('key' in val || 'value' in val)),
2020-11-19 23:03:31 +08:00
'`value` should in shape of `{ value: string | number, label?: any }` when you set `labelInValue` to `true`',
2020-10-07 22:49:01 +08: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 17:01:39 +08:00
children.some((node: any) => {
if (!isValidElement(node) || !node.type) {
return false;
}
2020-10-07 22:49:01 +08:00
2020-10-10 17:01:39 +08:00
const { type } = node as { type: { isSelectOption?: boolean; isSelectOptGroup?: boolean } };
2020-10-07 22:49:01 +08:00
2020-10-10 17:01:39 +08: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 22:49:01 +08:00
}
2020-10-10 17:01:39 +08:00
invalidateChildType = subNode.type;
return false;
});
if (allChildrenValid) {
return false;
2020-10-07 22:49:01 +08:00
}
return true;
2020-10-10 17:01:39 +08:00
}
invalidateChildType = type;
return true;
});
2020-10-07 22:49:01 +08:00
if (invalidateChildType) {
warning(
false,
2021-06-23 23:08:16 +08:00
`\`children\` should be \`Select.Option\` or \`Select.OptGroup\` instead of \`${
invalidateChildType.displayName || invalidateChildType.name || invalidateChildType
}\`.`,
2020-10-07 22:49:01 +08:00
);
}
warning(
inputValue === undefined,
'`inputValue` is deprecated, please use `searchValue` instead.',
);
}
}
export default warningProps;