# All files / internalHelpers _statefulSelectors.js

100% Statements 13/13
100% Branches 10/10
100% Functions 2/2
100% Lines 10/10

    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

    19x
    19x

    17x
    16x
    14x
    14x
    21x
    17x

    10x
    10x

     // @flow
    function generateSelectors(template: Function, state: string|null) {
      const stateSuffix = state ? `:${state}` : ''
      return template(stateSuffix)
    }

    type State =
      | typeof(undefined)
      | null
      | string;

    /**
     * Function helper that adds an array of states to a template of selectors. Used in textInputs and buttons.
     * @private
     */
    function statefulSelectors(states: Array<State>, template: Function, stateMap: ?Array<State>) {
      if (!template) throw new Error('You must provide a template to this method.')
      if (states.length === 0) return generateSelectors(template, null)
      let selectors = []
      for (let i = 0; i < states.length; i += 1) {
        if (stateMap && !stateMap.includes(states[i])) throw new Error('You passed an unsupported selector state to this method.')
        selectors.push(generateSelectors(template, states[i]))
      }
      selectors = selectors.join(',')
      return selectors
    }

    export default statefulSelectors