Input Date v1.0
The <e-input-date> component is a component that can be used for date inputs.
Usage
An e-input-date has attributes that must be provided
<e-input-date
id=""
value=""
label=""
name=""
placeholder=""
required=""
disabled=""
meta=""
/>
Attributes : e-input-date extends e-input
| Value | Type | Optional | Default |
|---|---|---|---|
| value | String | yes | new Date().toDateString() |
| meta | EInputMeta_Date | yes | new EInputMeta_Date() |
Attributes : e-input
| Value | Type | Optional | Default |
|---|---|---|---|
| id | String | yes | '' |
| value | String | yes | '' |
| label | String | yes | '' |
| name | String | yes | '' |
| placeholder | String | yes | '' |
| required | Boolean | yes | false |
| disabled | Boolean | yes | false |
| meta | EInputMeta | yes | { disabled: false } |
Attributes : e-input-meta_date
| Value | Type | Optional | Default |
|---|---|---|---|
| disabled | Boolean | yes | false |
| min | RawDateString | yes | '1970-01-01' |
| max | RawDateString | yes | '9999-12-31' |
Examples
Below a few interactive examples of e-input-date can be found.
Default
<e-input-date
id="dateInputWithOptions"
value="-"
/>
internal labelled, label='dateInput with Label'
<e-input-date
id="dateInputWithLabel"
label="dateInput with Label"
value="-"
/>
separate labelled, label='dateInput with Label'
<div class="input-example-field">
<e-input-label
id="dateInputWithExternalLabel"
value="dateInput with Label"
/>
<e-input-date
id="dateInputWithExternalLabel"
value="-"
/>
</div>
Source Code
e-input-date.vue
<script lang="ts" setup>
import { computed, ref, watch } from 'vue';
import { useDateFormat } from '@vueuse/core';
import { EInput_Date } from 'types/e-input/interfaces/EInput_Date';
export type Props = EInput_Date;
const props = withDefaults(defineProps<Props>(), {
value: new Date().toDateString(),
name: '',
label: '',
id: '',
placeholder: '',
required: false,
disabled: false,
meta: () => {
return {
disabled: false,
min: '1970-01-01',
max: '9999-12-31'
};
}
});
const emit = defineEmits<{
(e: 'blur', event: FocusEvent): void;
(e: 'keyup.enter', event: KeyboardEvent): void;
(e: 'update:value', value: string): void;
}>();
const input = ref<HTMLInputElement>();
const localValue = ref(new Date());
const datePattern = ref('YYYY-MM-DD');
const localDate = useDateFormat(localValue.value, datePattern.value);
const isDisabled = computed(() => props.disabled ?? props.meta?.disabled);
const minDate = computed(() => props.meta?.min || '1970-01-01');
const maxDate = computed(() => props.meta?.max || '9999-12-31');
const dateInputData = computed(() => {
return {
id: props.id,
name: props.name,
inputValue: localDate.value,
disabled: isDisabled.value,
placeholder: props.placeholder,
type: 'date',
pattern: datePattern.value,
required: props.required,
min: minDate.value,
max: maxDate.value
};
});
const stateClasses = computed(() => {
return {
required: props.required,
disabled: isDisabled.value
};
});
async function setLocalDate(event: InputEvent) {
const target = event.target as HTMLInputElement;
const isValid = target.checkValidity();
if (isValid) {
updateLocalValue(target.valueAsDate.toString());
emitUpdate();
}
}
function updateLocalValue(newValue: string) {
localValue.value = new Date(newValue);
localValue.value.setHours(0, 0, 0, 0);
}
function emitUpdate() {
const dateString = localValue.value.toDateString();
emit('update:value', dateString);
}
watch(
() => props.value,
(newValue, oldValue) => {
if (Date.parse(newValue) && oldValue !== newValue) {
updateLocalValue(newValue);
}
},
{
immediate: true
}
);
</script>
<template>
<input
:id="dateInputData.id"
ref="input"
class="e-input-date"
:class="stateClasses"
:name="dateInputData.name"
:value="dateInputData.inputValue"
:disabled="dateInputData.disabled"
:placeholder="dateInputData.placeholder"
:type="dateInputData.type"
:required="dateInputData.required"
:pattern="dateInputData.pattern"
:min="dateInputData.min"
:max="dateInputData.max"
@input="setLocalDate"
@blur="emit('blur', $event)"
@keyup.enter="emit('keyup.enter', $event)"
/>
</template>
<style scoped lang="scss">
.e-input-date {
background-color: var(--e-input-background);
border: none;
border-radius: 15px;
color: var(--e-body-font-color);
display: flex;
flex: 1 1 auto;
flex-direction: row;
font: normal normal normal 16px/20px var(--e-body-font-family);
gap: 10px;
height: 50px;
padding: 15px;
&::placeholder {
color: var(--e-gray-300);
}
&:focus {
background-color: var(--e-input-focus-background);
}
&:not(:disabled) {
cursor: pointer;
}
&:user-invalid,
&:out-of-range {
color: var(--e-red);
}
&:disabled {
color: var(--e-disabled);
cursor: not-allowed;
user-input: none;
::-webkit-calendar-picker-indicator {
color: var(--e-disabled);
}
}
&[required] {
+ label::after {
content: '*';
padding-left: 2px;
color: var(--e-required);
font-weight: bold;
}
}
}
</style>