Page MenuHomeWickedGov Phorge

ValidatingTextInput.js
No OneTemporary

Size
1 KB
Referenced Files
None
Subscribers
None

ValidatingTextInput.js

const { defineComponent, h, ref } = require( 'vue' );
const { CdxTextInput } = require( '@wikimedia/codex' );
/**
* A wrapper for a TextInput that uses HTMLInputElement's `checkValidity()` to validate
* the input against any constraints given via HTML attributes (i.e. `required`, or `min=1`).
*
* @todo replace with Codex useNativeValidation composable once available (T373872#10175988)
*/
module.exports = exports = defineComponent( {
name: 'ValidatingTextInput',
components: {
CdxTextInput
},
emits: [ 'update:status' ],
setup( props, { emit } ) {
const status = ref( 'default' );
const messages = ref( {} );
/**
* Clear error status when input is valid and emit the `update:messages` event.
*
* @param {Event} e
*/
const onInput = ( e ) => {
if ( e.target.checkValidity() ) {
status.value = 'default';
messages.value = {};
emit( 'update:status', status.value, messages.value );
}
};
/**
* Set error status when input is invalid and emit the `update:messages` event.
*
* @param {Event} e
*/
const onInvalid = ( e ) => {
status.value = 'error';
messages.value = {
error: e.target.validationMessage
};
emit( 'update:status', status.value, messages.value );
};
return {
status,
messages,
onInput,
onInvalid
};
},
render() {
return h( CdxTextInput, Object.assign( {}, this.$props, {
status: this.status,
onInput: this.onInput,
onInvalid: this.onInvalid
} ) );
}
} );

File Metadata

Mime Type
text/plain
Expires
Sat, May 16, 20:38 (1 d, 3 h)
Storage Engine
local-disk
Storage Format
Raw Data
Storage Handle
c2/eb/12cc0b7981936f349ee905981925
Default Alt Text
ValidatingTextInput.js (1 KB)

Event Timeline