(component): 优化datePicker 新增readonly与allClear两个属性

This commit is contained in:
0o张不歪o0 2022-07-18 10:16:59 +08:00
parent cd7fdf4a07
commit 1c04089581
5 changed files with 50 additions and 45 deletions

View File

@ -79,6 +79,7 @@ const handleMonthClick = (item: any) => {
} }
if (datePicker.type === "date" || datePicker.type === "datetime") { if (datePicker.type === "date" || datePicker.type === "datetime") {
emits("update:modelValue", MONTH_NAME.indexOf(item)); emits("update:modelValue", MONTH_NAME.indexOf(item));
datePicker.showPanel.value = datePicker.type;
} }
} }
if (datePicker.simple) { if (datePicker.simple) {
@ -101,7 +102,11 @@ const footOnOk = () => {
emits("ok"); emits("ok");
return; return;
} else { } else {
datePicker.ok(); if(datePicker.type==='datetime'||datePicker.type==='date'){
datePicker.showPanel.value = datePicker.type;
}else{
datePicker.ok();
}
} }
}; };

View File

@ -42,10 +42,8 @@
class="laydate-btns-time" class="laydate-btns-time"
>选择日期</span >选择日期</span
> >
<template v-else> <template v-else-if="!isNaN(hms.hh)&&!isNaN(hms.mm)&&!isNaN(hms.ss)">
{{ hms.hh > 9 ? hms.hh : "0" + hms.hh }}:{{ {{dayjs().hour(hms.hh).minute(hms.mm).second(hms.ss).format('HH:mm:ss')}}
hms.mm > 9 ? hms.mm : "0" + hms.mm
}}:{{ hms.ss > 9 ? hms.ss : "0" + hms.ss }}
</template> </template>
</PanelFoot> </PanelFoot>
</div> </div>
@ -132,6 +130,9 @@ const footOnOk = () => {
return; return;
} else { } else {
datePicker.ok(); datePicker.ok();
if(datePicker.type==='datetime'){
datePicker.showPanel.value='date';
}
} }
}; };
// //

View File

@ -30,7 +30,7 @@
class="laydate-btns-time" class="laydate-btns-time"
>选择月份</span >选择月份</span
> >
<template v-else>{{ Year }}</template> <template v-else-if="Year>0">{{ Year }}</template>
</PanelFoot> </PanelFoot>
</div> </div>
</template> </template>
@ -71,7 +71,8 @@ const handleYearClick = (item: any) => {
datePicker.showPanel.value = "month"; datePicker.showPanel.value = "month";
emits("update:modelValue", Year.value); emits("update:modelValue", Year.value);
} else { } else {
datePicker.showPanel.value = "date"; emits("update:modelValue", Year.value);
datePicker.showPanel.value = datePicker.type;
} }
} }
@ -117,7 +118,11 @@ const footOnOk = () => {
emits("ok"); emits("ok");
return; return;
} else { } else {
datePicker.ok(); if(datePicker.type==='datetime'||datePicker.type==='date'){
datePicker.showPanel.value = datePicker.type;
}else{
datePicker.ok();
}
} }
}; };

View File

