@econnect/webcomponents-library
Documentation
Styleguide
Documentation
Styleguide
  • Getting Started

    • Installation
    • Patch Notes
    • Styleguide
  • Styling

    • e-connect colors
  • UI Components

    • Form

      • Search Field
      • Form Field
      • Forms
    • Filter

      • Filter
      • Search Filter
    • Grid

      • Grid
      • Grid Row
    • Inputs

      • File Input
      • Input Checkbox
      • Input Dropdown
      • Input Date
      • Input Date Time
      • Input Date Time Local
      • Input File
      • Input Label
      • Input Multi Select
      • Input Number
      • Input Radio
      • Input Range
      • Input Select
      • Input Switch
      • Input Slider
      • Input Text
      • Input TextArea
      • Input Time
      • Input Time Range
    • Controls

      • Booleans
      • Button
      • Checkboxes
      • Date Displays
      • Dropdown Lists
      • Text Displays
    • Headers

      • e-header
    • Cards

      • e-card
    • Images

      • Icon
      • Images
      • Flag Image
      • Hamburger
    • Iframe
    • Pager
    • Settings
    • Document Validation
    • Navigation

      • Navigation
      • Navigation Menu
      • Navigation Footer
      • Navigation Button
      • Navigation Button Item
      • Navigation Search Item

Input Time v1.0

The <e-input-time> component is a component that can be used for .

Usage

An e-input-time has attributes that must be provided

<e-input-time
  id=""
  value=""
  label=""
  name=""
  placeholder=""
  required=""
  disabled=""
  meta=""
/>

Attributes : e-input-time extends e-input

ValueTypeOptionalDefault
valueStringyesnew Date().toTimeString()
metaEInputMeta_Timeyesnew EInputMeta_Time()

Attributes : e-input

ValueTypeOptionalDefault
idStringyes''
valueStringyes''
labelStringyes''
nameStringyes''
placeholderStringyes''
requiredBooleanyesfalse
disabledBooleanyesfalse
metaEInputMetayes{ disabled: false }

Attributes : e-input-meta_time

ValueTypeOptionalDefault
disabledBooleanyesfalse
minHourMinuteTimeStringyes'00:00'
maxHourMinuteTimeStringyes'23:59'

Examples

Below a few interactive examples of e-input-time can be found.

Default
<e-input-time
  id="timeInputWithOptions"
  value="-"
/>
internal labelled, label="timeInput with Label"
<e-input-time
  id="timeInputWithLabel"
  label="timeInput with Label"
  value="-"
/>
separate labelled, label="timeInput with Label"
<div class="input-example-field">
  <e-input-label
    id="timeInputWithExternalLabel"
    value="timeInput with Label"
  />
  <e-input-time
    id="timeInputWithExternalLabel"
    value="-"
  />
</div>

Source Code

e-input-time.vue
<script lang="ts" setup>
import { computed, ref, watch } from 'vue';
import { useDateFormat } from '@vueuse/core';

import { EInput_Time } from 'types/e-input/interfaces/EInput_Time';

export type Props = EInput_Time;
const props = withDefaults(defineProps<Props>(), {
  value: new Date().toTimeString(),
  name: '',
  label: '',
  id: '',
  placeholder: '',
  required: false,
  disabled: false,
  meta: () => {
    return {
      disabled: false,
      min: '00:00',
      max: '23:59'
    };
  }
});

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(props.value));
const timePattern = ref('HH:mm');

const localTime = useDateFormat(localValue, timePattern);

const minTime = computed(() => props.meta?.min || '00:00');
const maxTime = computed(() => props.meta?.max || '23:59');

const timeInputData = computed(() => {
  return {
    id: props.id,
    name: props.name,
    inputValue: localTime.value,
    disabled: isDisabled.value,
    placeholder: props.placeholder,
    type: 'time',
    required: props.required,
    pattern: timePattern.value,
    min: minTime.value,
    max: maxTime.value
  };
});

const isDisabled = computed(() => props.disabled ?? props.meta?.disabled);

const stateClasses = computed(() => {
  return {
    required: props.required,
    disabled: isDisabled.value
  };
});

function setLocalValue(timeString: string) {
  const currentDate = new Date();

  const isValidTime = timeString.match(timeStringRegex);
  const time = isValidTime
    ? timeString.split(':')
    : [`${currentDate.getUTCHours()}`, `${currentDate.getUTCMinutes()}`];

  const newHours = Number.parseInt(time[0]);
  const newMinutes = Number.parseInt(time[1]);
  localValue.value.setUTCHours(newHours, newMinutes, 0);
}

function setLocalTime(event: InputEvent) {
  const target = event.target as HTMLInputElement;
  const newValue = target.value;

  const isValid = target.checkValidity();

  if (isValid && Date.parse(newValue)) {
    setLocalValue(target.value);
    emitUpdate();
  }
}

function updateLocalValue(newValue: string) {
  localValue.value = new Date(newValue);
  localValue.value.setSeconds(0);
}

const emitUpdate = () => {
  const newValue = localValue.value;

  const hours = newValue.getUTCHours();
  const minutes = newValue.getUTCMinutes();
  const UTCTime = `${hours}:${minutes}`;

  emit('update:value', UTCTime);
};

watch(
  () => props.value,
  (newValue, oldValue) => {
    if (Date.parse(newValue) && oldValue !== newValue) {
      updateLocalValue(newValue);
    }
  },
  {
    immediate: true
  }
);
</script>

<template>
  <input
    :id="timeInputData.id"
    ref="input"
    class="e-input-time"
    :classes="stateClasses"
    :name="timeInputData.name"
    :value="timeInputData.inputValue"
    :disabled="timeInputData.disabled"
    :placeholder="timeInputData.placeholder"
    :type="timeInputData.type"
    :required="timeInputData.required"
    :pattern="timeInputData.pattern"
    :min="timeInputData.min"
    :max="timeInputData.max"
    @input="setLocalTime"
    @blur="emit('blur', $event)"
    @keyup.enter="emit('keyup.enter', $event)"
  />
</template>

<style scoped lang="scss">
.e-input-time {
  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);
  }

  &:user-invalid,
  &:out-of-range {
    color: var(--e-red);
  }

  &:disabled {
    color: var(--e-disabled);

    ::-webkit-calendar-picker-indicator {
      color: var(--e-disabled);
    }
  }

  &[required] {
    + label::after {
      content: '*';
      padding-left: 2px;
      color: var(--e-required);
      font-weight: bold;
    }
  }
}
</style>
Last Updated:: 12/16/24, 2:58 PM
Contributors: Marcel Lommers
Prev
Input TextArea
Next
Input Time Range