All files / src/gadgets/list item-dialog.js

100% Statements 39/39
100% Branches 32/32
100% Functions 14/14
100% Lines 38/38

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 171 172 173 174 175 176 177 178 179 180                                                                    22x 22x       22x       25x 22x 22x       19x           28x 21x 21x 21x   7x         8x 2x             55x 55x 8x 47x   7x   40x           1x       2x 1x   2x       1x       1x 1x       52x 7x     45x                                                 52x 52x   52x 52x             52x 69x         52x 52x                                                    
/* 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 React from 'react'
import styles from './dialog.css'
import { cloneDeep, isEqual, noop, throttle } from 'lodash'
import { Dialog, Icon, RaisedButton } from '@kuali/kuali-ui'
 
export default class ItemDialog extends React.Component {
  static displayName = 'ItemDialog'
 
  static propTypes = {
    readOnly: PropTypes.bool,
    headers: PropTypes.array,
    visible: PropTypes.bool,
    item: PropTypes.object,
    onClose: PropTypes.func,
    onSave: PropTypes.func,
    renderRow: PropTypes.func.isRequired,
    itemIndex: PropTypes.number
  }
 
  static defaultProps = {
    headers: [],
    onChange: () => {}
  }
 
  constructor (props) {
    super(props)
    this.state = {
      item: props.item || {},
      bottomOfDialogSeen: false
    }
    this.initialItem = cloneDeep(props.item)
  }
 
  scrollListener = throttle(() => {
    if (!this.state.bottomOfDialogSeen) {
      const { scrollTop, clientHeight, scrollHeight } = this.dialogContainer
      if (
        Math.ceil(scrollTop) + Math.ceil(clientHeight) >=
        Math.floor(scrollHeight) - 3
      ) {
        this.setState({ bottomOfDialogSeen: true })
      }
    }
  }, 100)
 
  handleScrollListener = morePrompt => {
    if (morePrompt) {
      this.dialogContainer = morePrompt.parentNode
      this.dialogContainer.addEventListener('scroll', this.scrollListener)
      this.scrollListener()
    } else {
      this.dialogContainer.removeEventListener('scroll', this.scrollListener)
    }
  }
 
  componentWillReceiveProps (nextProps) {
    if (!isEqual(this.props.item, nextProps.item)) {
      this.setState({
        item: nextProps.item || {}
      })
    }
  }
 
  get title () {
    const { readOnly, item } = this.props
    if (readOnly) {
      return 'Details'
    } else if (!item) {
      // Our parent did not provide us with an item, its new
      return 'Add'
    } else {
      return 'Edit'
    }
  }
 
  // handlers
  handleSave = _ => {
    this.props.onSave(this.state.item)
  }
 
  handleClose = () => {
    if (this.initialItem) {
      this.props.onChange(this.initialItem)
    }
    this.props.onClose()
  }
 
  handleChange = (field, value) => {
    const item = {
      ...this.state.item,
      [field]: value
    }
    this.setState({ item })
    this.props.onChange(item)
  }
 
  dialogButtons = () => {
    if (this.props.readOnly) {
      return null
    }
 
    return [
      <RaisedButton label='Cancel' onClick={this.handleClose} variant='plain'>
        <Icon variant='warning' name='close' />
      </RaisedButton>,
      <RaisedButton
        className={this.state.bottomOfDialogSeen ? '' : styles.disabledButton}
        label='Done'
        onClick={this.state.bottomOfDialogSeen ? this.handleSave : noop}
        tooltipLabel={
          this.state.bottomOfDialogSeen
            ? ''
            : 'Please view the remainder of this form by scrolling downward'
        }
        tooltipPosition='left'
        variant='plain'
      >
        <Icon
          variant={this.state.bottomOfDialogSeen ? 'success' : 'default'}
          name='done'
        />
      </RaisedButton>
    ]
  }
 
  dialogContent = () => {
    const { readOnly, renderRow, headers, itemIndex } = this.props
    const { item } = this.state
 
    const templates = headers.map(header => header.template)
    const newRow = renderRow(
      readOnly ? 'view' : 'edit',
      templates,
      item,
      this.handleChange,
      itemIndex
    )
    return React.Children.map(newRow, child => {
      return React.cloneElement(child, { className: styles['dialog-gadget'] })
    })
  }
 
  render () {
    const { visible } = this.props
    return (
      <Dialog
        actions={this.dialogButtons()}
        contentClassName={styles.dialog}
        id='addEditItemDialog'
        lastChild
        modal
        onHide={this.handleClose}
        renderNode={document.body}
        title={this.title}
        visible={visible}
      >
        <div
          className={styles.morePrompt}
          ref={this.handleScrollListener}
          style={{ display: this.state.bottomOfDialogSeen ? 'none' : 'block' }}
        >
          <Icon name='chevron_left' className={styles.moreIcon} />
          <div>MORE</div>
        </div>
 
        {this.dialogContent()}
      </Dialog>
    )
  }
}