mirror of
https://github.com/element-plus/element-plus.git
synced 2026-03-13 07:51:17 +08:00
* refactor(tokens): remove tokens * Remove tokens/breadcrumb. * refactor(tokens): remove tokens/button * refactor(tokens): remove tokens/carousel * refactor(tokens): removing tokens/checkbox * refactor(tokens): removing tokens/collapse * refactor(tokens): removing tokens/dialog * refactor(tokens): removing tokens/pagination * refactor(tokens): removing tokens/radio * refactor(tokens): removing tokens/row * refactor(tokens): removing tokens/scrollbar * refactor(tokens): removing tokens/slider * refactor(tokens): removing tokens/tabs * refactor(tokens): removing tokens/upload * refactor(tokens): removing tokens/popper * refactor(tokens): removing tokens/tooltip * refactor(tokens): removing tokens/tooltip-v2 * refactor(tokens): removing tokens/date-picker * refactor(project): removing tokens/experimentals * Remove tokens/experimentals * Remove package/tokens * Remove tokens related parts * refactor(project): removing packages/tokens completely * chore: update import statement
67 lines
1.6 KiB
TypeScript
67 lines
1.6 KiB
TypeScript
import { computed, provide, ref, watch } from 'vue'
|
|
import { ensureArray } from '@element-plus/utils'
|
|
import { useNamespace } from '@element-plus/hooks'
|
|
import { CHANGE_EVENT, UPDATE_MODEL_EVENT } from '@element-plus/constants'
|
|
import { collapseContextKey } from './constants'
|
|
|
|
import type { SetupContext } from 'vue'
|
|
import type {
|
|
CollapseActiveName,
|
|
CollapseEmits,
|
|
CollapseProps,
|
|
} from './collapse'
|
|
|
|
export const useCollapse = (
|
|
props: CollapseProps,
|
|
emit: SetupContext<CollapseEmits>['emit']
|
|
) => {
|
|
const activeNames = ref(ensureArray(props.modelValue))
|
|
|
|
const setActiveNames = (_activeNames: CollapseActiveName[]) => {
|
|
activeNames.value = _activeNames
|
|
const value = props.accordion ? activeNames.value[0] : activeNames.value
|
|
emit(UPDATE_MODEL_EVENT, value)
|
|
emit(CHANGE_EVENT, value)
|
|
}
|
|
|
|
const handleItemClick = (name: CollapseActiveName) => {
|
|
if (props.accordion) {
|
|
setActiveNames([activeNames.value[0] === name ? '' : name])
|
|
} else {
|
|
const _activeNames = [...activeNames.value]
|
|
const index = _activeNames.indexOf(name)
|
|
|
|
if (index > -1) {
|
|
_activeNames.splice(index, 1)
|
|
} else {
|
|
_activeNames.push(name)
|
|
}
|
|
setActiveNames(_activeNames)
|
|
}
|
|
}
|
|
|
|
watch(
|
|
() => props.modelValue,
|
|
() => (activeNames.value = ensureArray(props.modelValue)),
|
|
{ deep: true }
|
|
)
|
|
|
|
provide(collapseContextKey, {
|
|
activeNames,
|
|
handleItemClick,
|
|
})
|
|
return {
|
|
activeNames,
|
|
setActiveNames,
|
|
}
|
|
}
|
|
|
|
export const useCollapseDOM = () => {
|
|
const ns = useNamespace('collapse')
|
|
|
|
const rootKls = computed(() => ns.b())
|
|
return {
|
|
rootKls,
|
|
}
|
|
}
|