All files / addon/components nucleus-tabs.js

82.35% Statements 14/17
75% Branches 6/8
100% Functions 4/4
82.35% Lines 14/17

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 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                                                                                          15x                                                   7x                   15x                   15x                                         15x                           33x     33x                         33x                             3x 3x 2x   3x 3x 2x            
import Component from '@ember/component';
import { classNames, classNameBindings, layout as templateLayout } from '@ember-decorators/component';
import layout from '../templates/components/nucleus-tabs';
import { set, get, getProperties, computed, action } from '@ember/object';
import defaultProp from '@freshworks/core/utils/default-decorator';
import { A } from '@ember/array';
import { oneWay }from '@ember/object/computed';
 
/**
  __Usage:__
 
  [Refer component page](/docs/components/nucleus-tabs)
 
  @class Nucleus Tabs
  @namespace Components
  @extends Ember.Component
  @public
*/
@templateLayout(layout)
@classNames('nucleus-tabs')
@classNameBindings('variantClass', 'customClasses')
class NucleusTabs extends Component {
  /**
  * description
  *
  * @field description
  * @description to add aria label
  * @type string|null
  * @default null
  * @readonly
  * @public
  */
  @defaultProp
  description = null;
 
  /**
  * customClasses
  *
  * @field customClasses
  * @description to add custom class to the tabs component
  * @type string
  * @readonly
  * @public
  */
  @defaultProp 
  customClasses = '';
 
  /**
  * select
  *
  * @field select
  * @description default open tab 
  * @type string|null
  * @default null
  * @readonly
  * @public
  */
  @defaultProp
  select = null;
 
  /**
  * variant
  *
  * @field variant
  * @description tab styles, line/background
  * @type string
  * @default 'line'
  * @readonly
  * @public
  */
  @defaultProp
  variant = 'line';
 
  /**
  * tabPanels
  *
  * @field tabPanels
  * @description Collection of all tab panels
  * @type array
  * @public
  */
  tabPanels = A([]);
 
  /**
  * tabListItems
  *
  * @field tabListItems
  * @description Collection of all tab list items
  * @type array
  * @public
  */
  tabListItems = A([]);
 
  /**
  * selected
  *
  * @field selected
  * @description takes intial value from select
  * @type string|null
  * @public
  */
  @oneWay('select')
  selected;
 
  /**
  * variantClass
  *
  * @field variantClass
  * @type string
  * @public
  */
  @computed('variant', function() {
    return 'nucleus-tabs--' + get(this, 'variant');
  })
  variantClass;
 
  /**
  * registerPanel
  *
  * @method registerPanel
  * @param {Object} tab
  * @public
  *
  */
  @action
  registerPanel(tab) {
    Iif(!get(this, 'selected') && (tab.disabled === 'false')) {
      set(this, 'selected', tab.name); // set selected if not initially set
    }
    get(this, 'tabPanels').pushObject(tab);
  }
 
  /**
  * registerTabListItem 
  *
  * @method registerTabListItem
  * @param {Object} tab
  * @public
  *
  */
  @action
  registerTabListItem(tabList) {
    get(this, 'tabListItems').pushObject(tabList);
  }
 
  /**
  * activateTab
  *
  * @method activateTab
  * @description Handler that will be called when a tab is clicked
  * @param {string} name
  * @param {any} event
  * @public
  *
  */
  @action
  async activateTab(changedTo, event) {
    const { beforeChange, onChange, currentTab } = getProperties(this, 'beforeChange', 'onChange', 'currentTab');
    if(beforeChange) {
      await beforeChange.call(this, changedTo, currentTab, event);
    }
    set(this, 'selected', changedTo);
    if(onChange) {
      await onChange.call(this, changedTo, currentTab, event);
    }
  }
}
 
export default NucleusTabs;