All files / sp-form/fields/sp-text-field ViewModel.js

11.11% Statements 1/9
0% Branches 0/6
33.33% Functions 1/3
11.11% Lines 1/9

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54                                                                                    1x                      
import Field from 'spectre-canjs/util/field/Field';
 
/**
 * A `<sp-text-field />` component's ViewModel
 * @class ViewModel
 * @extends Field
 * @memberof sp-text-field
 */
export default Field.extend('TextField', {
    /** @lends sp-text-field.ViewModel.prototype */
    /**
     * The type of input to create.
     * The default is 'text'
     * @example
     * textType="number"
     * @memberof sp-text-field.ViewModel.prototype
     * @type {String}
     */
    textType: {default: 'text', type: 'string'},
    /**
     * Show or hide the clear input addon button 
     * @example
     * showClear="true"
     * @memberof sp-text-field.ViewModel.prototype
     * @type {Boolean}
     */
    showClear: {default: false, type: 'boolean'},
    /**
     * Checks for the enter keypress and triggers a change event on the input
     * The enter key press triggers a submit event on the form, but before the
     * submit event, we need to trigger a change on the field value
     * @param  {domElement} element The form input element
     * @param  {KeyDownEvent} event The form submit event
     * @return {Boolean}
     */
    beforeSubmit (element, event) {
        if (event.keyCode === 13) {
            element.dispatchEvent(new Event('change'));
        }
        return true;
    },
    clearValue () {
        this.value = '';
    },
    connectedCallback (element) {
        if (this.onInsert) {
            const el = element.querySelector('input,textarea');
            if (el) {
                return this.onInsert(el);
            }
        }
        return null;
    }
});