|
| 1 | +/* eslint react/prop-types: 0 */ |
| 2 | +/* eslint no-return-assign: 0 */ |
| 3 | +/* eslint class-methods-use-this: 0 */ |
| 4 | +/* eslint jsx-a11y/no-noninteractive-element-interactions: 0 */ |
| 5 | +import React, { Component } from 'react'; |
| 6 | +import cs from 'classnames'; |
| 7 | +import PropTypes from 'prop-types'; |
| 8 | + |
| 9 | +import TextEditor from './text-editor'; |
| 10 | +import EditorIndicator from './editor-indicator'; |
| 11 | +import { TIME_TO_CLOSE_MESSAGE } from './const'; |
| 12 | + |
| 13 | +export default _ => |
| 14 | + class EditingCell extends Component { |
| 15 | + static propTypes = { |
| 16 | + row: PropTypes.object.isRequired, |
| 17 | + column: PropTypes.object.isRequired, |
| 18 | + onUpdate: PropTypes.func.isRequired, |
| 19 | + onEscape: PropTypes.func.isRequired, |
| 20 | + timeToCloseMessage: PropTypes.number, |
| 21 | + className: PropTypes.string, |
| 22 | + style: PropTypes.object |
| 23 | + } |
| 24 | + |
| 25 | + static defaultProps = { |
| 26 | + timeToCloseMessage: TIME_TO_CLOSE_MESSAGE, |
| 27 | + className: null, |
| 28 | + style: {} |
| 29 | + } |
| 30 | + |
| 31 | + constructor(props) { |
| 32 | + super(props); |
| 33 | + this.indicatorTimer = null; |
| 34 | + this.clearTimer = this.clearTimer.bind(this); |
| 35 | + this.handleBlur = this.handleBlur.bind(this); |
| 36 | + this.handleClick = this.handleClick.bind(this); |
| 37 | + this.handleKeyDown = this.handleKeyDown.bind(this); |
| 38 | + this.beforeComplete = this.beforeComplete.bind(this); |
| 39 | + this.state = { |
| 40 | + invalidMessage: null |
| 41 | + }; |
| 42 | + } |
| 43 | + |
| 44 | + componentWillReceiveProps({ message }) { |
| 45 | + if (_.isDefined(message)) { |
| 46 | + this.createTimer(); |
| 47 | + this.setState(() => ({ |
| 48 | + invalidMessage: message |
| 49 | + })); |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + componentWillUnmount() { |
| 54 | + this.clearTimer(); |
| 55 | + } |
| 56 | + |
| 57 | + clearTimer() { |
| 58 | + if (this.indicatorTimer) { |
| 59 | + clearTimeout(this.indicatorTimer); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + createTimer() { |
| 64 | + this.clearTimer(); |
| 65 | + const { timeToCloseMessage, onErrorMessageDisappear } = this.props; |
| 66 | + this.indicatorTimer = _.sleep(() => { |
| 67 | + this.setState(() => ({ |
| 68 | + invalidMessage: null |
| 69 | + })); |
| 70 | + if (_.isFunction(onErrorMessageDisappear)) onErrorMessageDisappear(); |
| 71 | + }, timeToCloseMessage); |
| 72 | + } |
| 73 | + |
| 74 | + beforeComplete(row, column, newValue) { |
| 75 | + const { onUpdate } = this.props; |
| 76 | + if (_.isFunction(column.validator)) { |
| 77 | + const validateForm = column.validator(newValue, row, column); |
| 78 | + if (_.isObject(validateForm) && !validateForm.valid) { |
| 79 | + this.setState(() => ({ |
| 80 | + invalidMessage: validateForm.message |
| 81 | + })); |
| 82 | + this.createTimer(); |
| 83 | + return; |
| 84 | + } |
| 85 | + } |
| 86 | + onUpdate(row, column, newValue); |
| 87 | + } |
| 88 | + |
| 89 | + handleBlur() { |
| 90 | + const { onEscape, blurToSave, row, column } = this.props; |
| 91 | + if (blurToSave) { |
| 92 | + const value = this.editor.text.value; |
| 93 | + if (!_.isDefined(value)) { |
| 94 | + // TODO: for other custom or embed editor |
| 95 | + } |
| 96 | + this.beforeComplete(row, column, value); |
| 97 | + } else { |
| 98 | + onEscape(); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + handleKeyDown(e) { |
| 103 | + const { onEscape, row, column } = this.props; |
| 104 | + if (e.keyCode === 27) { // ESC |
| 105 | + onEscape(); |
| 106 | + } else if (e.keyCode === 13) { // ENTER |
| 107 | + const value = e.currentTarget.value; |
| 108 | + if (!_.isDefined(value)) { |
| 109 | + // TODO: for other custom or embed editor |
| 110 | + } |
| 111 | + this.beforeComplete(row, column, value); |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + handleClick(e) { |
| 116 | + if (e.target.tagName !== 'TD') { |
| 117 | + // To avoid the row selection event be triggered, |
| 118 | + // When user define selectRow.clickToSelect and selectRow.clickToEdit |
| 119 | + // We shouldn't trigger selection event even if user click on the cell editor(input) |
| 120 | + e.stopPropagation(); |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + render() { |
| 125 | + const { invalidMessage } = this.state; |
| 126 | + const { row, column, className, style } = this.props; |
| 127 | + const { dataField } = column; |
| 128 | + |
| 129 | + const value = _.get(row, dataField); |
| 130 | + const editorAttrs = { |
| 131 | + onKeyDown: this.handleKeyDown, |
| 132 | + onBlur: this.handleBlur |
| 133 | + }; |
| 134 | + |
| 135 | + const hasError = _.isDefined(invalidMessage); |
| 136 | + const editorClass = hasError ? cs('animated', 'shake') : null; |
| 137 | + return ( |
| 138 | + <td |
| 139 | + className={ cs('react-bootstrap-table-editing-cell', className) } |
| 140 | + style={ style } |
| 141 | + onClick={ this.handleClick } |
| 142 | + > |
| 143 | + <TextEditor |
| 144 | + ref={ node => this.editor = node } |
| 145 | + defaultValue={ value } |
| 146 | + className={ editorClass } |
| 147 | + { ...editorAttrs } |
| 148 | + /> |
| 149 | + { hasError ? <EditorIndicator invalidMessage={ invalidMessage } /> : null } |
| 150 | + </td> |
| 151 | + ); |
| 152 | + } |
| 153 | + }; |
0 commit comments