All files / src/gadgets/filtered-dropdown config.js

100% Statements 20/20
100% Branches 0/0
100% Functions 11/11
100% Lines 19/19

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                                                  4x   4x             1x       1x       1x 1x 1x 1x       6x 6x       6x   6x       6x 6x 5x         6x 6x               5x                     12x                            
/* Copyright © 2016 Kuali, Inc. - All Rights Reserved
 * You may use and modify this code under the terms of the Kuali, Inc.
 * Pre-Release License Agreement. You may not distribute it.
 *
 * You should have received a copy of the Kuali, Inc. Pre-Release License
 * Agreement with this file. If not, please write to license@kuali.co.
 */
 
import PropTypes from 'prop-types'
import Scenario from './scenario'
import { SelectField } from '@kuali/kuali-ui'
import styles from './config.css'
import { cloneDeep, get, values } from 'lodash'
import React, { Component } from 'react'
 
export default class Config extends Component {
  static propTypes = {
    gadgets: PropTypes.object,
    onChange: PropTypes.func.isRequired,
    value: PropTypes.shape({
      placeholder: PropTypes.string
    }).isRequired
  }
 
  constructor (props) {
    super(props)
 
    this.state = get(props, 'value.data', {
      referenceGadget: undefined,
      values: {}
    })
  }
 
  handleReferenceGadgetChange = newValue => {
    this.setState({
      referenceGadget: newValue,
      values: {}
    })
    this.props.onChange({ ...this.props.value, data: this.state })
  }
 
  handleListChange = (optionKey, newItems) => {
    const state = cloneDeep(this.state)
    state.values[optionKey] = newItems
    this.setState(state)
    this.props.onChange({ ...this.props.value, data: state })
  }
 
  findGadget = formKey => {
    const allGadgetsArray = values(this.props.gadgets)
    return allGadgetsArray.find(gadget => gadget.formKey === formKey)
  }
 
  gadgetsOptions = gadgetFormKey => {
    const x = get(this.findGadget(gadgetFormKey), 'details.options', [])
 
    return x
  }
 
  otherGadgetsThatHaveOptions = () => {
    const allGadgetsArray = values(this.props.gadgets)
    return allGadgetsArray.filter(
      gadget => get(gadget, 'details.options', []).length > 0
    )
  }
 
  render () {
    const { referenceGadget, values } = this.state
    return (
      <div className={styles.container}>
        <h3>Options:</h3>
        <div>
          When
          <SelectField
            className={styles.dropdown}
            id='referenceGadget'
            menuItems={this.otherGadgetsThatHaveOptions().map(gadget => ({
              label: gadget.label,
              value: gadget.formKey
            }))}
            onChange={this.handleReferenceGadgetChange}
            placeholder='-- Choose a Gadget --'
            position={SelectField.Positions.BELOW}
            value={referenceGadget}
          />
        </div>
        {this.gadgetsOptions(referenceGadget).map(option => (
          <Scenario
            onListChange={this.handleListChange}
            key={option.key}
            option={{
              label: option.lbl,
              value: option.key
            }}
            values={values[option.key]}
          />
        ))}
      </div>
    )
  }
}