Skip to content

useInput composable

The useInput composable is designed to simplify the creation of custom input components in Vue. While it streamlines the process, note that type safety in the template is limited. However, there are workarounds to address this limitation.

To use useInput, define your component and pass the necessary props. The composable returns useful properties and methods for managing input state and validation.

vue
<script setup lang="ts">
import { useInput, type InputProps } from 'vue-formify';

const props = defineProps<InputProps>();
const { inputProps, getError } = useInput(props);

</script>

<template>
	<div>
		<label>Custom Input</label>
		<div>
			<input v-bind="inputProps" />
			<small class="color-red">{{ getError() }}</small>
		</div>
	</div>
</template>

API Reference

Composable Arguments

ArgumentTypeDescription
propsInputProps | { name: string }Props required for the input. The name property is mandatory.
optionsUseInputOptionOptional configuration (see below).

options fields:

OptionTypeDescription
isArraybooleanIf true, sets the default input value to an empty array. Default is false.
isComponentbooleanSet to true for third-party components to avoid a duplicate value update on input.
isCheckboxbooleanSet to true when the input is a checkbox so the default value falls back to falseValue.

Returned Variables

VariableDescription
valueThe current field value (writable computed)
inputPropsBindings required for the input element
isValidIndicates if the field is valid
isDirtytrue when the value differs from initial
isTouchedtrue once the field has been touched

Returned Methods

FunctionParameterDescription
getError() => stringReturns the error message for the field.
setValue(value: any) => voidSets the field value.
setError(error: any) => voidSets an error message on the field.