# Popover

承载聚合类信息的气泡框，包含 Popover（信息气泡，可承载复杂、可操作内容）与 PopoverConfirm（轻量二次确认气泡）两个子组件。

## 适用场景

- 名词解释、更多信息、新功能提示等跟随交互出现的辅助提示
- 浮层内容需要用户进一步操作的场景（区别于仅展示的 Tooltip）
- 错误提示、输入提示、内容输入等聚合类信息承载
- 轻量级的二次确认操作，替代强打断的模态弹窗

## Props

### Popover Props

| 名称 | 类型 | 默认值 | 必填 | 说明 |
|------|------|--------|------|------|
| className | `string` | - | 否 | 自定义 class |
| withArrow | `boolean` | - | 否 | 是否有箭头 |
| headline | `ReactNode` | - | 否 | 标题 |
| content | `ReactNode` | - | 否 | 内容 |
| actions | `ReactNode` | - | 否 | 操作区 |
| actionsAlign | `"right" \| "center" \| "left"` | - | 否 | 操作区对齐方式 |
| withCloseX | `boolean` | - | 否 | 是否有关闭小叉 |
| onClickCloseX | `(e: MouseEvent<Element, MouseEvent>) => void` | - | 否 | 点击关闭小叉回调 |
| size | `"sm" \| "md"` | - | 否 | 尺寸，影响 headline/content 字体大小及按钮尺寸 |
| type | `"click" \| "hover" \| "focus"` | - | 否 | 触发方式 |
| visible | `boolean` | - | 否 | popper 是否显示控制值 |
| target | `(() => HTMLElement \| null \| void) \| HTMLElement \| null` | - | 否 | 指定目标元素，不接受 ReactNode 或模拟的 ReferenceObject |
| defaultVisible | `boolean` | - | 否 | popper 是否显示默认值 |
| onChange | `(visible: boolean, e?: Event \| SyntheticEvent<Element, Event>) => void` | - | 否 | visible 变化回调 |
| beforeChange | `(visible: boolean) => boolean \| void \| Promise<unknown>` | - | 否 | visible 变化前回调，返回 false 或 reject 可阻止变化 |
| mouseEnterDelay | `number` | `150` | 否 | mouseEnter 延迟触发（毫秒） |
| mouseLeaveDelay | `number` | `60` | 否 | mouseLeave 延迟触发（毫秒） |
| focusDelay | `number` | `150` | 否 | focus 延迟触发（毫秒） |
| blurDelay | `number` | - | 否 | blur 延迟触发（毫秒） |
| shouldHideParent | `boolean` | - | 否 | 是否隐藏时隐藏 parent |
| shouldHideOnMousedownDocument | `boolean \| ((e: Event) => boolean)` | - | 否 | type click 时点击外部隐藏，设为 false 可禁用，也可传回调自定义判断 |
| placement | `Placement` | - | 否 | 浮层相对于 target 的位置 |
| arrowOffset | `number \| "auto"` | - | 否 | 箭头偏移量，仅在 placement 为 *-end/*-start 时有效 |
| portalContainer | `HTMLElement \| (() => HTMLElement)` | - | 否 | 指定 portal 挂载容器，默认 document.body |

### PopoverConfirm Props

| 名称 | 类型 | 默认值 | 必填 | 说明 |
|------|------|--------|------|------|
| className | `string` | - | 否 | 自定义 class |
| type | `"info" \| "success" \| "warning" \| "error"` | `warning` | 否 | 类型 |
| icon | `SvgFC` | - | 否 | 自定义 icon |
| message | `ReactNode` | - | 否 | 提示内容（已废弃，请使用 content） |
| onConfirm | `(e: MouseEvent<Element, MouseEvent>) => void` | - | 否 | 点击确认回调 |
| onCancel | `(e: MouseEvent<Element, MouseEvent>) => void` | - | 否 | 点击取消回调 |
| confirmLabel | `string` | - | 否 | 自定义确认按钮文本 |
| cancelLabel | `string` | - | 否 | 自定义取消按钮文本 |
| placement | `Placement` | - | 否 | 浮层相对于 target 的位置 |
| withArrow | `boolean` | - | 否 | 是否有箭头 |
| arrowOffset | `number \| "auto"` | - | 否 | 箭头偏移量，仅在 placement 为 *-end/*-start 时有效 |
| portalContainer | `HTMLElement \| (() => HTMLElement)` | - | 否 | 指定 portal 挂载容器，默认 document.body |
| content | `ReactNode` | - | 否 | 内容 |
| visible | `boolean` | - | 否 | popper 是否显示控制值 |
| headline | `ReactNode` | - | 否 | 标题 |
| actions | `ReactNode` | - | 否 | 操作区 |
| actionsAlign | `"right" \| "center" \| "left"` | - | 否 | 操作区对齐方式 |
| withCloseX | `boolean` | - | 否 | 是否有关闭小叉 |
| onClickCloseX | `(e: MouseEvent<Element, MouseEvent>) => void` | - | 否 | 点击关闭小叉回调 |
| size | `"sm" \| "md"` | - | 否 | 尺寸，影响 headline/content 字体大小及按钮尺寸 |
| target | `(() => HTMLElement \| null \| void) \| HTMLElement \| null` | - | 否 | 指定目标元素，不接受 ReactNode 或模拟的 ReferenceObject |
| defaultVisible | `boolean` | - | 否 | popper 是否显示默认值 |
| beforeChange | `(visible: boolean) => boolean \| void \| Promise<unknown>` | - | 否 | visible 变化前回调，返回 false 或 reject 可阻止变化 |
| mouseEnterDelay | `number` | `150` | 否 | mouseEnter 延迟触发（毫秒） |
| mouseLeaveDelay | `number` | `60` | 否 | mouseLeave 延迟触发（毫秒） |
| focusDelay | `number` | `150` | 否 | focus 延迟触发（毫秒） |
| blurDelay | `number` | - | 否 | blur 延迟触发（毫秒） |
| shouldHideParent | `boolean` | - | 否 | 是否隐藏时隐藏 parent |
| shouldHideOnMousedownDocument | `boolean \| ((e: Event) => boolean)` | - | 否 | type click 时点击外部隐藏，设为 false 可禁用，也可传回调自定义判断 |

## 典型用法

### 基础 Popover

```tsx
import { Popover, Button, Input } from '@befe/brick'

// click 触发，带标题和关闭小叉
<Popover headline="有个标题" content="一小段说明文字这类的" withCloseX>
    <Button type="important">Basic</Button>
</Popover>

// hover 触发，带操作项
<Popover
    type="hover"
    placement="bottom-start"
    content="气泡宽度默认按内容自适应，总宽超过 400px 折行显示"
    actions={[<Button key="op" type="plain" color="brand">操作项</Button>]}
    actionsAlign="left"
>
    <span>hover</span>
</Popover>

// focus 触发（输入框场景）
<Popover content="这里是一小段信息" type="focus">
    <Input placeholder="focus to show" />
</Popover>
```

### 多方位 placement

```tsx
import { Popover, Button } from '@befe/brick'

<Popover headline="标题" placement="top-start" content="这里是一段内容的文案" withCloseX>
    <Button>top-start</Button>
</Popover>
```

### 确认气泡（PopoverConfirm）

```tsx
import { PopoverConfirm, Button } from '@befe/brick'

<PopoverConfirm
    content="请再次确认要进行操作"
    onConfirm={() => console.log('confirm')}
    onCancel={() => console.log('cancel')}
>
    <Button>简单确认</Button>
</PopoverConfirm>

// 带类型（info / success / warning / error）
<PopoverConfirm type="error" content="这是一段确认信息">
    <Button type="intensive" color="danger">危险操作</Button>
</PopoverConfirm>
```

### 异步确认后提交

> 二次确认后提交时，loading 效果应控制在 Button 的 `props.loading`，而非控制 PopoverConfirm 的 `props.visible`。

```tsx
const [isSubmitting, setIsSubmitting] = React.useState(false)

const submit = async () => {
    setIsSubmitting(true)
    await asyncSleep(2000)
    setIsSubmitting(false)
}

<PopoverConfirm onConfirm={submit} content="是否提交？">
    <Button type="intensive" loading={isSubmitting}>Async Submit</Button>
</PopoverConfirm>
```

### beforeChange 打开前校验

```tsx
import { Popover, Button } from '@befe/brick'

<Popover
    content="一小段说明文字这类的"
    beforeChange={(visible) => {
        if (visible && !confirm('假设我们做了一些校验')) return false
        return true
    }}
>
    <Button>before change</Button>
</Popover>
```

## 注意事项

- 非控制型 PopoverConfirm 在 `onConfirm` 时会立即关闭，不支持根据（同步或异步的）运行结果决定是否关闭
- 二次确认场景中，loading 效果应通过 Button 的 `props.loading` 实现，不应通过控制 PopoverConfirm 的 `props.visible` 实现
- 提交前的校验应作为前道工序，用 `beforeChange` 根据校验结果决定是否打开浮层；返回 `false` 或 `Promise.reject()` 均可阻止弹出
- 若浮层内容复杂、需要用户进一步输入，应考虑 Popover 乃至 Dialog，而非轻量的 PopoverConfirm
- `target` 只接受 HTMLElement 或返回 HTMLElement 的函数，不接受 ReactNode

