跳到主要内容

表单 schema 组件

uiSchema(form) 返回一个 schema table,描述界面的布局与字段。前端会把它渲染成 Vuetify 组件。

通用字段​

所有组件都支持以下字段:

字段类型说明
typestring组件类型,见下表
labelstring显示文本
fieldstring绑定到表单数据的字段名(表单值以它为键)
defaultany | function默认值;可以是函数,default() 的返回值作为默认值
visibleboolean | function是否显示;函数形式为 visible(form)
enableboolean | function是否可用;函数形式为 enable(form)
classstring附加到组件的 CSS 类
propstable透传给底层 Vuetify 组件的属性
备注

visible 与 enable 若为函数,会在框架处理 schema 时以当前表单值调用,转换为布尔值。

组件类型一览​

type用途
table纵向容器
flex弹性布局容器
group可折叠分组
text纯文本
input单行文本输入
number数字输入
select下拉选择
autocomplete自动补全输入
combobox可输入的下拉
checkbox复选框
radio单选
list列表(只读展示 + 删除)
checkboxSelectList多选列表
tableData表格数据
button按钮

table​

纵向容器,把子组件从上到下排列。

字段说明
children子组件数组
{ type = "table", children = {
{ type = "input", field = "a", label = "字段 A" },
{ type = "input", field = "b", label = "字段 B" },
} }

flex​

弹性布局容器,适合横向排列多个组件。

字段类型说明
direction"row" | "column"排列方向,默认 row
wrapboolean是否换行
gapnumber间距(像素)
childrenarray子组件数组

flex 的子组件还可以设置布局尺寸:

字段类型说明
widthnumber | string固定宽度(同时设置 min/max)
heightnumber | string固定高度
flexnumber弹性比例
{ type = "flex", direction = "row", wrap = true, gap = 8, children = {
{ type = "checkbox", field = "f1", label = "F1", width = 50, default = false },
{ type = "checkbox", field = "f2", label = "F2", width = 50, default = false },
} }
备注

schema 类型中还声明了 cols、align、justify 等布局提示,当前渲染层暂未使用,可留待后续扩展。

group​

可折叠分组,content 中放单个子组件。

字段说明
label分组标题
content单个子组件
{ type = "group", label = "高级设置", content = {
type = "flex", direction = "column", children = {
{ type = "checkbox", field = "debug", label = "调试模式" },
},
} }

text​

纯文本展示。

字段类型说明
labelstring文本内容
fontSizenumber字号(像素)
{ type = "text", label = "请先选择挂机点", fontSize = 13 }

input​

单行文本输入。

{ type = "input", field = "title", label = "任务名称", default = "" }

number​

数字输入。

字段类型说明
controlVariantstring控件样式:"default"、"split"、"hidden"、"stacked",默认 "hidden"
{ type = "number", field = "retry", label = "重试次数", default = 0 }

select / autocomplete / combobox​

三种选择型输入,均需提供 options。

字段类型说明
optionsarray | function选项数组,或返回选项数组的函数 options(form)
clearableboolean是否可清空

选项格式为 { label = ..., value = ... } 的数组。

{ type = "radio", field = "mode", label = "模式", options = {
{ label = "打死换怪", value = "打死换怪" },
{ label = "打红换怪", value = "打红换怪" },
}, default = "打死换怪" }

动态选项(根据当前表单值生成):

{ type = "select", field = "scene", label = "场景",
options = function(form)
local list = {}
for _, v in ipairs(getScenes()) do
table.insert(list, { label = v.name, value = v.id })
end
return list
end,
}

checkbox​

复选框。

{ type = "checkbox", field = "enable", label = "启用", default = false }

radio​

单选,需要 options。

{ type = "radio", field = "mode", label = "模式", options = {
{ label = "模式一", value = 1 },
{ label = "模式二", value = 2 },
} }

list​

只读列表,用于展示已经添加的条目,支持删除。常与 PushCommand 的 addItems 搭配。

字段类型说明
fieldstring绑定的字段(数组)
titleKeystring每行取哪个键显示,默认 label
heightnumber | string列表高度
deleteEnableboolean是否显示删除按钮,默认显示
{ type = "list", field = "points", label = "挂机点列表",
deleteEnable = true, height = 200, default = {} }

checkboxSelectList​

多选列表,用于从候选项中勾选若干项。

字段类型说明
fieldstring绑定的字段(选中值数组)
optionsarray | function候选项 { label, value }
heightnumber | string列表高度
{ type = "checkboxSelectList", field = "selectedItems", options = itemOptions }

tableData​

表格数据,按列展示数组,支持删除。常与 PushCommand 的 addRow 搭配。

字段类型说明
fieldstring绑定的字段(行数组)
columnsarray列定义 { title, key }
heightnumber | string表格高度
fixedHeaderboolean固定表头
deleteEnableboolean是否显示删除列
{ type = "tableData", field = "rows", height = 240, deleteEnable = true,
columns = {
{ title = "名称", key = "name" },
{ title = "数量", key = "count" },
},
}

button​

点击按钮,通过 event 与 click 绑定行为。

字段类型说明
labelstring按钮文本
eventstring事件名(需唯一)
clickfunction点击回调 click(form)
loadingBindTaskstring若该任务正在运行,按钮显示 loading 状态
classstringCSS 类
{ type = "button", label = "添加挂机点", event = "addPoint",
click = function(form)
local pos = hmPlayer.getCurrentPosition()
PushCommand("points", "addItems", {
{ label = pos.sceneName .. "(" .. pos.x .. "," .. pos.y .. ")" },
})
end,
}
提示

click 函数在界面环境中执行,可以使用 PushCommand、AddTask 等界面专属函数。

完整示例​

function main.uiSchema(form)
local items = {}
for _, name in ipairs(getItemNames()) do
table.insert(items, { label = name, value = name })
end

return {
type = "table",
children = {
{ type = "flex", direction = "row", gap = 8, children = {
{ type = "checkbox", field = "enable", label = "启用", default = false },
{ type = "number", field = "count", label = "数量", default = 1, width = 120 },
} },
{ type = "radio", field = "mode", label = "模式", default = 1, options = {
{ label = "模式一", value = 1 },
{ label = "模式二", value = 2 },
} },
{ type = "checkboxSelectList", field = "selectedItems", label = "物品", options = items, height = 200 },
{ type = "button", label = "添加", event = "add", class = "mt-4",
click = function(f)
PushCommand(nil, "openForm", { formName = "select", title = "选择物品" })
end,
},
},
}
end

下一步:表单模块与生命周期。