_statefulSelectors.js.flow 969 B

123456789101112131415161718192021222324252627282930313233
  1. // @flow
  2. import PolishedError from './_errors'
  3. import type { InteractionState } from '../types/interactionState'
  4. function generateSelectors(template: Function, state: InteractionState): string {
  5. const stateSuffix = state ? `:${state}` : ''
  6. return template(stateSuffix)
  7. }
  8. /**
  9. * Function helper that adds an array of states to a template of selectors. Used in textInputs and buttons.
  10. * @private
  11. */
  12. function statefulSelectors(
  13. states: Array<InteractionState>,
  14. template: Function,
  15. stateMap?: Array<InteractionState>,
  16. ): string {
  17. if (!template) throw new PolishedError(67)
  18. if (states.length === 0) return generateSelectors(template, null)
  19. let selectors = []
  20. for (let i = 0; i < states.length; i += 1) {
  21. if (stateMap && stateMap.indexOf(states[i]) < 0) {
  22. throw new PolishedError(68)
  23. }
  24. selectors.push(generateSelectors(template, states[i]))
  25. }
  26. selectors = selectors.join(',')
  27. return selectors
  28. }
  29. export default statefulSelectors