Provide onClick handleOriginal function

Fixes #406
This commit is contained in:
Tanner Linsley 2017-08-02 13:15:16 -06:00
parent bf88dda5dc
commit 9846bf1905
3 changed files with 32 additions and 15 deletions

View File

@ -593,12 +593,21 @@ This makes it extremely easy to add, say... a row click callback!
<ReactTable
getTdProps={(state, rowInfo, column, instance) => {
return {
onClick: e => {
onClick: (e, handleOriginal => {
console.log('A Td Element was clicked!')
console.log('it produced this event:', e)
console.log('It was in this column:', column)
console.log('It was in this row:', rowInfo)
console.log('It was in this table instance:', instance)
// IMPORTANT! React-Table uses onClick internally to trigger
// events like expanding SubComponents and pivots.
// By default a custom 'onClick' handler will override this functionality.
// If you want to fire the original onClick handler, call the
// 'handleOriginal' function.
if (handleOriginal) {
handleOriginal()
}
}
}
}}

View File

@ -574,7 +574,7 @@ export default class ReactTable extends Methods(Lifecycle(Component)) {
const value = cellInfo.value
let interactionProps
let useOnExpanderClick
let isBranch
let isPreview
@ -626,17 +626,10 @@ export default class ReactTable extends Methods(Lifecycle(Component)) {
if (cellInfo.pivoted || cellInfo.expander) {
// Make it expandable by defualt
cellInfo.expandable = true
interactionProps = {
onClick: onExpanderClick,
}
useOnExpanderClick = true
// If pivoted, has no subRows, and does not have a subComponent, do not make expandable
if (cellInfo.pivoted) {
if (!cellInfo.subRows) {
if (!SubComponent) {
cellInfo.expandable = false
interactionProps = {}
}
}
if (cellInfo.pivoted && !cellInfo.subRows && !SubComponent) {
cellInfo.expandable = false
}
}
@ -693,6 +686,21 @@ export default class ReactTable extends Methods(Lifecycle(Component)) {
}
}
// If there are multiple onClick events, make sure they don't override eachother. This should maybe be expanded to handle all function attributes
const interactionProps = {}
if (tdProps.rest.onClick) {
interactionProps.onClick = e => {
tdProps.rest.onClick(e, () => onExpanderClick(e))
}
}
if (columnProps.rest.onClick) {
interactionProps.onClick = e => {
columnProps.rest.onClick(e, () => onExpanderClick(e))
}
}
// Return the cell
return (
<TdComponent
@ -709,9 +717,9 @@ export default class ReactTable extends Methods(Lifecycle(Component)) {
width: _.asPx(width),
maxWidth: _.asPx(maxWidth),
}}
{...interactionProps}
{...tdProps.rest}
{...columnProps.rest}
{...interactionProps}
>
{resolvedCell}
</TdComponent>

View File

@ -145,7 +145,7 @@ function groupBy (xs, key) {
function asPx (value) {
value = Number(value)
return (Number.isNaN(value)) ? null : value + 'px'
return Number.isNaN(value) ? null : value + 'px'
}
function isArray (a) {
@ -179,7 +179,7 @@ function splitProps ({ className, style, ...rest }) {
return {
className,
style,
rest,
rest: rest || {},
}
}