💄(component): 完善 input 与 textarea 组件

This commit is contained in:
就眠儀式 2022-07-09 03:10:51 +08:00
parent 44f1d68e54
commit a939c48b79
7 changed files with 96 additions and 36 deletions

View File

@ -4,7 +4,7 @@
<lay-input <lay-input
readonly readonly
:name="name" :name="name"
:value="dateValue || modelValue" :model-value="dateValue || modelValue"
prefix-icon="layui-icon-date" prefix-icon="layui-icon-date"
> >
</lay-input> </lay-input>

View File

@ -13,7 +13,6 @@ import { useI18n } from "../../language";
export interface LayInputProps { export interface LayInputProps {
name?: string; name?: string;
type?: string; type?: string;
value?: string;
prefixIcon?: string; prefixIcon?: string;
suffixIcon?: string; suffixIcon?: string;
modelValue?: string; modelValue?: string;
@ -46,13 +45,13 @@ const emit = defineEmits<InputEmits>();
const { t } = useI18n(); const { t } = useI18n();
const slots = useSlots(); const slots = useSlots();
const currentValue = ref<string>(String(props.modelValue)); const currentValue = ref<string>(String(props.modelValue == null ? '' : props.modelValue));
const hasContent = computed(() => (props.modelValue as string)?.length > 0); const hasContent = computed(() => (props.modelValue as string)?.length > 0);
watch( watch(
() => props.modelValue, () => props.modelValue,
() => { () => {
currentValue.value = String(props.modelValue); currentValue.value = String(props.modelValue == null ? '' : props.modelValue);
} }
); );

View File

@ -17,6 +17,11 @@
min-height: 100px; min-height: 100px;
padding: 6px 10px; padding: 6px 10px;
resize: vertical; resize: vertical;
position: relative;
}
.layui-textarea-wrapper {
position: relative;
} }
.layui-textarea:hover { .layui-textarea:hover {
@ -27,6 +32,13 @@
border-color: #d2d2d2 !important; border-color: #d2d2d2 !important;
} }
.layui-textarea-clear {
position: absolute;
color: rgba(0, 0, 0, 0.45);
right: 10px;
top: 10px;
}
.layui-textarea::-webkit-input-placeholder { .layui-textarea::-webkit-input-placeholder {
line-height: 1.3; line-height: 1.3;
} }

View File

