Sha256: f2d6ae7f55316cbb474351c61ad89ff39e3f86749a30b1197cb6e6d2497bd301

Contents?: true

Size: 1.99 KB

Versions: 10

Compression:

Stored size: 1.99 KB

Contents

Rev.registerMixin 'Core',

  suitSet: (base, flags)->
    classes = {}
    classes[base] = true
    for key, value of flags
      classes["#{base}--#{key}"] = value
    @cx classes

  # Returns a copy of @props sans specific keys
  # Example:
  #   props = @getPropsWithout "className"
  #   <div {...props}><button className={@props.className}>hi</button></div>
  getPropsWithout: ->
    skipList = {}
    skipList[name] = true for name in arguments
    props = {}
    for name, value of @props
      props[name] = value unless skipList[name]
    props

  # An object that maps the CSS classes of @props.className from their name to `true`.
  _getClassSetObject: ->
    return {} unless @props.className
    classNames = @props.className.split ' '
    object = {}
    object[name] = true for name in classNames
    object

  # Like React.addons.classSet but where the existing classes of @props.className are included.
  # (Unless they are overridden by `classToBoolMap`.)
  # Example:
  #   className = @classAdd
  #     active: @props.active
  #     <div className={className}>hi</div>
  classAdd: (classToBoolMap) ->
    newClassToBoolMap = @_getClassSetObject()
    newClassToBoolMap[name] = value for name, value of classToBoolMap
    @classSet newClassToBoolMap

  # Alias for `React.addons.classSet`
  classSet: (classNameToBool) ->
    classNames = []
    for className, shouldBeIncluded of classNameToBool
      classNames.push className if shouldBeIncluded
    classNames.join ' '

  # Alias for `@classSet`
  cx: -> @classSet.apply this, arguments

  # Returns @props.children always as either `null` or an array.
  # This is because for some reason in React `children` is only an array when
  # there is more than one. Sometimes it's easier to be able to assume it's an
  # array.
  # Example:
  #   <ul>{@getNormalizedChildren().map (i, child) -> <li key={i}>{child}</li>}</ul>
  getNormalizedChildren: ->
    children = @props.children
    if children? and not children.length
      children = [children]
    children

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
revelry_core-0.1.11.6 app/assets/javascripts/revelry/mixins/core.js.coffee
revelry_core-0.1.11.5 app/assets/javascripts/revelry/mixins/core.js.coffee
revelry_core-0.1.11.2 app/assets/javascripts/revelry/mixins/core.js.coffee
revelry_core-0.1.11.1 app/assets/javascripts/revelry/mixins/core.js.coffee
revelry_core-0.1.11.0 app/assets/javascripts/revelry/mixins/core.js.coffee
revelry_core-0.1.10.7 app/assets/javascripts/revelry/mixins/core.js.coffee
revelry_core-0.1.10.6 app/assets/javascripts/revelry/mixins/core.js.coffee
revelry_core-0.1.10.5 app/assets/javascripts/revelry/mixins/core.js.coffee
revelry_core-0.1.10.4 app/assets/javascripts/revelry/mixins/core.js.coffee
revelry_core-0.1.10.3 app/assets/javascripts/revelry/mixins/core.js.coffee