All files / lib/core/renderer controlValue.js

100% Statements 65/65
100% Branches 15/15
100% Functions 1/1
100% Lines 65/65

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 55 56 57 58 59 60 61 62 63 64 65 66778x 778x 778x 778x 778x 778x 778x 778x 778x 778x 778x 778x 778x 778x 778x 778x 778x 778x 778x 778x 778x 778x 778x 778x 778x 778x 778x 778x 778x 778x 778x 778x 778x 778x 778x 778x 778x 778x 778x 778x 778x 778x 778x 778x 778x 778x 778x 46x 16x 16x 48x 48x 8x 8x 48x 16x 16x 46x 22x 22x 46x 2x 2x 6x 46x  
/**
 * @file controlValue.js
 * @description Mirrors a bound `value` attribute onto a form control's property.
 *
 * For a form control the `value` attribute is the *default* value: once the
 * user has typed, the property and the attribute are independent, and setting
 * the attribute alone leaves what the user sees unchanged. Two-way binding
 * depends on this -- a state change made in code has to reach the control the
 * user has already touched. Both render paths call this after writing the
 * attribute, so the rule for turning the attribute into a property is stated
 * once.
 */
 
/**
 * Elements whose `value` attribute stops driving their value once a user has
 * typed into them.
 * @type {Set<string>}
 */
export const VALUE_PROPERTY_TAGS = new Set(['INPUT', 'TEXTAREA', 'SELECT']);
 
/**
 * Input types whose binding writes a number rather than the typed string.
 * @type {Set<string>}
 */
const NUMERIC_INPUT_TYPES = new Set(['number', 'range']);
 
/**
 * Writes a rendered `value` attribute onto the control's `value` property.
 *
 * A `<select multiple>` is the exception. Its `value` property addresses one
 * option, so assigning the rendered array to it selected nothing -- an array
 * bound through `data-ax-bind` renders as `String(array)`, a comma-separated
 * list, and no option has that value. The list is split and each option's
 * `selected` follows membership, the mirror of the array-bound checkbox group.
 *
 * A number or range input is left alone while it already reads as the number
 * the attribute holds. Its binding writes a number, which renders back as that
 * number's canonical string: a typed "1.0" becomes 1 in state and "1" in the
 * attribute. Writing "1" over the control would discard the "0" the user is
 * about to follow with a digit, and "1e3" would turn into "1000" under their
 * cursor. A single select, a textarea and every other input assign the string
 * as before.
 * @param {Element} element - An `<input>`, `<textarea>` or `<select>`.
 * @param {string} value - The attribute value just rendered.
 * @returns {void}
 */
export function syncControlValue(element, value) {
  if (element.nodeName === 'SELECT' && element.multiple) {
    const selected = value === '' ? [] : value.split(',');
    for (const option of Array.from(element.options)) {
      const shouldSelect = selected.includes(option.value);
      if (option.selected !== shouldSelect) {
        option.selected = shouldSelect;
      }
    }
    return;
  }
  if (element.value === value) {
    return;
  }
  if (value !== '' && NUMERIC_INPUT_TYPES.has(element.type) && element.valueAsNumber === Number(value)) {
    return;
  }
  element.value = value;
}