> For the complete documentation index, see [llms.txt](https://ak-scripts.gitbook.io/docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://ak-scripts.gitbook.io/docs/yi-lai/markdown/ui-mo-kuai-dao-chu/shu-ru-input.md).

# 输入（Input）

## ✍️ Input（输入框）模块导出文档

### 📋 概述

Input 模块是 ak-lib 的输入系统，支持表单验证、多字段输入、下拉框、日期选择、复选框等多种输入类型。

### 🚀 导出方式

#### 客户端

```lua
exports['ak-lib']:Input(data, callback)
exports['ak-lib']:OpenInput(data, callback)
exports['ak-lib']:CloseInput()
exports['ak-lib']:IsInputOpen()
exports['ak-lib']:GetCurrentInput()
```

#### 服务端

```lua
exports['ak-lib']:Input(target, data, callback)
exports['ak-lib']:OpenInput(target, data, callback)
```

### 📖 可用导出

#### 客户端

| 导出                          | 说明        | 参数                                    | 返回值                   |
| --------------------------- | --------- | ------------------------------------- | --------------------- |
| `Input(data, callback)`     | 打开输入框     | `data` (table), `callback` (function) | boolean               |
| `OpenInput(data, callback)` | 打开输入框（别名） | `data` (table), `callback` (function) | boolean               |
| `CloseInput()`              | 关闭输入框     | -                                     | boolean               |
| `IsInputOpen()`             | 检查输入框是否打开 | -                                     | boolean, table (当前数据) |
| `GetCurrentInput()`         | 获取当前输入框数据 | -                                     | table / nil           |

#### 服务端

| 导出                                  | 说明             | 参数                                                       | 返回值 |
| ----------------------------------- | -------------- | -------------------------------------------------------- | --- |
| `Input(target, data, callback)`     | 向指定玩家打开输入框     | `target` (number), `data` (table), `callback` (function) | nil |
| `OpenInput(target, data, callback)` | 向指定玩家打开输入框（别名） | `target` (number), `data` (table), `callback` (function) | nil |

### 💡 使用示例

#### 客户端：基础使用

```lua
-- 单字段输入（文本）
exports['ak-lib']:Input({
    title = '输入框',
    fields = {
        {
            label = '姓名',
            type = 'text',
            placeholder = '请输入姓名'
        }
    }
}, function(result)
    if result then
        print("输入结果:", result[1])
    else
        print("取消输入")
    end
end)

-- 多字段输入
exports['ak-lib']:Input({
    title = '角色创建',
    fields = {
        {
            label = '名字',
            type = 'text',
            placeholder = '请输入名字'
        },
        {
            label = '姓氏',
            type = 'text',
            placeholder = '请输入姓氏'
        },
        {
            label = '年龄',
            type = 'number',
            placeholder = '请输入年龄',
            min = 18,
            max = 100
        }
    }
}, function(result)
    if result then
        local firstName = result[1]
        local lastName = result[2]
        local age = tonumber(result[3])
        print(string.format("姓名: %s %s, 年龄: %d", firstName, lastName, age))
    end
end)

-- 密码输入
exports['ak-lib']:Input({
    title = '登录',
    fields = {
        {
            label = '密码',
            type = 'password',
            placeholder = '请输入密码'
        }
    }
}, function(result)
    if result then
        print("密码已输入")
    end
end)

-- 下拉选择
exports['ak-lib']:Input({
    title = '选择',
    fields = {
        {
            label = '性别',
            type = 'select',
            options = {
                { label = '男', value = 'male' },
                { label = '女', value = 'female' }
            }
        }
    }
}, function(result)
    if result then
        print("选择了:", result[1])
    end
end)

-- 日期选择
exports['ak-lib']:Input({
    title = '选择日期',
    fields = {
        {
            label = '出生日期',
            type = 'date'
        }
    }
}, function(result)
    if result then
        print("选择的日期:", result[1])
    end
end)

-- 多行输入
exports['ak-lib']:Input({
    title = '备注',
    fields = {
        {
            label = '备注',
            type = 'textarea',
            placeholder = '请输入备注'
        }
    }
}, function(result)
    if result then
        print("备注:", result[1])
    end
end)

-- 复选框
exports['ak-lib']:Input({
    title = '同意条款',
    fields = {
        {
            label = '我已阅读并同意条款',
            type = 'checkbox'
        }
    }
}, function(result)
    if result and result[1] then
        print("已同意")
    else
        print("未同意")
    end
end)

-- 表单验证
exports['ak-lib']:Input({
    title = '注册',
    fields = {
        {
            label = '用户名',
            type = 'text',
            required = true,
            minLength = 3,
            maxLength = 20
        },
        {
            label = '邮箱',
            type = 'text',
            required = true,
            pattern = 'email'
        },
        {
            label = '年龄',
            type = 'number',
            required = true,
            min = 18,
            max = 100
        }
    }
}, function(result)
    if result then
        print("验证通过，注册成功")
    end
end)

-- 检查输入框是否打开
local isOpen, data = exports['ak-lib']:IsInputOpen()
if isOpen then
    print("输入框已打开")
end

-- 关闭输入框
exports['ak-lib']:CloseInput()
```

#### 服务端：向玩家发送输入框

```lua
-- 向指定玩家打开输入框
exports['ak-lib']:Input(source, {
    title = '输入名称',
    fields = {
        {
            label = '名称',
            type = 'text'
        }
    }
}, function(result)
    if result then
        print("玩家输入了:", result[1])
    end
end)
```

### 📝 参数说明

#### data 参数（table）

| 字段         | 类型       | 说明      | 必需  | 默认值                     |
| ---------- | -------- | ------- | --- | ----------------------- |
| `title`    | string   | 输入框标题   | 否   | `'输入框'`                 |
| `fields`   | table\[] | 字段列表    | ✅ 是 | -                       |
| `position` | string   | 位置（九宫格） | 否   | `Config.Input.Position` |

#### field 参数（table）

| 字段            | 类型       | 说明              | 必需  | 默认值                        |
| ------------- | -------- | --------------- | --- | -------------------------- |
| `label`       | string   | 字段标签            | ✅ 是 | -                          |
| `type`        | string   | 字段类型            | ✅ 是 | `Config.Input.DefaultType` |
| `placeholder` | string   | 占位符文本           | 否   | -                          |
| `required`    | boolean  | 是否必填            | 否   | `false`                    |
| `minLength`   | number   | 最小长度            | 否   | -                          |
| `maxLength`   | number   | 最大长度            | 否   | `Config.Input.MaxLength`   |
| `min`         | number   | 最小值（number 类型）  | 否   | -                          |
| `max`         | number   | 最大值（number 类型）  | 否   | -                          |
| `pattern`     | string   | 验证模式            | 否   | -                          |
| `options`     | table\[] | 选项列表（select 类型） | 否   | -                          |
| `default`     | any      | 默认值             | 否   | -                          |

#### 字段类型（type）

* `'text'` - 普通文本
* `'number'` - 数字
* `'password'` - 密码（遮挡显示）
* `'select'` - 下拉选择
* `'date'` - 日期选择
* `'textarea'` - 多行输入
* `'checkbox'` - 复选框

#### 验证模式（pattern）

* `'email'` - 邮箱格式
* 其他正则表达式字符串

#### callback 回调函数

```lua
function(result, canceled)
    -- result: table / nil - 输入结果数组，取消时为 nil
    -- canceled: boolean - 是否取消
end
```

**返回值：**

* 提交成功：`result` 为数组，包含每个字段的值
* 取消输入：`result` 为 `nil`

#### 位置选项（position）

支持九宫格位置（默认：`'middle-center'`）：

* `'top-left'` - 左上角
* `'top-center'` - 顶部中间
* `'top-right'` - 右上角
* `'middle-left'` - 左边中间
* `'middle-center'` - 屏幕中央（默认）
* `'middle-right'` - 右边中间
* `'bottom-left'` - 左下角
* `'bottom-center'` - 底部中间
* `'bottom-right'` - 右下角

### ⚠️ 注意事项

1. **fields 必需**：`fields` 字段是必需的，至少包含一个字段。
2. **同时只能显示一个**：Input 同时只能显示一个，新的会替换旧的。
3. **回调函数**：回调函数在提交或取消时调用，`result` 为数组，包含每个字段的值。
4. **表单验证**：使用 `required`、`minLength`、`maxLength`、`min`、`max`、`pattern` 等字段进行验证。
5. **字段值顺序**：回调函数中的 `result` 数组按照 `fields` 的顺序返回值。
6. **复选框值**：复选框类型返回 `true` 或 `false`（boolean）。
7. **服务端调用**：服务端调用时需要提供 `target` 参数（玩家服务器 ID）。
8. **输入长度限制**：默认最大长度为 `Config.Input.MaxLength`（128），可以通过 `maxLength` 字段自定义。

### 🔗 相关文档

* 输入框说明


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://ak-scripts.gitbook.io/docs/yi-lai/markdown/ui-mo-kuai-dao-chu/shu-ru-input.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