@ -14,28 +14,49 @@ export interface LayTextareaProps {
placeholder?: string; placeholder?: string;
disabled?: boolean; disabled?: boolean;
showCount?: boolean; showCount?: boolean;
allowClear?: boolean;
} }
const props = defineProps<LayTextareaProps>(); const props = defineProps<LayTextareaProps>();
const emit = defineEmits(["update:modelValue", "input", "focus", "blur"]); interface TextareaEmits {
(e: "blur", event: Event): void;
(e: "input", event: Event): void;
(e: "update:modelValue", value: string): void;
(e: "change", event: Event): void;
(e: "focus", event: Event): void;
(e: "clear"): void;
}
const emit = defineEmits<TextareaEmits>();
const attrs = useAttrs(); const attrs = useAttrs();
const onInput = function (event: InputEvent) { const onInput = function (event: Event) {
const inputElement = event.target as HTMLInputElement; const inputElement = event.target as HTMLInputElement;
emit("update:modelValue", inputElement.value); emit("update:modelValue", inputElement.value);
emit("input", inputElement.value); emit("input", event);
}; };
const onFocus = function (event: FocusEvent) { const onFocus = function (event: Event) {
emit("focus", event); emit("focus", event);
}; };
const onBlur = function () { const onBlur = function (event: Event) {
emit("blur"); emit("blur", event);
}; };
const onChange = (event: Event) => {
emit("change", event);
};
const onClear = function () {
emit("update:modelValue", "");
emit("clear");
};
const hasContent = computed(() => (props.modelValue as string)?.length > 0);
const wordCount = computed(() => { const wordCount = computed(() => {
let count = String(props.modelValue?.length ?? 0); let count = String(props.modelValue?.length ?? 0);
if (attrs.maxlength) { if (attrs.maxlength) {
@ -46,6 +67,7 @@ const wordCount = computed(() => {
</script> </script>
<template> <template>
<div class="layui-textarea-wrapper">
<textarea <textarea
:value="modelValue" :value="modelValue"
v-bind="$attrs" v-bind="$attrs"
@ -56,9 +78,14 @@ const wordCount = computed(() => {
:class="{ 'layui-disabled': disabled }" :class="{ 'layui-disabled': disabled }"
@input="onInput" @input="onInput"
@focus="onFocus" @focus="onFocus"
@change="onChange"
@blur="onBlur" @blur="onBlur"
></textarea> ></textarea>
<span class="layui-textarea-clear" v-if="allowClear && hasContent">
<lay-icon type="layui-icon-close-fill" @click="onClear"></lay-icon>
</span>
<div v-if="showCount" class="layui-texterea-show-count"> <div v-if="showCount" class="layui-texterea-show-count">
{{ wordCount }} {{ wordCount }}
</div> </div>
</div>
</template> </template>

View File

@ -14,7 +14,6 @@
::: demo ::: demo
<template> <template>
选择的时间:{{endTime}}
<lay-date-picker v-model="endTime"></lay-date-picker> <lay-date-picker v-model="endTime"></lay-date-picker>
</template> </template>
@ -41,7 +40,6 @@ export default {
::: demo ::: demo
<template> <template>
选择的时间:{{endTime2}}
<lay-date-picker type="datetime" v-model="endTime2"></lay-date-picker> <lay-date-picker type="datetime" v-model="endTime2"></lay-date-picker>
</template> </template>
@ -68,7 +66,6 @@ export default {
::: demo ::: demo
<template> <template>
<!-- 选择的时间:{{endTime3}} -->
<lay-date-picker disabled type="year" v-model="endTime3"></lay-date-picker> <lay-date-picker disabled type="year" v-model="endTime3"></lay-date-picker>
</template> </template>
@ -95,7 +92,6 @@ export default {
::: demo ::: demo
<template> <template>
选择的时间:{{endTime3}}
<lay-date-picker type="year" v-model="endTime3"></lay-date-picker> <lay-date-picker type="year" v-model="endTime3"></lay-date-picker>
</template> </template>
@ -122,7 +118,6 @@ export default {
::: demo ::: demo
<template> <template>
选择的时间:{{endTime4}}
<lay-date-picker type="month" v-model="endTime4"></lay-date-picker> <lay-date-picker type="month" v-model="endTime4"></lay-date-picker>
</template> </template>
@ -198,10 +193,7 @@ export default {
::: title 一次性选择 ::: title 一次性选择
::: :::
::: describe 只需要点击一次后自动关闭,无需点击确认按钮 ::: demo 只需要点击一次后自动关闭,无需点击确认按钮
:::
::: demo
<template> <template>
<lay-date-picker v-model="endTime7" simple></lay-date-picker> <lay-date-picker v-model="endTime7" simple></lay-date-picker>

View File

@ -91,6 +91,32 @@ export default {
::: :::
::: title 清空内容
:::
::: demo
<template>
<lay-textarea placeholder="请输入内容" v-model="data3" allow-clear></lay-textarea>
</template>
<script>
import { ref } from 'vue'
export default {
setup() {
const data3 = ref("");
return {
data3
}
}
}
</script>
:::
::: title 显示字数 ::: title 显示字数
::: :::

View File

@ -16,10 +16,14 @@
<li> <li>
<h3>1.2.8 <span class="layui-badge-rim">2022-07-08</span></h3> <h3>1.2.8 <span class="layui-badge-rim">2022-07-08</span></h3>
<ul> <ul>
<li>[优化] cascader 组件 change 回调函数。 by @SmallWai</li>
<li>[修复] layer 组件 Notifiy 缺失关闭图标。 by @SmallWai</li> <li>[修复] layer 组件 Notifiy 缺失关闭图标。 by @SmallWai</li>
<li>[修复] input 组件 modelValue 设置为 zero 不显示的问题。by @Jmysy</li> <li>[修复] input 组件 modelValue 设置为 zero 不显示的问题。by @Jmysy</li>
<li>[新增] textarea 组件 allow-clear 属性, 允许清空。by @Jmysy</li>
<li>[新增] textarea 组件 change 回调函数。by @Jmysy</li>
<li>[新增] textarea 组件 clear 回调函数。by @Jmysy</li>
<li>[新增] cascader 组件 replaceFields属性 用于自义定字段。by @SmallWai</li> <li>[新增] cascader 组件 replaceFields属性 用于自义定字段。by @SmallWai</li>
<li>[优化] cascader 组件 change 回调函数。 by @SmallWai</li>
<li>[删除] input 组件 value 属性, 与 v-model 属性冲突。by @Jmysy</li>
</ul> </ul>
</li> </li>
</ul> </ul>