﻿# 导入模板 {#import-template}

模板下载与文件导入步骤卡片，保留旧版属性、事件与插槽。组件只通知业务处理，不自行下载文件或发起上传请求。

## 下载与导入 {#examples}

### 下载与导入
```vue
<script setup>
import { ref } from 'vue'
const downloads = ref(0)
const fileName = ref('')
function upload(file) { fileName.value = file.name }
</script>

<template>
  <nf-import-template description="下载模板" btn-text="下载模板" tip="先下载模板，再选择文件" @click="downloads++" />
  <nf-import-template mode="upload" order="2" description="选择文件" btn-text="选择文件" tip="仅支持 Excel 文件" @upload="upload" />
  <p>click: {{ downloads }} / File: {{ fileName }}</p>
</template>
```

## 交互体验 {#interactive}


See expanded example example-2 (ImportTemplateExamples).


## 按需引入 {#installation}

```ts
import NfImportTemplate from '@94ai-plus/nf-import-template'
import 'element-plus/dist/index.css'
import '@94ai-plus/nf-theme-chalk/nf-import-template.css'

app.use(NfImportTemplate)
```

## 属性 {#props}

| 属性 | 说明 | 类型／可选值 | 默认值 |
| --- | --- | --- | --- |
| order | 步骤前缀 | string | `'1'` |
| description | 步骤描述 | string | `''` |
| btnText | 按钮文本 | string | `''` |
| tip | 提示 | string | `''` |
| subTip | 子提示 | string | `''` |
| tipDirection | 提示排列方向 | vertical / horizontal | vertical |
| mode | 下载／导入模式 | downLoad / download / upload | downLoad |
| uploadMineType | 支持的文件扩展名，不是 MIME 类型 | string[] | `['.xlsx', '.xls']` |
| limitSize | 文件大小必须严格小于该值（MiB） | number | 50 |
| showError | 显示错误模式 | boolean | false |
| errorText | 错误文本；空值显示上次有效文件名 | string | `''` |
| loading | 操作按钮加载中，阻止下载与上传 | boolean | false |

## 事件 {#events}

| 事件 | 说明 | 参数 |
| --- | --- | --- |
| click | 下载模式点击按钮 | MouseEvent |
| upload | 导入模式选择文件，扩展名和大小校验通过 | File（原始文件） |
| emit-error | 错误态点击文本或图标，支持键盘 Enter／空格 | — |

## 插槽 {#slots}

| Slot | 说明 |
| --- | --- |
| default | 自定义 description，保留步骤号 |
| tip | 自定义 tip 与 subTip |

## 迁移说明 {#migration}

实际默认值为 `mode="downLoad"`、`tipDirection="vertical"`，另兼容 `download`。扩展名校验不区分大小写；大小必须严格小于 `limitSize` MiB。修复取消选文件时报错，支持重复选择同一文件。业务须自行校验文件内容和服务端限制。

支持单包安装、总包具名导出及 resolver 自动导入。Chalk/OEM 保留旧版悬浮背景图，并跟随统一组件主题切换。校验消息随文档／Provider 的语言切换，显式业务文案由业务控制。

错误提示优先使用应用的 `$openMessage.error`，未配置时使用 `ElMessage.error`。错误模式显示 `errorText` 或上次有效文件名；文件名主体超过 20 字符时截断，并保留扩展名。

## Expanded component examples

### ImportTemplateExamples

Source: docs/.vitepress/theme/components/ImportTemplateExamples.vue

```vue
<script setup lang="ts">
import { ref } from 'vue'
import { NfImportTemplate, type ImportTemplateMode, type ImportTemplateTipDirection } from '@94ai-plus/common-ui'

const mode = ref<ImportTemplateMode>('downLoad')
const tipDirection = ref<ImportTemplateTipDirection>('vertical')
const loading = ref(false)
const showError = ref(false)
const errorText = ref('')
const extensions = ref('.xlsx,.xls')
const limitSize = ref(50)
const customSlots = ref(false)
const downloads = ref(0)
const uploads = ref(0)
const errors = ref(0)
const lastFile = ref('')
function upload(file: File) {
  uploads.value++
  lastFile.value = `${file.name} (${file.size} bytes)`
}
</script>

<template>
  <section class="import-examples">
    <div class="import-controls">
      <label>mode <select v-model="mode" data-testid="import-mode"><option>downLoad</option><option>download</option><option>upload</option></select></label>
      <label>tipDirection <select v-model="tipDirection" data-testid="import-direction"><option>vertical</option><option>horizontal</option></select></label>
      <label>uploadMineType <input v-model="extensions" data-testid="import-extensions"></label>
      <label>limitSize (MiB) <input v-model.number="limitSize" type="number" min="0" step="0.001" data-testid="import-limit"></label>
      <label>errorText <input v-model="errorText" data-testid="import-error-text"></label>
      <label><input v-model="loading" type="checkbox" data-testid="import-loading">loading</label>
      <label><input v-model="showError" type="checkbox" data-testid="import-error">showError</label>
      <label><input v-model="customSlots" type="checkbox" data-testid="import-slots">slots</label>
    </div>
    <NfImportTemplate data-testid="interactive-import" :mode="mode" order="2" description="导入模板 / Import template" btn-text="操作 / Action" tip="Excel (.xlsx, .xls)" sub-tip="Max 50 MiB" :tip-direction="tipDirection" :upload-mine-type="extensions.split(',').map(item => item.trim())" :limit-size="limitSize" :loading="loading" :show-error="showError" :error-text="errorText" @click="downloads++" @upload="upload" @emit-error="errors++">
      <template v-if="customSlots" #default><strong data-testid="import-description-slot">Custom description</strong></template>
      <template v-if="customSlots" #tip><em data-testid="import-tip-slot">Custom tip</em></template>
    </NfImportTemplate>
    <output data-testid="import-result">click: {{ downloads }}; upload: {{ uploads }}; error: {{ errors }}; {{ lastFile }}</output>
  </section>
</template>

<style scoped>
.import-controls { display: flex; flex-wrap: wrap; gap: 12px; margin-bottom: 20px; }
.import-controls label { display: inline-flex; align-items: center; gap: 5px; font-size: 12px; }
.import-controls input:not([type='checkbox']) { width: 120px; }
.import-controls input, .import-controls select { border: 1px solid var(--site-border); border-radius: 4px; padding: 4px; background: var(--site-background); color: var(--site-text); }
.import-examples output { font-size: 12px; overflow-wrap: anywhere; }
</style>
```
