V1.0.0
Skip to content

WInput 组件

输入框组件,基于 WinDesignNext WInput 封装,支持用户输入、数据绑定和事件响应。

基础用法

Props API

属性名类型默认值说明
idstring-组件唯一标识(必填)
componentstringWInput组件类型(必填)
typestringtext输入类型
sizestring''输入框尺寸
disabledbooleanfalse是否禁用
readonlybooleanfalse是否只读
placeholderstring''占位文本
clearablebooleanfalse是否可清空
showPasswordbooleanfalse是否显示密码切换
showWordLimitbooleanfalse是否显示字数统计
maxlengthnumber-最大输入长度
minlengthnumber-最小输入长度
rowsnumber2文本域行数(textarea 时)
autosizebooleanfalse是否自适应高度(textarea)
resizestringnone是否可缩放(textarea)
autofocusbooleanfalse是否自动聚焦
autocompletestringoff自动补全
tabindexnumber0Tab 键顺序
validateEventbooleantrue是否触发表单校验
modelValueobject-绑定值(A2UI v0.9 数据绑定,支持 path
actionobject-A2UI v0.9 事件定义

path 支持说明

通用绑定格式见 path 数据绑定速查

属性支持 path说明
modelValue双向绑定输入值(字符串)
action.event.context[].value事件上下文可引用 dataModel
action.event.api.constParams[].valueAPI 固定参数可引用 dataModel
placeholder / disabled / type静态配置;动态变更请用 updateProp

绑定示例:

json
{
  "modelValue": { "path": "/username" },
  "action": {
    "event": {
      "name": "onInput",
      "context": [{ "key": "value", "value": { "path": "/username" } }]
    }
  }
}

modelValue 数据绑定

modelValue 用于将输入值绑定到 dataModel:

json
{
  "modelValue": { "path": "/username" }
}

当用户输入时,组件会自动将新值同步到 dataModel 的指定路径。

action 定义

json
{
  "action": {
    "event": {
      "name": "onInput",
      "context": [{ "key": "value", "value": { "literalString": "输入的值" } }]
    }
  }
}

输入时触发,context 中的 value 为当前输入内容。


使用示例

基础输入框

json
{
  "id": "input_001",
  "component": "WInput",
  "type": "text",
  "placeholder": "请输入",
  "modelValue": { "path": "/name" },
  "action": {
    "event": { "name": "onNameInput" }
  }
}

配合 WForm 使用

json
[
  {
    "id": "form_item_name",
    "component": "WFormItem",
    "label": "姓名",
    "children": ["input_name"]
  },
  {
    "id": "input_name",
    "component": "WInput",
    "modelValue": { "path": "/name" },
    "action": {
      "event": {
        "name": "onNameInput",
        "context": [{ "key": "value", "value": { "path": "/name" } }]
      }
    }
  }
]

事件处理

输入事件

用户输入时自动触发,context 携带当前输入值:

typescript
processor.onEvent((event) => {
  const eventName = event.message.userAction?.name;
  const context = event.message.userAction?.context;

  if (eventName === 'onNameInput') {
    // context: { value: "用户输入的值" }
    console.log('输入值:', context);
    event.resolve([]);
  }
});

数据同步

WInput 在输入时会自动将值同步到 dataModel:

  1. 用户输入 → 更新本地值
  2. 通过 setData(component, modelValue.path, val, surfaceId) 同步到 dataModel
  3. 发送 action.event 事件
  4. 其他组件(如 WButton)可通过 path 读取最新值
json
// WButton 提交时读取 WInput 的最新值
{
  "action": {
    "event": {
      "name": "onSubmit",
      "context": [{ "key": "name", "value": { "path": "/name" } }]
    }
  }
}