﻿# nf-form-item {#form-item}

A companion form item preserving native validation, label/error slots and collapse registration.

Import the components below; see the package README for standalone installation.

## Examples {#examples}

### Basic usage {#example-1}

### Basic usage
```vue
<script setup>
import { reactive, ref } from 'vue'
import { ElInput } from 'element-plus'
import { NfConfigProvider, NfForm, NfFormItem, NfButton } from '@94ai-plus/common-ui'
const form = ref()
const model = reactive({ name: '', email: '', city: '', team: '' })
const result = ref('')
const rules = { name: [{ required: true, message: 'Name is required', trigger: 'blur' }] }
async function validate() { try { await form.value.validate(); result.value = 'valid' } catch { result.value = 'invalid' } }
</script>

<template>
  <NfConfigProvider theme="chalk" locale="zh-CN">
    <NfForm ref="form" :model="model" :rules="rules" label-width="80px" :columns="3" @is-collapse-change="expanded => result = 'expanded: ' + expanded">
      <NfFormItem v-for="key in Object.keys(model)" :key="key" :label="key" :prop="key"><ElInput v-model="model[key]" /></NfFormItem>
      <NfFormItem>
        <NfButton type="primary" @click="validate">Validate</NfButton>
        <NfButton @click="form.resetFields()">Reset</NfButton>
      </NfFormItem>
    </NfForm>
    <output>{{ result }}</output>
  </NfConfigProvider>
</template>
```

### More examples 2 {#example-2}

### More examples 2
```vue
<script setup>
import { reactive } from 'vue'
import { ElInput } from 'element-plus'
import { NfConfigProvider, NfForm, NfFormItem } from '@94ai-plus/common-ui'
const model = reactive({ value: '' })
</script>

<template>
  <NfConfigProvider theme="chalk" locale="zh-CN">
    <NfForm :model="model" :show-btn="false" label-width="100px" size="mini" disabled>
      <NfFormItem label="Inherited size" prop="value">
        <ElInput v-model="model.value" placeholder="Disabled by form" />
        <template #label="{ label }"><strong>{{ label }}</strong></template>
        <template #error="{ error }"><em>{{ error }}</em></template>
      </NfFormItem>
    </NfForm>
  </NfConfigProvider>
</template>
```

## Props {#props}

Text props follow the library language when omitted; defaults below use this language.

| Parameter | Description | Type | Options | Default |
| --- | --- | --- | --- | --- |
| `hidden` | Exclude the item from collapsing when true; the item remains visible. | `boolean` | — | `false` |
| `prop` | Field in the form model; required for validation and reset, supports path arrays. | `string \| string[]` | — | `undefined` |
| `label` | Label text. | `string` | — | `undefined` |
| `labelWidth` | Label width, e.g. 50px; supports auto and inherits the form setting when omitted. | `string \| number` | — | `undefined` |
| `labelPosition` | Label position; inherits the form setting when omitted. | `string` | left / right / top | `''` |
| `required` | Whether required; determined by validation rules when omitted. | `boolean` | — | `undefined` |
| `rules` | Validation rules using the Element Plus / async-validator format. | `FormItemRule \| FormItemRule[]` | — | `undefined` |
| `error` | Field error message; setting it immediately enters the error state and displays it. | `string` | — | `undefined` |
| `showMessage` | Whether to show validation error messages. | `boolean` | — | `true` |
| `inlineMessage` | Display validation messages inline. | `boolean` | — | `undefined` |
| `size` | Control size; mini maps to small and medium to default. | `string` | mini / small / medium / default / large | `undefined` |
| `validateStatus` | Field validation state. | `string` | error / validating / success | `undefined` |
| `for` | Native label for attribute, referencing an input ID. | `string` | — | `undefined` |

## Events {#events}

No component events. Listen to validate on the parent NfForm for validation results.

## Slots {#slots}

| Parameter | Description | Payload |
| --- | --- | --- |
| `default` | Form Item content: field controls or action buttons. | — |
| `label` | Custom label content. | `{ label: string }` |
| `error` | Custom rendering of validation messages. | `{ error: string }` |

## Methods {#methods}

| Name | Type | Description |
| --- | --- | --- |
| `validate(trigger, callback?) / resetField() / clearValidate()` | `native` | Element Plus field methods. |

## Migration notes {#migration}

Use with NfForm. hidden excludes the item from collapse; it does not hide it. Use Vue 3 v-model and Element Plus rules rather than private Element UI instance properties.
