General
Below you will find an alphabetical listing of some of our input fields. Each input field has its own purpose and comes with specific features. However, there are also some global characteristics that apply equally to all input fields.
- Placeholders. Normally we do not use placeholder texts. On the one hand, this could give the wrong impression that some fields are already filled in. (At least when quickly skimming a form.) And on the other hand, the placeholder text and thus the additional information disappears as soon as the user clicks into the input field. For additional info on a particular input field, we would rather use the
wui-message
. - Input type. Input fields have many different
types
. Always make sure that the correct input type is used. This makes forms more accessible and improves usability. For example, using the correct input type on mobile devices invokes the appropriate keyboard (numbers vs. letters).
Email input
- Initial. As long as the input field is empty, the button is disabled.
- Functionality. Clicking the button at the end of the input field will open the user's e-mail program such as MS Outlook, Thunderbird or the like.
- Input-type.
type="email"
- Icon.
class="far fa-paper-plane"
- Readonly. Even if the input is disabled - e.g. in the readonly view - the button can still be clicked.
<div class="input-group">
<div class="wui-form-control-group wui-form-control-group--has-icon">
<input type="text" id="" name="" value="wui@wescale.com" autocomplete="off" class="wui-form-control" />
</div>
<span class="input-group-btn">
<button type="button" class="wui-btn wui-btn--default wui-btn--outline">
<i class="far fa-paper-plane"></i>
</button>
</span>
</div>
URL input
- Initial. As long as the input field is empty, the button is disabled.
- Functionality. Clicking the button at the end of the input field will open a HTTP link.
- Default. If no configuration option is set, the target is
_blank
and the link opens in a new tab. - Input-type.
type="url"
- Icon.
class="far fa-external-link-alt"
- Readonly. Even if the input is disabled - e.g. in the readonly view - the button can still be clicked.
<div class="input-group">
<div class="wui-form-control-group wui-form-control-group--has-icon">
<input type="text" id="" name="" value="https://styleguide.wescale.io/latest/" autocomplete="off" class="wui-form-control" />
</div>
<span class="input-group-btn">
<button type="button" class="wui-btn wui-btn--default wui-btn--outline">
<i class="far fa-external-link-alt"></i>
</button>
</span>
</div>
Remaining characters
<wui-form>
<wui-form-group
label="Basic input"
label-for="remaining-example"
>
<wui-form-remaining-characters
:max-length="10"
:show="true"
>
<wui-form-input id="remaining-example" />
</wui-form-remaining-characters>
</wui-form-group>
<wui-form-group
label="Input group"
label-for="remaining-group-example"
>
<wui-input-group>
<wui-form-remaining-characters
:max-length="10"
:show="true"
class="form-control border-0 p-0"
>
<wui-form-input id="remaining-group-example" />
</wui-form-remaining-characters>
<wui-input-group-append>
<wui-input-group-text><wui-fa-icon icon="search" /></wui-input-group-text>
</wui-input-group-append>
</wui-input-group>
</wui-form-group>
<wui-form-group
label="Textarea"
label-for="textarea-example"
>
<wui-form-remaining-characters
:max-length="10"
:show="true"
>
<wui-form-textarea
id="textarea-example"
:rows="4"
></wui-form-textarea>
</wui-form-remaining-characters>
</wui-form-group>
</wui-form>
import { WuiFormInput } from "@wui/wui-vue/lib/form-input";
Description
Create various type inputs such as:
text
,password
,number
,url
,search
,range
,date
and more.
<template>
<div>
<wui-form-input
v-model="text"
placeholder="Enter your name"
></wui-form-input>
<div class="m-t-5">Value: { text }</div>
</div>
</template>
<script>
export default {
data() {
return {
text: "",
};
},
};
</script>
<!-- wui-form-input.vue -->
Input type
<wui-form-input>
defaults to a text
input, but you can set the type
prop to one of the supported native browser HTML5 types: text
, password
, email
, number
, url
, tel
, search
, date
, datetime
, datetime-local
, month
, week
, time
, range
, or color
.
<template>
<wui-container fluid>
<wui-row class="m-y-5" v-for="type in types" :key="type">
<wui-col sm="3">
<label :for="`type-${type}`">Type <code>{ type }</code>:</label>
</wui-col>
<wui-col sm="9">
<wui-form-input :id="`type-${type}`" :type="type"></wui-form-input>
</wui-col>
</wui-row>
</wui-container>
</template>
<script>
export default {
data() {
return {
types: [
"text",
"number",
"email",
"password",
"search",
"url",
"tel",
"date",
"time",
"range",
"color",
],
};
},
};
</script>
<!-- wui-form-input-types.vue -->
If the type
prop is set to an input type that is not supported (see above), a text
input will be rendered and a console warning will be issued.
Caveats with input types:
- Not all browsers support all input types, nor do some types render in the same format across browser types/versions. Refer to Can I use.
- Browsers that do not support a particular type will fall back to a
text
input type (even though the renderedtype
attribute markup shows the requested type). - No testing is performed to see if the requested input type is supported by the browser.
- Chrome lost support for
datetime
in version 26, Opera in version 15, and Safari in iOS 7. Instead of usingdatetime
, since support should be deprecated, usedate
andtime
as two separate inputs. date
andtime
inputs are native browser types, and are not a custom date/time picker.- For date and time style inputs, where supported, the displayed value in the GUI may be different than what is returned by its value (i.e. ordering of year-month-date).
- Regardless of input type, the value is always returned as a string representation.
v-model.lazy
is not supported by<wui-form-input>
(nor any custom Vue component). Use thelazy
prop instead.v-model
modifiers.number
and.trim
can cause unexpected cursor jumps when the user is typing (this is a Vue issue withv-model
on custom components). Avoid using these modifiers. Use thenumber
ortrim
props instead.- Older version of Firefox may not support
readonly
forrange
type inputs. - Input types that do not support
min
,max
andstep
(i.e.text
,password
,tel
,email
,url
, etc.) will silently ignore these values (although they will still be rendered on the input markup) if values are provided.
Caveats with predictive text entry and IME composition entry:
- When using predictive text auto-suggested words, the
v-model
will not update until the auto-suggested word is selected (or a space is typed). If an auto suggested word is not selected, the v-model will update with the current displayed text of the input when the input is blurred. - When using IME composition (ie. Chinese, Japanese, etc.), the
v-model
will not update until the IME composition is completed.
Range type input
Inputs with type range
render using the .form-range
class. The track (the background) and thumb (the value) are both styled to appear the same across browsers.
Range inputs have implicit values for min
and max
of 0
and 100
respectively. You may specify new values for those using the min
and max
props.
<template>
<div>
<label for="range-1">Example range with min and max</label>
<wui-form-input
id="range-1"
v-model="value"
type="range"
min="0"
max="5"
></wui-form-input>
<div class="m-t-5">Value: { value }</div>
</div>
</template>
<script>
export default {
data() {
return {
value: "2",
};
},
};
</script>
<!-- wui-form-input-range.vue -->
By default, range inputs "snap" to integer values. To change this, you can specify a step
value. In the example below, we double the number of steps by using step="0.5".
<template>
<div>
<label for="range-2">Example range with step value</label>
<wui-form-input
id="range-2"
v-model="value"
type="range"
min="0"
max="5"
step="0.5"
></wui-form-input>
<div class="m-t-5">Value: { value }</div>
</div>
</template>
<script>
export default {
data() {
return {
value: "2",
};
},
};
</script>
<!-- wui-form-input-range-step.vue -->
Note: Range inputs (as do all input types) return their value as a string. You may need to convert the value to a native number by using Number(value)
, parseInt(value, 10)
, parseFloat(value)
, or use the number
prop.
Note: WUI CSS does not include styling for range inputs inside input groups, nor validation styling on range inputs. However, WuiVue includes custom styling to handle these situations until styling is included in WUI CSS.
Control sizing
Set heights using the size
prop to sm
or lg
for small or large respectively.
To control width, place the input inside standard WUI grid column.
<wui-container fluid>
<wui-row class="m-y-5">
<wui-col sm="2">
<label for="input-small">Small:</label>
</wui-col>
<wui-col sm="10">
<wui-form-input
id="input-small"
size="sm"
placeholder="Enter your name"
></wui-form-input>
</wui-col>
</wui-row>
<wui-row class="m-y-5">
<wui-col sm="2">
<label for="input-default">Default:</label>
</wui-col>
<wui-col sm="10">
<wui-form-input
id="input-default"
placeholder="Enter your name"
></wui-form-input>
</wui-col>
</wui-row>
<wui-row class="m-y-5">
<wui-col sm="2">
<label for="input-large">Large:</label>
</wui-col>
<wui-col sm="10">
<wui-form-input
id="input-large"
size="lg"
placeholder="Enter your name"
></wui-form-input>
</wui-col>
</wui-row>
</wui-container>
<!-- wui-form-input-size.vue -->
Note: Input type range
currently does not support control sizing unless it is placed inside a <wui-input-group>
which has its size
prop set.
Note: The native HTML <input>
attribute size
(which sets a horizontal width on the <input>
in characters) is not supported. Use styling, utility classes, or the layout rows (<wui-row>
) and columns (<wui-col>
) to set the desired width.
Contextual states
WUI includes validation styles for valid
(success
) and invalid
(error
, warning
) states on most form controls.
Generally speaking, you'll want to use a particular state for specific types of feedback:
false
orerror
(denotes invalid state) is great for when there's a blocking or required field. A user must fill in this field properly to submit the form.true
orsuccess
(denotes valid state) is ideal for situations when you have per-field validation throughout a form and want to encourage a user through the rest of the fields.warning
(denotes invalid state) is great for when there's a field with a value which is not an error but might be a risky value in the context of the submitted datanull
Displays no validation state (neither valid nor invalid)
To apply one of the contextual state icons on <wui-form-input>
, set the state
prop to false
or error
(for invalid), true
or success
(for valid), or null
(no validation state).
<wui-container fluid>
<wui-row class="m-y-5">
<wui-col sm="3">
<label for="input-none">No State:</label>
</wui-col>
<wui-col sm="9">
<wui-form-input
id="input-none"
:state="null"
placeholder="No validation"
></wui-form-input>
</wui-col>
</wui-row>
<wui-row class="m-y-5">
<wui-col sm="3">
<label for="input-valid">Valid State:</label>
</wui-col>
<wui-col sm="9">
<wui-form-input
id="input-valid"
:state="true"
placeholder="Valid input"
></wui-form-input>
</wui-col>
</wui-row>
<wui-row class="m-y-5">
<wui-col sm="3">
<label for="input-invalid">Invalid State:</label>
</wui-col>
<wui-col sm="9">
<wui-form-input
id="input-invalid"
:state="false"
placeholder="Invalid input"
></wui-form-input>
</wui-col>
</wui-row>
</wui-container>
<!-- wui-form-input-states.vue -->
Live Example
<template>
<div role="group">
<label for="input-live">Name:</label>
<wui-form-input
id="input-live"
v-model="name"
:state="nameState"
aria-describedby="input-live-help input-live-feedback"
placeholder="Enter your name"
trim
></wui-form-input>
<!-- This will only be shown if the preceding input has an invalid state -->
<wui-form-invalid-feedback id="input-live-feedback">
Enter at least 3 letters
</wui-form-invalid-feedback>
<!-- This is a form text block (formerly known as help block) -->
<wui-form-text id="input-live-help">Your full name.</wui-form-text>
</div>
</template>
<script>
export default {
computed: {
nameState() {
return this.name.length > 2;
},
},
data() {
return {
name: "",
};
},
};
</script>
<!-- wui-form-input-states-feedback.vue -->
Tip: Use the
<wui-form-group>
component to automatically generate markup similar to above.
Conveying contextual state to assistive technologies and colorblind users
Using these contextual states to denote the state of a form control only provides a visual, color-based indication, which will not be conveyed to users of assistive technologies - such as screen readers - or to colorblind users.
Ensure that an alternative indication of state is also provided. For instance, you could include a hint about state in the form control's <label>
text itself, or by providing an additional help text block.
ARIA aria-invalid
attribute
Specifically for assistive technologies, invalid form controls can also be assigned an aria-invalid="true"
attribute.
When <wui-form-input>
has an invalid contextual state (i.e. state is false
) you may also want to set the <wui-form-input>
prop aria-invalid
to true
, or to one of the supported values:
false
: Convey no errors detected (default)true
(or'true'
): Convey that the value has failed validation.'grammar'
Convey that a grammatical error has been detected.'spelling'
Convey that a spelling error has been detected.
If aria-invalid
is not explicitly set and state
is set to false
, then the aria-invalid
attribute on the input will automatically be set to 'true'
;
Formatter support
<wui-form-input>
optionally supports formatting by passing a function reference to the formatter
prop.
Formatting (when a formatter function is supplied) occurs when the control's native input
and change
events fire. You can use the boolean prop lazy-formatter
to restrict the formatter function to being called on the control's native blur
event.
The formatter
function receives two arguments: the raw value
of the input element, and the native event
object that triggered the format (if available).
The formatter
function should return the formatted value as a string.
Formatting does not occur if a formatter
is not provided.
<template>
<div>
<wui-form-group
label="Text input with formatter (on input)"
label-for="input-formatter"
description="We will convert your name to lowercase instantly"
class="m-b-0"
>
<wui-form-input
id="input-formatter"
v-model="text1"
placeholder="Enter your name"
:formatter="formatter"
></wui-form-input>
</wui-form-group>
<p><b>Value:</b> { text1 }</p>
<wui-form-group
label="Text input with lazy formatter (on blur)"
label-for="input-lazy"
description="This one is a little lazy!"
class="m-b-0"
>
<wui-form-input
id="input-lazy"
v-model="text2"
placeholder="Enter your name"
lazy-formatter
:formatter="formatter"
></wui-form-input>
</wui-form-group>
<p class="m-b-0"><b>Value:</b> { text2 }</p>
</div>
</template>
<script>
export default {
data() {
return {
text1: "",
text2: "",
};
},
methods: {
formatter(value) {
return value.toLowerCase();
},
},
};
</script>
<!-- wui-form-input-formatter.vue -->
Note: When using a non-text-like input (i.e. color
, range
, date
, number
, email
etc.), ensure that your formatter function returns the value in the expected format (date
-> '2000-06-01', color
-> '#ff0000', etc.) for the input type. The formatter must return the value as a string.
Note: With non-lazy formatting, if the cursor is not at the end of the input value, the cursor may jump to the end after a character is typed. You can use the provided event object and the event.target
to access the native input's selection methods and properties to control where the insertion point is. This is left as an exercise for the reader.
Readonly plain text
If you want to have <wui-form-input readonly>
elements in your form styled as plain text, set the plaintext
prop (no need to set readonly
) to remove the default form field styling and preserve the correct margin and padding.
The plaintext
option is not supported by input types color
or range
.
Disabling mousewheel events on numeric-like inputs
On some browsers, scrolling the mousewheel while a numeric-like input is focused will increment or decrement the input's value. To disable this browser feature, just set the no-wheel
prop to true
.
v-model
modifiers
Vue does not officially support .lazy
, .trim
, and .number
modifiers on the v-model
of custom component based inputs, and may generate a bad user experience. Avoid using Vue's native modifiers.
To get around this, <wui-form-input>
has three boolean props trim
, number
, and lazy
which emulate the native Vue v-model
modifiers .trim
and .number
and .lazy
respectively. The lazy
prop will update the v-model on change
/blur
events.
Notes:
- The
number
prop takes precedence over thetrim
prop (i.e.trim
will have no effect whennumber
is set). - When using the
number
prop, and if the value can be parsed as a number (viaparseFloat
) it will return a value of typeNumber
to thev-model
, otherwise the original input value is returned as typeString
. This is the same behaviour as the native.number
modifier. - The
trim
andnumber
modifier props do not affect the value returned by theinput
orchange
events. These events will always return the string value of the content of<textarea>
after optional formatting (which may not match the value returned via thev-model
update
event, which handles the modifiers).
Debounce support
As an alternative to the lazy
modifier prop, <wui-form-input>
optionally supports debouncing user input, updating the v-model
after a period of idle time from when the last character was entered by the user (or a change
event occurs). If the user enters a new character (or deletes characters) before the idle timeout expires, the timeout is re-started.
To enable debouncing, set the prop debounce
to any integer greater than zero. The value is specified in milliseconds. Setting debounce
to 0
will disable debouncing.
Note: debouncing will not occur if the lazy
prop is set.
<template>
<div>
<wui-form-input v-model="value" type="text" debounce="500"></wui-form-input>
<div class="m-t-5">Value: "{ value }"</div>
</div>
</template>
<script>
export default {
data() {
return {
value: "",
};
},
};
</script>
<!-- wui-form-input-debounce.vue -->
Autofocus
When the autofocus
prop is set, the input will be auto-focused when it is inserted (i.e. mounted) into the document, or re-activated when inside a Vue <keep-alive>
component. Note that this prop does not set the autofocus
attribute on the input, nor can it tell when the input becomes visible.
Native and custom events
All native events (other than the custom input
and change
events) are supported, without the need for the .native
modifier.
The custom input
and change
events receive a single argument of the current value
(after any formatting has been applied), and are triggered by user interaction.
The custom update
event is passed the input value, and is emitted whenever the v-model
needs updating (it is emitted before input
, change
. and blur
as needed).
You can always access the native input
and change
events by using the .native
modifier.
Exposed input properties and methods
<wui-form-input>
exposes several of the native input element's properties and methods on the component reference (i.e. assign a ref
to your <wui-form-input ref="foo" ...>
and use this.$refs['foo'].propertyName
or this.$refs['foo'].methodName(...)
).
Input properties
Property | Notes |
---|---|
.selectionStart | Read/Write |
.selectionEnd | Read/Write |
.selectionDirection | Read/Write |
.validity | Read only |
.validationMessage | Read only |
.willValidate | Read only |
Input methods
Method | Notes |
---|---|
.focus() | Focus the input |
.blur() | Remove focus from the input |
.select() | Selects all text within the input |
.setSelectionRange() | |
.setRangeText() | |
.setCustomValidity() | |
.checkValidity() | |
.reportValidity() |
Refer to https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement for more information on these methods and properties. Support will vary based on input type.
Using HTML5 <input>
as an alternative
If you just need a simple input with basic WUI styling, you can simply use the following:
<template>
<div>
<input v-model="value" type="text" class="form-control" />
<br />
<p>Value: "{ value }"</p>
</div>
</template>
<script>
export default {
data() {
return {
value: "",
};
},
};
</script>
<!-- native-input.vue -->
Component reference
<wui-form-input>
Properties
Property | Type | Default | Description |
---|---|---|---|
aria-invalid | Boolean or String | false | Sets the 'aria-invalid' attribute with the specified value |
autocomplete | String | Sets the 'autocomplete' attribute value on the form control | |
autofocus | Boolean | false | When set to true , attempts to auto-focus the control when it is mounted, or re-activated when in a keep-alive. Does not set the autofocus attribute on the control |
debounce | Number or String | 0 | When set to a number of milliseconds greater than zero, will debounce the user input. Has no effect if prop 'lazy' is set |
disabled | Boolean | false | When set to true , disables the component's functionality and places it in a disabled state |
form | String | ID of the form that the form control belongs to. Sets the form attribute on the control | |
formatter | Function | Reference to a function for formatting the input | |
id | String | Used to set the id attribute on the rendered content, and used as the base to generate any additional element IDs as needed | |
lazy | Boolean | false | When set, updates the v-model on 'change'/'blur' events instead of 'input'. Emulates the Vue '.lazy' v-model modifier |
lazy-formatter | Boolean | false | When set, the input is formatted on blur instead of each keystroke (if there is a formatter specified) |
list | String | The ID of the associated datalist element or component | |
max | Number or String | Value to set in the 'max' attribute on the input. Used by number-like inputs | |
min | Number or String | Value to set in the 'min' attribute on the input. Used by number-like inputs | |
name | String | Sets the value of the name attribute on the form control | |
no-wheel | Boolean | false | For number-like inputs, disables the mouse wheel from incrementing or decrementing the value |
number | Boolean | false | When set attempts to convert the input value to a native number. Emulates the Vue '.number' v-model modifier |
placeholder | String | Sets the placeholder attribute value on the form control | |
plaintext | Boolean | false | Set the form control as readonly and renders the control to look like plain text (no borders) |
readonly | Boolean | false | Sets the readonly attribute on the form control |
required | Boolean | false | Adds the required attribute to the form control |
size | String | Set the size of the component's appearance. 'sm', 'md' (default), or 'lg' | |
state | Boolean | null | Controls the validation state appearance of the component. true for valid, false for invalid, or null for no validation state |
step | Number or String | Value to set in the 'step' attribute on the input. Used by number-like inputs | |
trim | Boolean | false | When set, trims any leading and trailing white space from the input value. Emulates the Vue '.trim' v-model modifier |
type | String | 'text' | The type of input to render. See the docs for supported types |
value | Number or String | '' | The current value of the input. Result will always be a string, except when the number prop is used |
v-model
Property | Event |
---|---|
value | update |
Events
Event | Arguments | Description |
---|---|---|
blur | event - Native blur event (before any formatting) | Emitted after the input looses focus |
change | value - Current value of input | Change event triggered by user interaction. Emitted after any formatting (not including 'trim' or 'number' props) and after the v-model is updated |
input | value - Current value of input | Input event triggered by user interaction. Emitted after any formatting (not including 'trim' or 'number' props) and after the v-model is updated |
update | value - Value of input, after any formatting. Not emitted if the value does not change | Emitted to update the v-model |