# Download

下载按钮组件，通过 iframe 方式触发文件下载，支持下载前拦截、下载开始/失败提示与国际化文案。

## 适用场景

- 点击按钮触发文件下载（GET/POST 请求）
- 需要在下载前进行权限检查或异步确认
- 需要展示下载开始/失败的提示信息
- 需要对下载提示文案做多语言（中英文）适配

## Props

### DownloadButton Props

| 名称 | 类型 | 默认值 | 必填 | 说明 |
|------|------|--------|------|------|
| className | `string` | `-` | 否 | 自定义 class |
| beforeDownload | `((e: MouseEvent<HTMLButtonElement, MouseEvent>) => boolean \| void \| Promise<unknown>) \| ((e: MouseEvent<HTMLAnchorElement, MouseEvent>) => boolean \| ... 1 more ... \| Promise<...>)` | `-` | 否 | download 执行前的回调，根据返回值确定是否执行：Promise resolve 后执行；返回 false 不执行；返回 true/void 立即执行 |
| url | `string` | `-` | 否 | 下载地址 |
| method | `DownloadIframeMethod` | `-` | 否 | 请求方法 |
| params | `Record<string, unknown>` | `-` | 否 | 请求参数 `{ [key]: string \| number }` |
| onResponse | `(respJson: Json, respText: string) => void` | `-` | 否 | 请求成功的回调 |
| isResponseSuccess | `(respJson: Json, respText: string) => boolean` | `-` | 否 | request 响应成功的判断函数 |
| startMessageDelay | `number` | `300` | 否 | 开始下载消息延迟出现时间；<=0 不触发延时，>0 延迟触发 |
| startMessageDuration | `number` | `-` | 否 | 开始下载消息持续时间 |
| size | `"xs" \| "sm" \| "md" \| "lg" \| "xl"` | `-` | 否 | 尺寸，默认使用 config context 的 `baseSize` |
| disabled | `boolean` | `-` | 否 | 是否禁用 |
| type | `"normal" \| "intensive" \| "important" \| "plain" \| "ghost"` | `-` | 否 | 类型 |
| color | `"normal" \| "brand" \| "success" \| "danger" \| "warning"` | `-` | 否 | 颜色；已废弃的 `primary` 仍会在运行时归一到 `brand`，但不再作为合法类型 |
| icon | `FC<{}>` | `-` | 否 | 图标 |
| loading | `boolean` | `-` | 否 | 控制是否处于 loading 状态；spin icon 按 loadingDelay 延迟显示，loading 态下 click 无效但无 disabled 样式 |
| loadingIcon | `SvgFC` | `-` | 否 | 自定义 loading Icon |
| loadingType | `"normal" \| "icon-only"` | `-` | 否 | loading 类型 |
| loadingDelay | `number` | `-` | 否 | loading 图标的延迟响应时间（ms），0 为立即显示 |
| href | `string` | `-` | 否 | `<a>` 链接跳转的地址；传入非 undefined 值时 Button 将渲染为 `<a>` |
| target | `string` | `-` | 否 | `<a>` 链接跳转的 target |
| download | `string` | `-` | 否 | `<a>` 的 download 属性，指示浏览器下载 href 资源而非导航打开 |
| onClick | `((e: MouseEvent<HTMLButtonElement, MouseEvent>) => void \| Promise<unknown>) \| ((e: MouseEvent<HTMLAnchorElement, MouseEvent>) => void \| Promise<...>)` | `-` | 否 | 点击回调；返回 Promise 会自动进入 async loading 状态直到 resolve（自动防连击），spin icon 按 loadingDelay 延迟显示 |
| refHTMLElement | `Ref<HTMLButtonElement> \| Ref<HTMLAnchorElement>` | `-` | 否 | ref html element |

### DownloadIframe Props

| 名称 | 类型 | 默认值 | 必填 | 说明 |
|------|------|--------|------|------|
| className | `string` | `-` | 否 | 自定义 class |
| url | `string` | `-` | 否 | 下载地址 |
| method | `DownloadIframeMethod` | `-` | 否 | 请求方法 |
| params | `Record<string, unknown>` | `-` | 否 | 请求参数 `{ [key]: string \| number }` |
| onResponse | `(respJson: Json, respText: string) => void` | `-` | 否 | 请求成功的回调 |
| isResponseSuccess | `(respJson: Json, respText: string) => boolean` | `-` | 否 | request 响应成功的判断函数 |
| startMessageDelay | `number` | `300` | 否 | 开始下载消息延迟出现时间；<=0 不触发延时，>0 延迟触发 |
| startMessageDuration | `number` | `-` | 否 | 开始下载消息持续时间 |

### LocaleDownload Props

| 名称 | 类型 | 默认值 | 必填 | 说明 |
|------|------|--------|------|------|
| download_start | `LocaleText` | `zh: '下载开始...'; en: 'Start downloading...'` | 是 | 下载开始提示 |
| download_fail | `LocaleText` | `zh: '下载失败'; en: 'Download failed'` | 是 | 下载失败提示 |

## 典型用法

### 基础下载

```tsx
import {Download} from '@befe/brick-comp-down_load'

const downloadUrl = 'https://example.com/file?id=xxx'

<Download url={downloadUrl}>导出</Download>
```

### 下载前拦截（beforeDownload）

```tsx
import {useState} from 'react'
import {toast} from '@befe/brick-comp-toast'
import {Switch} from '@befe/brick-comp-switch'
import {Download} from '@befe/brick-comp-down_load'

function Demo() {
    const [canDownload, setCanDownload] = useState(false)

    const beforeDownload = () => {
        if (!canDownload) {
            toast.warning('will not download')
            return false // 阻止下载
        }
        return undefined // 继续下载；也可 return true 或 return Promise.resolve()
    }

    return (
        <>
            <Switch checked={canDownload} size="md" onChange={setCanDownload} checkedLabel="Y" uncheckedLabel="N" />
            <Download beforeDownload={beforeDownload} url={downloadUrl}>导出</Download>
        </>
    )
}
```

## 注意事项

- `Download` 导出自 `@befe/brick-comp-down_load`（注意包名含下划线）
- 破坏性变更（2021）：`startMessageDelayInMS` 改名为 `startMessageDelay`，`startMessageDurationInMS` 改名为 `startMessageDuration`，`loadingDelayInMS` 改名为 `loadingDelay`
- `beforeDownload` 返回 `false` 会阻止下载，返回 Promise 会等待 resolve 后再执行下载
- `LocaleDownload` 的 `download_start` / `download_fail` 为必填项，用于下载开始/失败提示的多语言文案
- 若需要 ajax 方式下载，请参考 sack 工具库的 download 方法，不属于本组件范畴

