@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

Search Field v1.0

The <e-search-field> component is a component that can be used as a search input field.

Usage

<e-search-field
  :model="fieldData"
/>

Attributes

ValueTypeOptionalDefault
modelEFormFieldModelno
idStringyes'search'
placeholderStringyes'search.searchPlaceholder'
valueStringyes''
direction['default', 'inverted']yes'default'

Sandbox

Examples

Below a few interactive examples of e-search-field can be found.

Default
<e-search-field
  id='search-field'
  :model="fieldData"
/>

Source Code

e-search-field.vue
<script setup lang="ts">
import { computed, ref, watch } from 'vue';
import { useI18n } from 'vue-i18n';

import { EFormFieldModel, EFormFieldType, ValueUpdate } from 'types';

import EIcon from '../e-icon/e-icon.vue';
import EFormField from '../e-form-field/e-form-field.vue';

export type Props = {
  model?: EFormFieldModel;
  id?: string;
  placeholder?: string;
  value?: string;
  direction?: 'default' | 'inverted';
};

const props = withDefaults(defineProps<Props>(), {
  model: null,
  id: 'search',
  placeholder: 'search.searchPlaceholder',
  value: '',
  direction: 'default'
});

const emit = defineEmits<Events>();

interface Events {
  'search-for': [value: string];
}

const { t, te } = useI18n({ useScope: 'global' });

const classes = computed(() => {
  return {
    inverted: props.direction === 'inverted'
  };
});

const searchValue = ref('');

const translatedPlaceholder = computed(() => {
  return te(props.placeholder) ? t(props.placeholder) : props.placeholder;
});

const searchFieldModel = computed(() => {
  if (props.model) return props.model;

  return {
    type: EFormFieldType.Text,
    id: props.id,
    value: searchValue.value,
    placeholder: translatedPlaceholder.value,
    hidden: false,
    meta: {},
    options: []
  } as EFormFieldModel;
});

function setSearchValue(value: ValueUpdate): void {
  searchValue.value = value.newValue as string;
}
function updateSearchValue(e: KeyboardEvent) {
  const textValue = (e.target as HTMLInputElement).value;
  searchValue.value = textValue;

  emit('search-for', searchValue.value);
}

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

<template>
  <div
    class="e-search-field"
    :class="classes"
  >
    <e-icon
      :icon="['search']"
      style-key="static"
    />
    <e-form-field
      class="search-field"
      :model="searchFieldModel"
      @emit:value="setSearchValue"
      @keyup.enter="updateSearchValue"
    />
  </div>
</template>

<style scoped lang="scss">
.e-search-field {
  display: flex;
  align-items: center;
  gap: 12px;

  .search-field {
    flex: 1 1 auto;
  }

  .e-icon {
    width: 28px;
  }

  &.inverted {
    flex-direction: row-reverse;
  }
}
</style>
Last Updated:: 11/1/24, 10:37 AM
Contributors: Antony Elfferich, Marcel Lommers
Next
Form Field