﻿# Import template {#import-template}

Template download and file import step card, preserving the legacy props, events and slots. It emits notifications only; the application handles downloads and upload requests.

## Download and import {#examples}

### Download and import
```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="Download template" btn-text="Download template" tip="Download a template, then select a file" @click="downloads++" />
  <nf-import-template mode="upload" order="2" description="Choose file" btn-text="Choose file" tip="Excel files only" @upload="upload" />
  <p>click: {{ downloads }} / File: {{ fileName }}</p>
</template>
```

## Interactive example {#interactive}


See expanded example example-2 (ImportTemplateExamples).


## Installation {#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 {#props}

| Prop | Type | Default |
| --- | --- | --- |
| order | string | `'1'` |
| description | string | `''` |
| btnText | string | `''` |
| tip | string | `''` |
| subTip | string | `''` |
| tipDirection | vertical / horizontal | vertical |
| mode | downLoad / download / upload | downLoad |
| uploadMineType | string[] (extensions) | `['.xlsx', '.xls']` |
| limitSize | number (MiB, file size < limit) | 50 |
| showError | boolean | false |
| errorText | string | `''` |
| loading | boolean | false |

## Events {#events}

| Event | Payload |
| --- | --- |
| click | MouseEvent (downLoad / download) |
| upload | File (upload) |
| emit-error | — (showError) |

## Slots {#slots}

| Slot | Content |
| --- | --- |
| default | description |
| tip | tip + subTip |

## Migration notes {#migration}

The actual defaults are `mode="downLoad"` and `tipDirection="vertical"`; `download` is also accepted. Extensions are case-insensitive. File size must be strictly less than `limitSize` MiB. Cancelled selections are safe, and the same file can be selected again. Applications must validate content and server limits.

`showError`: `errorText || lastValidFileName`. `loading`: no click/upload. `$openMessage.error` → `ElMessage.error`.

## 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>
```
