1 const { defineComponent, h, ref } = require( 'vue' );
2 const { CdxTextInput } = require( '@wikimedia/codex' );
5 * A wrapper for a TextInput that uses HTMLInputElement's `checkValidity()` to validate
6 * the input against any constraints given via HTML attributes (i.e. `required`, or `min=1`).
8 * @todo replace with Codex useNativeValidation composable once available (T373872#10175988)
10 module.exports = exports = defineComponent( {
11 name: 'ValidatingTextInput',
15 emits: [ 'update:status' ],
16 setup( props, { emit } ) {
17 const status = ref( 'default' );
18 const messages = ref( {} );
21 * Clear error status when input is valid and emit the `update:messages` event.
25 const onInput = ( e ) => {
26 if ( e.target.checkValidity() ) {
27 status.value = 'default';
29 emit( 'update:status', status.value, messages.value );
34 * Set error status when input is invalid and emit the `update:messages` event.
38 const onInvalid = ( e ) => {
39 status.value = 'error';
41 error: e.target.validationMessage
43 emit( 'update:status', status.value, messages.value );
54 return h( CdxTextInput, Object.assign( {}, this.$props, {
56 onInput: this.onInput,
57 onInvalid: this.onInvalid