all files / addon/components/ polaris-checkbox.js

100% Statements 14/14
100% Branches 12/12
87.5% Functions 7/8
100% Lines 14/14
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 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191                                                                                                                                                                                                                                                                                                                  22×       22×       22×   22×     22×       18×       23× 23×   23×     23×     23×        
import Component from '@ember/component';
import { computed } from '@ember/object';
import { guidFor } from '@ember/object/internals';
import { typeOf } from '@ember/utils';
import { equal } from '@ember/object/computed';
import layout from '../templates/components/polaris-checkbox';
 
/**
 * Polaris checkbox component.
 * See https://polaris.shopify.com/components/forms/checkbox
 */
export default Component.extend({
  // Tagless component, renders a `polaris-choice` component internally.
  tagName: '',
 
  layout,
 
  /**
   * Label for the checkbox
   *
   * @property label
   * @type {String}
   * @default null
   * @public
   */
  label: null,
 
  /**
   * Component to render for the checkbox's label
   *
   * @property labelComponent
   * @type {String | Component}
   * @default null
   * @public
   */
  labelComponent: null,
 
  /**
   * Visually hide the label
   *
   * @property labelHidden
   * @type {Boolean}
   * @default false
   * @public
   */
  labelHidden: false,
 
  /**
   * Checkbox is selected. `indeterminate` shows a horizontal line in the checkbox
   *
   * @property checked
   * @type {Boolean/String}
   * @default false
   * @public
   */
  checked: false,
 
  /**
   * Additional text to aide in use
   *
   * @property helpText
   * @type {String}
   * @default null
   * @public
   */
  helpText: null,
 
  /**
   * ID for form input
   *
   * @property inputId
   * @type {String}
   * @default null
   * @public
   */
  inputId: null,
 
  /**
   * Name for form input
   *
   * @property name
   * @type {String}
   * @default null
   * @public
   */
  name: null,
 
  /**
   * Value for form input
   *
   * @property value
   * @type {String}
   * @default null
   * @public
   */
  value: null,
 
  /**
   * Display an error state
   *
   * @property error
   * @type {String}
   * @default null
   * @public
   */
  error: null,
 
  /**
   * Disable the checkbox
   *
   * @property disabled
   * @type {Boolean}
   * @default false
   * @public
   */
  disabled: false,
 
  /**
   * Callback when checkbox is toggled
   *
   * @property onChange
   * @type {function}
   * @default noop
   * @public
   */
  onChange() {},
 
  /**
   * Callback when checkbox is focussed
   *
   * @property onFocus
   * @type {function}
   * @default noop
   * @public
   */
  onFocus() {},
 
  /**
   * Callback when focus is removed
   *
   * @property onBlur
   * @type {function}
   * @default noop
   * @public
   */
  onBlur() {},
 
  /*
   * Internal properties.
   */
  isIndeterminate: equal('checked', 'indeterminate').readOnly(),
 
  isChecked: computed('isIndeterminate', 'checked', function() {
    return !this.get('isIndeterminate') && Boolean(this.get('checked'));
  }).readOnly(),
 
  checkedState: computed('isIndeterminate', 'isChecked', function() {
    return this.get('isIndeterminate') ? 'mixed' : `${ this.get('isChecked') }`;
  }).readOnly(),
 
  checkboxClasses: computed('isIndeterminate', function() {
    let classNames = ['Polaris-Checkbox__Input'];
 
    if (this.get('isIndeterminate')) {
      classNames.push('Polaris-Checkbox__Input--indeterminate');
    }
 
    return classNames.join(' ');
  }).readOnly(),
 
  _id: computed('inputId', function() {
    return this.get('inputId') || `polaris-checkbox-${ guidFor(this) }`;
  }).readOnly(),
 
  describedBy: computed('error', 'helpText', '_id', function() {
    let describedBy = [];
    const { error, helpText } = this.getProperties('error', 'helpText');
 
    if (typeOf(error) === 'string') {
      describedBy.push(`${ this.get('_id') }Error`);
    }
 
    if (helpText) {
      describedBy.push(`${ this.get('_id') }HelpText`);
    }
 
    return describedBy.join(' ');
  }).readOnly(),
});