66 <wd-icon name =" arrow-down" :custom-class =" `wd-collapse-item__arrow ${expanded ? 'is-retract' : ''}`" />
77 </slot >
88 </view >
9- <view class =" wd-collapse-item__wrapper" :style =" contentStyle" >
10- <view class =" wd-collapse-item__body" >
9+ <view class =" wd-collapse-item__wrapper" :style =" contentStyle" @transitionend = " handleTransitionEnd " >
10+ <view class =" wd-collapse-item__body" :id = " collapseId " >
1111 <slot />
1212 </view >
1313 </view >
@@ -25,23 +25,21 @@ export default {
2525 </script >
2626
2727<script lang="ts" setup>
28- import { computed , getCurrentInstance , onMounted , ref , watch } from ' vue'
29- import { getRect , isArray , isDef , isPromise , objToStyle } from ' ../common/util'
28+ import { computed , getCurrentInstance , onMounted , ref , watch , type CSSProperties } from ' vue'
29+ import { addUnit , getRect , isArray , isDef , isPromise , objToStyle , requestAnimationFrame , uuid } from ' ../common/util'
3030import { useParent } from ' ../composables/useParent'
3131import { COLLAPSE_KEY } from ' ../wd-collapse/types'
3232import { collapseItemProps , type CollapseItemExpose } from ' ./types'
3333
34- const $body = ' .wd-collapse-item__body '
34+ const collapseId = ref < string >( ` collapseId${ uuid ()} ` )
3535
3636const props = defineProps (collapseItemProps )
3737
3838const { parent : collapse, index } = useParent (COLLAPSE_KEY )
3939
4040const height = ref <string | number >(' ' )
41-
41+ const inited = ref < boolean >( false )
4242const expanded = ref <boolean >(false )
43-
44- const transD = ref <string >(' 0.3s' )
4543const { proxy } = getCurrentInstance () as any
4644
4745/**
@@ -55,9 +53,14 @@ const isFirst = computed(() => {
5553 * 容器样式,(动画)
5654 */
5755const contentStyle = computed (() => {
58- let style: Record <string , string > = {
59- height: expanded .value ? height .value + ' px' : ' 0px' ,
60- ' transition-duration' : transD .value
56+ const style: CSSProperties = {}
57+ if (inited .value ) {
58+ style .transition = ' height 0.3s ease-in-out'
59+ }
60+ if (! expanded .value ) {
61+ style .height = ' 0px'
62+ } else if (height .value ) {
63+ style .height = addUnit (height .value )
6164 }
6265 return objToStyle (style )
6366})
@@ -72,21 +75,11 @@ const selected = computed(() => {
7275
7376watch (
7477 () => selected .value ,
75- (newVal ) => {
76- const name = props .name
77- if (isDef (newVal )) {
78- if (typeof newVal === ' string' && newVal === name ) {
79- doResetHeight ($body )
80- expanded .value = true
81- } else if (isArray (newVal ) && newVal .indexOf (name as string ) >= 0 ) {
82- doResetHeight ($body )
83- expanded .value = true
84- } else {
85- expanded .value = false
86- }
87- } else {
88- expanded .value = false
78+ () => {
79+ if (! inited .value ) {
80+ return
8981 }
82+ updateExpend ()
9083 },
9184 {
9285 deep: true ,
@@ -95,29 +88,40 @@ watch(
9588)
9689
9790onMounted (() => {
98- init ()
91+ updateExpend ()
9992})
10093
101- /**
102- * 初始化将组件信息注入父组件
103- */
104- function init() {
105- doResetHeight ($body )
106- }
107-
108- /**
109- * 控制折叠面板滚动
110- * @param {String} select 选择器名称
111- * @param {Boolean} firstRender 是否首次渲染
112- */
113- function doResetHeight(select : string ) {
114- getRect (select , false , proxy ).then ((rect ) => {
115- if (! rect ) return
94+ function updateExpend() {
95+ return getRect (` #${collapseId .value } ` , false , proxy ).then ((rect ) => {
11696 const { height : rectHeight } = rect
117- height .value = Number (rectHeight )
97+ height .value = isDef (rectHeight ) ? Number (rectHeight ) : ' '
98+ const name = props .name
99+ requestAnimationFrame (() => {
100+ if (isDef (selected .value )) {
101+ if (
102+ (typeof selected .value === ' string' && selected .value === name ) ||
103+ (isArray (selected .value ) && selected .value .indexOf (name as string ) >= 0 )
104+ ) {
105+ expanded .value = true
106+ } else {
107+ expanded .value = false
108+ }
109+ } else {
110+ expanded .value = false
111+ }
112+ if (! inited .value ) {
113+ inited .value = true
114+ }
115+ })
118116 })
119117}
120118
119+ function handleTransitionEnd() {
120+ if (expanded .value ) {
121+ height .value = ' '
122+ }
123+ }
124+
121125// 点击子项
122126function handleClick() {
123127 if (props .disabled ) return
0 commit comments