Skip to content
This repository was archived by the owner on Jun 4, 2024. It is now read-only.

Add fallback copy handler. #725

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ This project adheres to [Semantic Versioning](http://semver.org/).
### Changed
- [#713](https://github.com/plotly/dash-table/pull/713) Update from React 16.8.6 to 16.13.0

### Fixed
- [#725](https://github.com/plotly/dash-table/pull/725) Fix bug in which copy events were not triggered except upon selection of a different cell.

## [4.6.1] - 2020-02-27
### Added
- [#711](https://github.com/plotly/dash-table/pull/711) Added R examples to package help
Expand Down
13 changes: 12 additions & 1 deletion src/dash-table/components/ControlledTable/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,12 @@ export default class ControlledTable extends PureComponent<ControlledTableProps>
}

componentDidMount() {
// Fallback method for paste handling in Chrome
// Fallback method for copy/paste handling in Chrome
// when no input element has focused inside the table
window.addEventListener('resize', this.forceHandleResize);
document.addEventListener('mousedown', this.handleClick);
document.addEventListener('paste', this.handlePaste);
document.addEventListener('copy', this.handleCopy);

const {
active_cell,
Expand Down Expand Up @@ -222,6 +223,16 @@ export default class ControlledTable extends PureComponent<ControlledTableProps>
}
}

handleCopy = (event: any) => {
// no need to check for target as this will only be called if
// a child fails to handle the copy event (e.g table, table input)
// make sure the active element is in the scope of the component
const $el = this.$el;
if ($el && $el.contains(document.activeElement)) {
this.onCopy(event);
}
}

forceHandleResize = () => this.handleResize(true);

handleResize = (force: boolean = false) => {
Expand Down
22 changes: 22 additions & 0 deletions tests/selenium/test_basic_copy_paste.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,3 +235,25 @@ def test_tbcp008_copy_paste_between_tables_with_hidden_columns(test):
target.cell(row + 10, col).get_text()
== target.cell(row, col).get_text()
)


def test_tbcp009_copy_paste_to_external_element(test):
app = get_app()

new_layout = html.Div([html.Textarea(id="clipboard-content"), app.layout])

app.layout = new_layout

test.start_server(app)

target = test.table("table")
with test.hold(Keys.SHIFT):
target.cell(0, 0).click()
target.cell(2, 1).click()
test.copy()

target.cell(2, 2).click()
test.paste()
test.driver.find_element_by_id("clipboard-content").click()

test.paste()