Skip to content

Commit

Permalink
fix(field): fix placeholder resize failure in swipe and close #1763
Browse files Browse the repository at this point in the history
  • Loading branch information
haoziqaq committed Sep 14, 2024
1 parent 31e563c commit 23f907c
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 2 deletions.
7 changes: 7 additions & 0 deletions packages/varlet-ui/src/field-decorator/FieldDecorator.vue
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ import { isEmpty, getStyle, call, doubleRaf } from '@varlet/shared'
import { createNamespace } from '../utils/components'
import { onWindowResize, onSmartMounted } from '@varlet/use'
import { usePopup } from '../popup/provide'
import { useSwipeResizeDispatcher } from '../swipe/provide'
const { name, n, classes } = createNamespace('field-decorator')
Expand All @@ -134,6 +135,7 @@ export default defineComponent({
const transitionDisabled = ref(true)
const isFloating = computed(() => props.hint && (!isEmpty(props.value) || props.isFocusing))
const { popup, bindPopup } = usePopup()
const { bindSwipeResizeDispatcher } = useSwipeResizeDispatcher()
const color = computed<string | undefined>(() =>
!props.isError ? (props.isFocusing ? props.focusColor : props.blurColor) : undefined
Expand All @@ -152,6 +154,11 @@ export default defineComponent({
onUpdated(resize)
call(bindPopup, null)
call(bindSwipeResizeDispatcher, {
onResize() {
nextTick().then(resize)
},
})
if (popup) {
watch(
Expand Down
8 changes: 7 additions & 1 deletion packages/varlet-ui/src/swipe/Swipe.vue
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ import VarButton from '../button'
import VarIcon from '../icon'
import Hover from '../hover'
import { defineComponent, ref, computed, watch, onActivated } from 'vue'
import { useSwipeItems, type SwipeProvider } from './provide'
import { useSwipeItems, useSwipeResizeListeners, type SwipeProvider } from './provide'
import { props, type SwipeToOptions } from './props'
import { clamp, isNumber, toNumber, doubleRaf, preventDefault, call } from '@varlet/shared'
import { createNamespace } from '../utils/components'
Expand Down Expand Up @@ -141,6 +141,7 @@ export default defineComponent({
const index = ref(0)
const hovering = ref(false)
const { swipeItems, bindSwipeItems, length } = useSwipeItems()
const { swipeResizeListeners, bindSwipeResizeListeners } = useSwipeResizeListeners()
const { popup, bindPopup } = usePopup()
const {
deltaX,
Expand Down Expand Up @@ -171,6 +172,7 @@ export default defineComponent({
useEventListener(() => window, 'keydown', handleKeydown)
call(bindPopup, null)
call(bindSwipeResizeListeners, null)
watch(
() => length.value,
Expand Down Expand Up @@ -442,6 +444,10 @@ export default defineComponent({
setTimeout(() => {
lockDuration.value = false
})
swipeResizeListeners.forEach(({ onResize }) => {
onResize()
})
}
// expose
Expand Down
32 changes: 31 additions & 1 deletion packages/varlet-ui/src/swipe/provide.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { ComputedRef, Ref } from 'vue'
import { useChildren } from '@varlet/use'
import { useChildren, useParent } from '@varlet/use'
import { type SwipeItemProvider } from '../swipe-item/provide'

export interface SwipeProvider {
Expand All @@ -8,8 +8,16 @@ export interface SwipeProvider {
vertical: ComputedRef<boolean>
}

export interface SwipeResizeListenerProvider {
onResize(): void
}

export const SWIPE_BIND_SWIPE_ITEM_KEY = Symbol('SWIPE_BIND_SWIPE_ITEM_KEY')

export const SWIPE_RESIZE_DISPATCHER_BIND_SWIPE_RESIZE_LISTENER_KEY = Symbol(
'SWIPE_RESIZE_DISPATCHER_BIND_SWIPE_RESIZE_LISTENER_KEY'
)

export function useSwipeItems() {
const { childProviders, length, bindChildren } = useChildren<SwipeProvider, SwipeItemProvider>(
SWIPE_BIND_SWIPE_ITEM_KEY
Expand All @@ -21,3 +29,25 @@ export function useSwipeItems() {
bindSwipeItems: bindChildren,
}
}

export function useSwipeResizeListeners() {
const { childProviders, bindChildren } = useChildren<null, SwipeResizeListenerProvider>(
SWIPE_RESIZE_DISPATCHER_BIND_SWIPE_RESIZE_LISTENER_KEY
)

return {
swipeResizeListeners: childProviders,
bindSwipeResizeListeners: bindChildren,
}
}

export function useSwipeResizeDispatcher() {
const { parentProvider, bindParent } = useParent<null, SwipeResizeListenerProvider>(
SWIPE_RESIZE_DISPATCHER_BIND_SWIPE_RESIZE_LISTENER_KEY
)

return {
swipeResizeDispatcher: parentProvider,
bindSwipeResizeDispatcher: bindParent,
}
}

0 comments on commit 23f907c

Please sign in to comment.