diff --git a/src/js/table/index.js b/src/js/table/index.js index faf710c56..f658030a3 100644 --- a/src/js/table/index.js +++ b/src/js/table/index.js @@ -25,15 +25,32 @@ class EditDialog extends React.Component { } onKeyUp = (event) => { - // hide box and return old value + // hide box on escape key if (event.which === keycode('escape')) { - this.props.onSubmit(this.props.value) + this.props.onCancel() } } + onClick = (event) => { + // make sure click happened outside of component + const element = this.c + // https://github.com/tj/react-click-outside/blob/master/index.js#L25 + if (!element.contains(event.target)) { + this.props.onCancel() + } + } + + componentDidMount = () => { + document.addEventListener('click', this.onClick) + } + + componentWillUnmount = () => { + document.removeEventListener('click', this.onClick) + } + render () { return ( -
+
{ this.c = c }}>
{ + hide = () => { this.setState({ isEditing: false }) + } + + onSubmit = (value) => { + this.hide() this.props.onSubmit(value) } @@ -128,7 +149,7 @@ export class TableBodyCell extends React.Component { return ( {this.state.isEditing - ? + ? : null } { editable diff --git a/src/js/table/test/test.js b/src/js/table/test/test.js index bc5fab062..2e83689a3 100644 --- a/src/js/table/test/test.js +++ b/src/js/table/test/test.js @@ -1,4 +1,4 @@ -/* global describe, it */ +/* global describe, it, Event */ import assert from 'assert' import React from 'react' @@ -263,16 +263,12 @@ describe('Table', () => { wrapper.find('.Table-edit-container').simulate('submit') }) - it('should show the old value when escape is pressed', (done) => { - const onSubmit = (value) => { - assert.equal(value, 'hello world') - done() - } + it('should cancel when escape is pressed', () => { const table = ( - + hello world @@ -291,5 +287,37 @@ describe('Table', () => { wrapper.find('.Textfield-input').simulate('keyup', { which: 27 }) + // make sure old value is still present + assert.equal(wrapper.find('.Table-body-row-cell-edit-wrapper').text(), 'hello world') + // make sure overlay isn't visible anymore + assert.equal(wrapper.find('.Table-edit').length, 0) + }) + + it('should cancel when mouse click happened outside of edit dialog', () => { + const table = ( +
+ + + + hello world + + + +
+ ) + const wrapper = mount(table) + wrapper.find('.Table-body-row-cell-edit-wrapper').simulate('click') + // enter some new text + wrapper.find('.Textfield-input').simulate('change', { + target: { + value: 'beep bopp' + } + }) + // simulate click outside of component + document.dispatchEvent(new Event('click')) + // make sure old value is still present + assert.equal(wrapper.find('.Table-body-row-cell-edit-wrapper').text(), 'hello world') + // make sure overlay isn't visible anymore + assert.equal(wrapper.find('.Table-edit').length, 0) }) })