|
| 1 | +import { ref, computed, nextTick, watch, onMounted, inject, defineComponent, getCurrentInstance } from 'vue'; |
| 2 | +import { ChevronDownIcon, ChevronUpIcon } from 'tdesign-icons-vue-next'; |
| 3 | +import TCell from '../cell'; |
| 4 | +import props from './collapse-panel-props'; |
| 5 | +import config from '../config'; |
| 6 | +import { findIndex } from './util'; |
| 7 | +import { useTNodeJSX, useContent } from '../hooks/tnode'; |
| 8 | +import { usePrefixClass } from '../hooks/useClass'; |
| 9 | +import { CollapseProvide } from './collapse'; |
| 10 | + |
| 11 | +const { prefix } = config; |
| 12 | +const name = `${prefix}-collapse-panel`; |
| 13 | + |
| 14 | +export default defineComponent({ |
| 15 | + name, |
| 16 | + components: { TCell }, |
| 17 | + props, |
| 18 | + setup(props, { slots }) { |
| 19 | + const renderTNodeJSX = useTNodeJSX(); |
| 20 | + const renderContent = useContent(); |
| 21 | + |
| 22 | + const componentName = usePrefixClass('collapse-panel'); |
| 23 | + |
| 24 | + const parent = inject<CollapseProvide>('collapse'); |
| 25 | + const renderParentTNode: Function = inject('renderParentTNode'); |
| 26 | + |
| 27 | + const disabled = computed(() => parent?.disabled.value || props.disabled); |
| 28 | + const rootClass = computed(() => ({ |
| 29 | + [`${componentName.value}`]: true, |
| 30 | + [`${componentName.value}--${props.placement}`]: true, |
| 31 | + [`${componentName.value}--active`]: isActive.value, |
| 32 | + [`${componentName.value}--disabled`]: disabled.value, |
| 33 | + })); |
| 34 | + const isActive = computed(() => findIndex(props.value, parent?.activeValue.value) > -1); |
| 35 | + const updatePanelValue = (args?: any) => { |
| 36 | + if (props.value != null) { |
| 37 | + parent?.onPanelChange(props.value, args); |
| 38 | + } |
| 39 | + }; |
| 40 | + |
| 41 | + const handleClick = (e: MouseEvent) => { |
| 42 | + e?.stopPropagation(); |
| 43 | + if (disabled.value) { |
| 44 | + return; |
| 45 | + } |
| 46 | + updatePanelValue({ e }); |
| 47 | + }; |
| 48 | + |
| 49 | + // 设置折叠/展开高度过渡 |
| 50 | + const bodyRef = ref(); |
| 51 | + const wrapRef = ref(); |
| 52 | + const headRef = ref(); |
| 53 | + const wrapperHeight = ref(''); |
| 54 | + const updatePanelState = () => { |
| 55 | + nextTick(() => { |
| 56 | + if (!wrapRef.value) { |
| 57 | + return; |
| 58 | + } |
| 59 | + const { height: headHeight } = headRef.value.getBoundingClientRect(); |
| 60 | + if (!isActive.value) { |
| 61 | + wrapperHeight.value = `${headHeight}px`; |
| 62 | + return; |
| 63 | + } |
| 64 | + const { height: bodyHeight } = bodyRef.value.getBoundingClientRect(); |
| 65 | + const height = headHeight + bodyHeight; |
| 66 | + wrapperHeight.value = `${height}px`; |
| 67 | + }); |
| 68 | + }; |
| 69 | + |
| 70 | + watch( |
| 71 | + isActive, |
| 72 | + () => { |
| 73 | + nextTick(() => updatePanelState()); |
| 74 | + }, |
| 75 | + { |
| 76 | + immediate: true, |
| 77 | + }, |
| 78 | + ); |
| 79 | + |
| 80 | + onMounted(() => { |
| 81 | + if (parent?.defaultExpandAll) { |
| 82 | + updatePanelValue(); |
| 83 | + } |
| 84 | + }); |
| 85 | + |
| 86 | + const renderDefaultIcon = () => { |
| 87 | + if (props.placement === 'bottom') { |
| 88 | + return isActive.value ? <ChevronUpIcon /> : <ChevronDownIcon />; |
| 89 | + } |
| 90 | + return isActive.value ? <ChevronDownIcon /> : <ChevronUpIcon />; |
| 91 | + }; |
| 92 | + const panelExpandIcon = computed(() => slots.expandIcon || props.expandIcon); |
| 93 | + const renderRightIcon = () => { |
| 94 | + const tNodeRender = panelExpandIcon.value === undefined ? renderParentTNode : renderTNodeJSX; |
| 95 | + return <div class={`${componentName.value}__header-icon`}>{tNodeRender('expandIcon', renderDefaultIcon())}</div>; |
| 96 | + }; |
| 97 | + |
| 98 | + return () => { |
| 99 | + const panelContent = renderContent('default', 'content'); |
| 100 | + const headerContent = renderTNodeJSX('header'); |
| 101 | + const noteContent = renderTNodeJSX('headerRightContent'); |
| 102 | + const leftIcon = renderTNodeJSX('headerLeftIcon'); |
| 103 | + |
| 104 | + return ( |
| 105 | + <div ref={wrapRef} class={rootClass.value} style={{ height: wrapperHeight.value }}> |
| 106 | + <div ref={headRef} class={`${componentName.value}__title`} onClick={handleClick}> |
| 107 | + <TCell |
| 108 | + class={[ |
| 109 | + `${componentName.value}__header`, |
| 110 | + `${componentName.value}__header--${props.placement}`, |
| 111 | + { [`${componentName.value}__header--expanded`]: isActive.value }, |
| 112 | + ]} |
| 113 | + v-slots={{ |
| 114 | + leftIcon: () => leftIcon, |
| 115 | + title: () => headerContent, |
| 116 | + note: () => noteContent, |
| 117 | + rightIcon: () => renderRightIcon(), |
| 118 | + }} |
| 119 | + ></TCell> |
| 120 | + </div> |
| 121 | + <div ref={bodyRef} class={`${componentName.value}__content`}> |
| 122 | + {panelContent} |
| 123 | + </div> |
| 124 | + </div> |
| 125 | + ); |
| 126 | + }; |
| 127 | + }, |
| 128 | +}); |
0 commit comments