Skip to content

OcTextInput component

Description

OcTextInputs allow users to provide text input. Commonly used when the expected input is short. This component has a range of options and supports several input types, including numbers. For longer input, use the OcTextarea component.

Accessibility

The label is required and represents the name of the input.

The description-message can be used additionally to give further information about the input field. When a description is given, it will be referenced via the aria-describedby property automatically. An error or warning will replace the description as well as the aria-describedby property until the error or warning is fixed.

Examples

Default

The default and most simple use case involves a v-model and a label.

vue
<template>
  <oc-text-input v-model="name" label="Your name" />
  <p>So your name is {{ name }}? Nice to meet you!</p>
</template>

<script setup lang="ts">
import { ref } from 'vue'

const name = ref<string>('John Doe')
</script>

Disabled

vue
<oc-text-input disabled label="Disabled" model-value="I am disabled" />

Input Types

The following input types ares supported.

vue
<oc-text-input class="oc-mb-s" label="Text" />
<oc-text-input class="oc-mb-s" read-only="true" label="Read only" value="I am read only" />
<oc-text-input class="oc-mb-s" type="number" label="Number" />
<oc-text-input class="oc-mb-s" type="email" label="Email" />
<oc-text-input class="oc-mb-s" type="password" label="Password" />

Interactions

vue
<template>
  <oc-button class="oc-my-m" @click="focus">Focus input below</oc-button>
  <oc-text-input ref="inputForFocusRef" label="Focus field" />
  <oc-button class="oc-my-m" @click="focusAndSelect">Focus and select input below</oc-button>
  <oc-text-input
    ref="inputForFocusSelectRef"
    label="Select field"
    model-value="Text to be selected"
  />
  <oc-text-input v-model="inputValueForClearing" label="Clear input" :clear-button-enabled="true" />
  <oc-text-input
    v-model="inputValueWithDefault"
    label="Input with default"
    :clear-button-enabled="true"
    default-value="Some default"
  />

  <p>Value: {{ inputValueWithDefault !== null ? inputValueWithDefault : 'null' }}</p>
</template>

<script setup lang="ts">
import { ref, unref } from 'vue'

const inputValueForClearing = ref<string>('clear me')
const inputValueWithDefault = ref<string | null>(null)

const inputForFocusRef = ref(null)
const focus = () => {
  unref(inputForFocusRef).focus()
}
const inputForFocusSelectRef = ref(null)
const focusAndSelect = () => {
  unref(inputForFocusSelectRef).focus()
}
</script>

Messages

vue
<template>
  <oc-text-input
    label="Input with description message below"
    class="oc-mb-s"
    description-message="This is a description message."
    :fix-message-line="true"
  />
  <oc-text-input
    v-model="valueForMessages"
    label="Input with error with reserved space below"
    class="oc-mb-s"
    :error-message="errorMessage"
    :fix-message-line="true"
  />
  <oc-text-input
    v-model="valueForMessages"
    label="Input with error without reserved space below"
    class="oc-mb-s"
    :error-message="errorMessage"
  />
</template>

<script setup lang="ts">
import { computed, ref, unref } from 'vue'

const valueForMessages = ref<string>('')

const errorMessage = computed(() => {
  return unref(valueForMessages).length === 0 ? 'Value is required.' : ''
})
</script>

Properties

Emits

Slots