fix: cascader types (#3912)

pull/3923/head
John 2021-04-10 22:26:11 +08:00 committed by GitHub
parent 58c543985d
commit 4c370edf24
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 22 additions and 7 deletions

View File

@ -80,6 +80,16 @@ export interface ShowSearchType {
limit?: number | false; limit?: number | false;
} }
export interface EmptyFilteredOptionsType {
disabled: boolean;
[key: string]: any;
}
export interface FilteredOptionsType extends EmptyFilteredOptionsType {
__IS_FILTERED_OPTION: boolean;
path: CascaderOptionType[];
}
// const ShowSearchType = PropTypes.shape({ // const ShowSearchType = PropTypes.shape({
// filter: PropTypes.func, // filter: PropTypes.func,
// render: PropTypes.func, // render: PropTypes.func,
@ -217,11 +227,13 @@ const Cascader = defineComponent({
data() { data() {
const { value, defaultValue, popupVisible, showSearch, options } = this.$props; const { value, defaultValue, popupVisible, showSearch, options } = this.$props;
return { return {
sValue: value || defaultValue || [], sValue: (value || defaultValue || []) as any[],
inputValue: '', inputValue: '',
inputFocused: false, inputFocused: false,
sPopupVisible: popupVisible, sPopupVisible: popupVisible as boolean,
flattenOptions: showSearch ? flattenTree(options, this.$props) : undefined, flattenOptions: showSearch
? flattenTree(options as CascaderOptionType[], this.$props)
: undefined,
}; };
}, },
watch: { watch: {
@ -311,7 +323,7 @@ const Cascader = defineComponent({
handleInputClick(e: MouseEvent & { nativeEvent?: any }) { handleInputClick(e: MouseEvent & { nativeEvent?: any }) {
const { inputFocused, sPopupVisible } = this; const { inputFocused, sPopupVisible } = this;
// Prevent `Trigger` behaviour. // Prevent `Trigger` behavior.
if (inputFocused || sPopupVisible) { if (inputFocused || sPopupVisible) {
e.stopPropagation(); e.stopPropagation();
if (e.nativeEvent && e.nativeEvent.stopImmediatePropagation) { if (e.nativeEvent && e.nativeEvent.stopImmediatePropagation) {
@ -346,8 +358,8 @@ const Cascader = defineComponent({
const displayRender = getComponent(this, 'displayRender', {}, false) || defaultDisplayRender; const displayRender = getComponent(this, 'displayRender', {}, false) || defaultDisplayRender;
const value = this.sValue; const value = this.sValue;
const unwrappedValue = Array.isArray(value[0]) ? value[0] : value; const unwrappedValue = Array.isArray(value[0]) ? value[0] : value;
const selectedOptions = arrayTreeFilter( const selectedOptions = arrayTreeFilter<CascaderOptionType>(
options, options as CascaderOptionType[],
(o, level) => o[names.value] === unwrappedValue[level], (o, level) => o[names.value] === unwrappedValue[level],
{ childrenKeyName: names.children }, { childrenKeyName: names.children },
); );
@ -366,7 +378,10 @@ const Cascader = defineComponent({
} }
}, },
generateFilteredOptions(prefixCls: string | undefined, renderEmpty: RenderEmptyHandler) { generateFilteredOptions(
prefixCls: string | undefined,
renderEmpty: RenderEmptyHandler,
): EmptyFilteredOptionsType[] | FilteredOptionsType[] {
const { showSearch, notFoundContent } = this; const { showSearch, notFoundContent } = this;
const names: FilledFieldNamesType = getFilledFieldNames(this.$props); const names: FilledFieldNamesType = getFilledFieldNames(this.$props);
const { const {