Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: popover add visible props #711

Merged
merged 1 commit into from
Apr 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 18 additions & 4 deletions packages/react-vant/src/components/popover/Popover.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import React, {
useEffect,
useImperativeHandle,
useRef,
useState,
} from 'react'
import cls from 'clsx'
import { Instance, createPopper, offsetModifier } from '@vant/popperjs'
Expand All @@ -15,6 +14,7 @@ import useClickAway from '../hooks/use-click-away'
import Popup from '../popup'
import useLazyEffect from '../hooks/use-lazy-effect'
import { mergeProps } from '../utils/get-default-props'
import { usePropsValue } from '../hooks'

const popupProps = [
'overlay',
Expand All @@ -23,8 +23,6 @@ const popupProps = [
'overlayClass',
'closeOnClickOverlay',
'teleport',
'onClose',
'onOpen',
'onClosed',
'onOpened',
'onClickOverlay',
Expand All @@ -46,11 +44,25 @@ const Popover = forwardRef<PopoverInstance, PopoverProps>(
actions: [],
placement: 'bottom',
})
const [visible, updateShow] = useState(false)
const popper = useRef<Instance>(null)
const wrapperRef = useRef<HTMLElement>()
const popoverRef = useRef<PopupInstanceType>()

const onVisibleChange = (visible: boolean) => {
if (visible) {
props.onOpen?.()
} else {
props.onClose?.()
}
}
const [visible, updateShow] = usePropsValue({
value: props.visible,
defaultValue: false,
onChange: onVisibleChange,
})
const onInternalOpen = () => updateShow(true)
const onInternalClose = () => updateShow(false)

const createPopperInstance = () =>
createPopper(wrapperRef.current, popoverRef.current.popupRef.current, {
placement: props.placement,
Expand Down Expand Up @@ -189,6 +201,8 @@ const Popover = forwardRef<PopoverInstance, PopoverProps>(
position=''
transition='rv-zoom'
lockScroll={false}
onOpen={onInternalOpen}
onClose={onInternalClose}
{...pick(props, popupProps)}
>
<div className={cls(bem('arrow'))} />
Expand Down
2 changes: 2 additions & 0 deletions packages/react-vant/src/components/popover/PropsType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ export interface PopoverProps extends BaseTypeProps {
reference?: string | React.ReactNode
/** 指定挂载的节点 */
teleport?: TeleportType
/** 受控的显示状态 */
visible?: boolean
children?: React.ReactNode
/** 点击选项时触发 */
onSelect?: (action: PopoverAction, index: number) => void
Expand Down
1 change: 1 addition & 0 deletions packages/react-vant/src/components/popover/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ bottom-end # 底部右侧位置
| theme | 主题风格,可选值为 `dark` | _string_ | `light` |
| trigger | 触发方式,可选值为 `manual` | _string_ | `click` |
| duration | 动画时长,单位秒,设置为 0 可以禁用动画 | _number \| string_ | `0.3` |
| visible | 受控的是否显示菜单 | _boolean_ | - |
| offset | 出现位置的偏移量 | _[number, number]_ | `[0, 8]` |
| overlay | 是否显示遮罩层 | _boolean_ | `false` |
| overlayClass | 自定义遮罩层类名 | _string_ | - |
Expand Down
Loading