Skip to content

Input 输入框

规则

基础示例

js
const rule = {
    type:"input",
    title:"商品名称",
    field:"goods_name",
    value:"iphone 7",
    props: {
        type: "text",
    },
    validate:[
        { required: true, message: '请输入goods_name', trigger: 'blur' },
    ],
}

Props 配置示例

文本域(Textarea)

js
const rule = {
    type:"input",
    title:"商品描述",
    field:"description",
    props: {
        type: "textarea",
        placeholder: "请输入商品描述",
        rows: 4,
        autosize: { minRows: 3, maxRows: 6 },
        resize: "vertical",
    },
}

密码输入框(Password)

js
const rule = {
    type:"input",
    title:"商品描述",
    field:"description",
    props: {
        type: "password",
    },
}

可清空输入框

js
const rule = {
    type:"input",
    title:"用户名",
    field:"username",
    props: {
        placeholder: "请输入用户名",
        allowClear: true,
        prefix: "UserOutlined",
    },
}

显示字数统计

js
const rule = {
    type:"input",
    title:"商品描述",
    field:"description",
    props: {
        placeholder: "请输入商品描述",
        showCount: true,
        maxlength: 200,
    },
}

前后缀标签

js
const rule = {
    type:"input",
    title:"价格",
    field:"price",
    props: {
        addonBefore: "¥",
        addonAfter: "元",
        placeholder: "请输入价格",
    },
}

禁用状态

js
const rule = {
    type:"input",
    title:"订单号",
    field:"order_no",
    value:"ORD20240101001",
    props: {
        disabled: true,
    },
}

Events 事件示例

监听输入变化和焦点事件

js
const rule = {
    type:"input",
    title:"搜索关键词",
    field:"keyword",
    props: {
        placeholder: "请输入搜索关键词",
        allowClear: true,
    },
    on: {
        change: (e) => {
            console.log('输入值改变:', e.target.value);
        },
        pressEnter: (e) => {
            console.log('按下回车:', e.target.value);
        },
    },
}

实时搜索(change 事件)

js
const rule = {
    type:"input",
    title:"搜索商品",
    field:"keyword",
    props: {
        placeholder: "请输入商品名称搜索",
        allowClear: true,
        prefix: "SearchOutlined",
    },
    inject: true,
    on: {
        change: ($inject, e) => {
            // 实时搜索:输入时自动触发搜索
            const value = e.target.value;
            if (value && value.length >= 2) {
                // 调用搜索接口
                searchProducts(value).then(res => {
                    // 更新搜索结果
                    $inject.api.setValue('searchResults', res.data);
                });
            }
        },
    },
}

联动更新其他字段(change 事件)

js
const rule = [
    {
        type:"input",
        title:"商品名称",
        field:"goods_name",
        props: {
            placeholder: "请输入商品名称",
        },
        inject: true,
        on: {
            change: ($inject, e) => {
                // 当商品名称改变时,自动生成商品编码
                const value = e.target.value;
                if (value) {
                    const code = 'GD' + value.substring(0, 3).toUpperCase() + Date.now().toString().slice(-6);
                    $inject.api.setValue('goods_code', code);
                }
            },
        },
    },
    {
        type:"input",
        title:"商品编码",
        field:"goods_code",
        props: {
            disabled: true,
        },
    },
]

完整配置项:Ant-design-vue_Input

value :String

Props

参数说明类型默认值
addonAfter带标签的 input,设置后置标签string|slot
addonBefore带标签的 input,设置前置标签string|slot
allowClear可以点击清除图标删除内容boolean
bordered是否有边框booleantrue
clearIcon自定义清除图标 (allowClear 为 true 时生效)slot<CloseCircleFilled />
defaultValue输入框默认内容string
disabled是否禁用状态,默认为 falsebooleanfalse
id输入框的 idstring
maxlength最大长度number
prefix带有前缀图标的 inputstring|slot
showCount是否展示字数booleanfalse
status设置校验状态'error' | 'warning'-
size控件大小。注:标准表单内的输入框大小限制为 middle。可选 large middle smallstring-
suffix带有后缀图标的 inputstring|slot
type声明 input 类型,同原生 input 标签的 type 属性,见:MDN(请直接使用 <a-textarea /> 代替 type="textarea")。stringtext

Input 事件

事件名称说明回调参数
change输入框内容变化时的回调function(e)
pressEnter按下回车的回调function(e)

TextArea

参数说明类型默认值
allowClear可以点击清除图标删除内容boolean
autosize自适应内容高度,可设置为 `truefalse 或对象:{ minRows: 2, maxRows: 6 }`boolean|object
showCount是否展示字数booleanfalse

TextArea 事件

事件名称说明回调参数
pressEnter按下回车的回调function(e)

Textarea 的其他属性和浏览器自带的 textarea 一致。

参数说明类型默认值
enterButton是否有确认按钮,可设为按钮文字。该属性会与 addon 冲突。boolean|slotfalse
loading搜索 loadingboolean

Input.Search 事件

事件名称说明回调参数
search点击搜索或按下回车键时的回调function(value, event)

其余属性和 Input 一致。

Input.Password

参数说明类型默认值
visible密码是否可见booleanfalse
iconRender自定义切换按钮slot-
visibilityToggle是否显示切换按钮或者控制密码显隐booleantrue

FormCreate 是一个开源项目,基于 MIT 许可证发布,欢迎个人和企业用户免费使用