Appearance
InputDispatcher
Element Plus 的 el-input 组件分发器,添加了只读功能。
基本用法
vue
<template>
<el-input-dispatcher
ref="inputRef"
v-model="input"
style="width: 240px"
placeholder="Please input"
/>
</template>
<script lang="ts" setup>
import { ref, onMounted } from "vue";
const input = ref("abc");
</script>
隐藏源码
禁用状态
通过 disabled 属性指定是否禁用 input 组件
vue
<template>
<el-input-dispatcher
v-model="input"
style="width: 240px"
disabled
placeholder="Please input"
/>
</template>
<script lang="ts" setup>
import { ref } from "vue";
const input = ref("abc");
</script>
隐藏源码
格式化
在 formatter 的情况下显示值,我们通常同时使用 parser
vue
<template>
<el-input-dispatcher
v-model="input"
style="width: 240px"
placeholder="Please input"
:formatter="(value: string) => `$ ${value}`.replace(/\B(?=(\d{3})+(?!\d))/g, ',')"
:parser="(value: string) => value.replace(/\$\s?|(,*)/g, '')"
/>
</template>
<script lang="ts" setup>
import { ref } from "vue";
const input = ref("");
</script>
隐藏源码
密码框
使用 show-password 属性即可得到一个可切换显示隐藏的密码框
vue
<template>
<el-input-dispatcher
v-model="input"
style="width: 240px"
type="password"
placeholder="Please input password"
show-password
/>
</template>
<script lang="ts" setup>
import { ref } from "vue";
const input = ref("");
</script>
隐藏源码
带图标的输入框
带有图标标记输入类型
要在输入框中添加图标,你可以简单地使用 prefix-icon 和 suffix-icon 属性。 另外, prefix 和 suffix 命名的插槽也能正常工作。
Using attributes
Using slots
vue
<template>
<div class="flex flex-wrap gap-16px mb-16px">
<span class="label">Using attributes</span>
<el-input-dispatcher
v-model="input1"
style="width: 240px"
placeholder="Pick a date"
:suffix-icon="Calendar"
/>
<el-input-dispatcher
v-model="input2"
style="width: 240px"
placeholder="Type something"
:prefix-icon="Search"
/>
</div>
<div class="flex flex-wrap gap-16px">
<span class="label">Using slots</span>
<el-input-dispatcher
v-model="input3"
style="width: 240px"
placeholder="Pick a date"
>
<template #suffix>
<el-icon class="el-input__icon"><calendar /></el-icon>
</template>
</el-input-dispatcher>
<el-input-dispatcher
v-model="input4"
style="width: 240px"
placeholder="Type something"
>
<template #prefix>
<el-icon class="el-input__icon"><search /></el-icon>
</template>
</el-input-dispatcher>
</div>
</template>
<script setup lang="ts">
import { ref } from "vue";
import { Calendar, Search } from "@element-plus/icons-vue";
const input1 = ref("");
const input2 = ref("");
const input3 = ref("");
const input4 = ref("");
</script>
隐藏源码
复合型输入框
可以在输入框中前置或后置一个元素,通常是标签或按钮。
可通过 slot 来指定在 Input 中分发的前置或者后置的内容。
Http://
.com
Select
Select
vue
<template>
<div>
<el-input-dispatcher
v-model="input1"
style="max-width: 600px"
placeholder="Please input"
>
<template #prepend>Http://</template>
</el-input-dispatcher>
</div>
<div class="mt-16px">
<el-input-dispatcher
v-model="input2"
style="max-width: 600px"
placeholder="Please input"
>
<template #append>.com</template>
</el-input-dispatcher>
</div>
<div class="mt-16px">
<el-input-dispatcher
v-model="input3"
style="max-width: 600px"
placeholder="Please input"
class="input-with-select"
>
<template #prepend>
<el-select v-model="select" placeholder="Select" style="width: 115px">
<el-option label="Restaurant" value="1" />
<el-option label="Order No." value="2" />
<el-option label="Tel" value="3" />
</el-select>
</template>
<template #append>
<el-button :icon="Search" />
</template>
</el-input-dispatcher>
</div>
<div class="mt-16px">
<el-input-dispatcher
v-model="input3"
style="max-width: 600px"
placeholder="Please input"
class="input-with-select"
>
<template #prepend>
<el-button :icon="Search" />
</template>
<template #append>
<el-select v-model="select" placeholder="Select" style="width: 115px">
<el-option label="Restaurant" value="1" />
<el-option label="Order No." value="2" />
<el-option label="Tel" value="3" />
</el-select>
</template>
</el-input-dispatcher>
</div>
</template>
<script setup lang="ts">
import { ref } from "vue";
import { Search } from "@element-plus/icons-vue";
const input1 = ref("");
const input2 = ref("");
const input3 = ref("");
const select = ref("");
</script>
<style>
.input-with-select .el-input-group__prepend {
background-color: var(--el-fill-color-blank);
}
</style>
隐藏源码
尺寸
使用 size 属性改变输入框大小。 除了默认大小外,还有另外两个选项: large, small。
vue
<template>
<div class="flex gap-16px mb-16px items-center">
<el-input-dispatcher
v-model="input1"
style="width: 240px"
size="large"
placeholder="Please Input"
/>
<el-input-dispatcher
v-model="input2"
style="width: 240px"
placeholder="Please Input"
/>
<el-input-dispatcher
v-model="input3"
style="width: 240px"
size="small"
placeholder="Please Input"
/>
</div>
<div class="flex gap-16px mb-16px items-center">
<el-input-dispatcher
v-model="input1"
style="width: 240px"
size="large"
placeholder="Please Input"
:suffix-icon="Search"
/>
<el-input-dispatcher
v-model="input2"
style="width: 240px"
placeholder="Please Input"
:suffix-icon="Search"
/>
<el-input-dispatcher
v-model="input3"
style="width: 240px"
size="small"
placeholder="Please Input"
:suffix-icon="Search"
/>
</div>
<div class="flex gap-16px items-center">
<el-input-dispatcher
v-model="input1"
style="width: 240px"
size="large"
placeholder="Please Input"
:prefix-icon="Search"
/>
<el-input-dispatcher
v-model="input2"
style="width: 240px"
placeholder="Please Input"
:prefix-icon="Search"
/>
<el-input-dispatcher
v-model="input3"
style="width: 240px"
size="small"
placeholder="Please Input"
:prefix-icon="Search"
/>
</div>
</template>
<script lang="ts" setup>
import { ref } from "vue";
import { Search } from "@element-plus/icons-vue";
const input1 = ref("");
const input2 = ref("");
const input3 = ref("");
</script>
隐藏源码
Props
修改 props中的 ${Config.namespace}State 进行读写状态更改
ReadWrite
vue
<template>
<div>
<el-input-dispatcher
v-model="input"
:rw-dispatcher-state="rwState"
style="width: 240px"
placeholder="Please input"
/>
<p>
<el-switch
v-model="rwState"
active-value="write"
inactive-value="read"
active-text="Write"
inactive-text="Read"
@change="handleStateToggle"
>
Toggle State
</el-switch>
</p>
</div>
</template>
<script lang="ts" setup>
import { ref } from "vue";
import { type RWDispatcherState } from "element-plus-form-dispatcher/helper";
const input = ref("abc");
const rwState = ref<RWDispatcherState>("write");
const handleStateToggle = () => {
console.log(`Current stage: ${rwState.value}`);
};
</script>
隐藏源码
Slots
使用 slots 中的 ${Config.namespace}Reader, ${Config.namespace}Writer 覆盖读写状态渲染函数
writer: abc
vue
<template>
<el-input-dispatcher
v-model="input"
style="width: 240px"
placeholder="Please input"
>
<template #[`${Config.namespace}Reader`]>
<span style="color: green" key="reader">reader: {{ input }}</span>
</template>
<template #[`${Config.namespace}Writer`]>
<span style="color: red" key="writer">writer: {{ input }}</span>
</template>
</el-input-dispatcher>
</template>
<script lang="ts" setup>
import { ref } from "vue";
import { Config } from "element-plus-form-dispatcher";
const input = ref("abc");
</script>
隐藏源码
注意
建议在 slots 中加上 key, 避免切换读写状态出现渲染切换不过来的情况
如示例中 #${Config.namespace}Reader 加上了 key="reader", #${Config.namespace}Writer 加上了 key="writer"
API
与 el-input 的 props attrs slots 完全一致