import { nextTick, reactive, watch } from 'vue'; import { createNamespace } from '../utils'; // Components import Tab from '../tab'; import Tabs from '../tabs'; import Icon from '../icon'; const [createComponent, bem, t] = createNamespace('cascader'); export default createComponent({ props: { title: String, modelValue: [Number, String], fieldNames: Object, placeholder: String, activeColor: String, options: { type: Array, default: () => [], }, closeable: { type: Boolean, default: true, }, }, emits: ['close', 'change', 'finish', 'update:modelValue'], setup(props, { slots, emit }) { const state = reactive({ tabs: [], activeTab: 0, }); const { text: textKey, value: valueKey, children: childrenKey } = { text: 'text', value: 'value', children: 'children', ...props.fieldNames, }; const getSelectedOptionsByValue = (options, value) => { for (let i = 0; i < options.length; i++) { const option = options[i]; if (option[valueKey] === value) { return [option]; } if (option[childrenKey]) { const selectedOptions = getSelectedOptionsByValue( option[childrenKey], value ); if (selectedOptions) { return [option, ...selectedOptions]; } } } }; const updateTabs = () => { if (props.modelValue || props.modelValue === 0) { const selectedOptions = getSelectedOptionsByValue( props.options, props.modelValue ); if (selectedOptions) { let optionsCursor = props.options; state.tabs = selectedOptions.map((option) => { const tab = { options: optionsCursor, selectedOption: option, }; const next = optionsCursor.filter( (item) => item[valueKey] === option[valueKey] ); if (next.length) { optionsCursor = next[0][childrenKey]; } return tab; }); if (optionsCursor) { state.tabs.push({ options: optionsCursor, selectedOption: null, }); } nextTick(() => { state.activeTab = state.tabs.length - 1; }); return; } } state.tabs = [ { options: props.options, selectedOption: null, }, ]; }; const onSelect = (option, tabIndex) => { state.tabs[tabIndex].selectedOption = option; if (state.tabs.length > tabIndex + 1) { state.tabs = state.tabs.slice(0, tabIndex + 1); } if (option[childrenKey]) { const nextTab = { options: option[childrenKey], selectedOption: null, }; if (state.tabs[tabIndex + 1]) { state.tabs[tabIndex + 1] = nextTab; } else { state.tabs.push(nextTab); } nextTick(() => { state.activeTab++; }); } const selectedOptions = state.tabs .map((tab) => tab.selectedOption) .filter((item) => !!item); const eventParams = { value: option[valueKey], tabIndex, selectedOptions, }; emit('update:modelValue', option[valueKey]); emit('change', eventParams); if (!option[childrenKey]) { emit('finish', eventParams); } }; const onClose = () => { emit('close'); }; const renderHeader = () => (