@ -1,37 +1,14 @@
<template> <template>
<div> <div>
<lay-dropdown <lay-dropdown ref="dropdownRef" :disabled="disabled" :autoFitMinWidth="false">
ref="dropdownRef" <lay-input :name="name" :readonly="readonly" v-model="(dateValue as string)" :placeholder="placeholder"
:disabled="disabled" prefix-icon="layui-icon-date" :disabled="disabled" v-if="!range" @change="onChange" :allow-clear="allowClear" @clear="dateValue='';onChange()">
:autoFitMinWidth="false"
>
<lay-input
readonly
:name="name"
:model-value="(dateValue as string)"
:placeholder="placeholder"
prefix-icon="layui-icon-date"
:disabled="disabled"
v-if="!range"
>
</lay-input> </lay-input>
<div class="laydate-range-inputs" v-else> <div class="laydate-range-inputs" v-else>
<lay-input <lay-input :readonly="readonly" :name="name" v-model="dateValue[0]" :disabled="disabled" @change="onChange" class="start-input">
readonly
:name="name"
:model-value="dateValue[0]"
:disabled="disabled"
class="start-input"
>
</lay-input> </lay-input>
<span class="range-separator">{{ rangeSeparator }}</span> <span class="range-separator">{{ rangeSeparator }}</span>
<lay-input <lay-input :readonly="readonly" :name="name" :allow-clear="allowClear" v-model="dateValue[1]" :disabled="disabled" @change="onChange" class="end-input" @clear="dateValue=[];onChange()">
readonly
:name="name"
:model-value="dateValue[1]"
:disabled="disabled"
class="end-input"
>
</lay-input> </lay-input>
</div> </div>
@ -115,6 +92,8 @@ export interface LayDatePickerProps {
min?: string; min?: string;
range?: boolean; range?: boolean;
rangeSeparator?: string; rangeSeparator?: string;
readonly?:boolean;
allowClear?:boolean;
} }
const props = withDefaults(defineProps<LayDatePickerProps>(), { const props = withDefaults(defineProps<LayDatePickerProps>(), {
@ -124,6 +103,8 @@ const props = withDefaults(defineProps<LayDatePickerProps>(), {
simple: false, simple: false,
range: false, range: false,
rangeSeparator: "至", rangeSeparator: "至",
readonly:false,
allowClear:true,
}); });
const dropdownRef = ref(null); const dropdownRef = ref(null);
@ -180,10 +161,10 @@ const getDateValue = () => {
.format("HH:mm:ss"); .format("HH:mm:ss");
break; break;
case "yearmonth": case "yearmonth":
dayjsVal = dayjs() dayjsVal = currentYear.value !== -1&&currentMonth.value !== -1?dayjs()
.year(currentYear.value) .year(currentYear.value)
.month(currentMonth.value) .month(currentMonth.value)
.format("YYYY-MM"); .format("YYYY-MM"):'';
break; break;
default: default:
dayjsVal = dayjsVal =
@ -196,7 +177,12 @@ const getDateValue = () => {
: ""; : "";
break; break;
} }
dateValue.value = dayjsVal; dateValue.value = dayjsVal!=='Invalid Date'?dayjsVal:'';
if (dayjsVal === 'Invalid Date') {
unWatch = false;
$emits("update:modelValue", '');
return;
}
$emits("update:modelValue", dayjsVal); $emits("update:modelValue", dayjsVal);
setTimeout(() => { setTimeout(() => {
unWatch = false; unWatch = false;
@ -274,7 +260,6 @@ watch(
hms.value.hh = dayjs(initModelValue).hour(); hms.value.hh = dayjs(initModelValue).hour();
hms.value.mm = dayjs(initModelValue).minute(); hms.value.mm = dayjs(initModelValue).minute();
hms.value.ss = dayjs(initModelValue).second(); hms.value.ss = dayjs(initModelValue).second();
if (initModelValue.length === 8 && props.type === "time") { if (initModelValue.length === 8 && props.type === "time") {
//dayjs //dayjs
let modelValue = initModelValue; let modelValue = initModelValue;
@ -283,10 +268,9 @@ watch(
hms.value.mm = dayjs(modelValue).minute(); hms.value.mm = dayjs(modelValue).minute();
hms.value.ss = dayjs(modelValue).second(); hms.value.ss = dayjs(modelValue).second();
} }
currentYear.value = getYear(initModelValue); currentYear.value = initModelValue?getYear(initModelValue):-1;
currentMonth.value = getMonth(initModelValue); currentMonth.value = initModelValue?getMonth(initModelValue):-1;
currentDay.value = getDay(initModelValue); currentDay.value = initModelValue?getDay(initModelValue):-1;
rangeValue.first = initModelValue; rangeValue.first = initModelValue;
rangeValue.last = rangeValue.last =
props.range && props.modelValue props.range && props.modelValue
@ -301,6 +285,13 @@ watch(
{ immediate: true } { immediate: true }
); );
const onChange=()=>{
if (dropdownRef.value)
// @ts-ignore
dropdownRef.value.hide();
$emits('update:modelValue',dateValue.value)
}
provide("datePicker", { provide("datePicker", {
currentYear: currentYear, currentYear: currentYear,
currentMonth: currentMonth, currentMonth: currentMonth,

View File

@ -259,7 +259,10 @@ const rangeTime3 = ref(['2022-01-01','2023-02-1']);
| v-model | 当前时间 | `string` | -- | — | | v-model | 当前时间 | `string` | -- | — |
| type | 选择类型 | `string` | `date` | `date` `datetime` `year` `month` `time` `yearmonth` | | type | 选择类型 | `string` | `date` | `date` `datetime` `year` `month` `time` `yearmonth` |
| disabled | 是否禁止修改 | `boolean` | false | — | | disabled | 是否禁止修改 | `boolean` | false | — |
| simple | 一次性选择,无需点击确认按钮 | `boolean` | false | — | | simple | 一次性选择,无需点击确认按钮 | `boolean` | false | — |
| readonly | 只读 | `boolean` | false | — |
| allowClear | 允许清空 | `boolean` | true | — |
::: :::