Skip to content

Commit 9846bf1

Browse files
committed
Provide onClick handleOriginal function
Fixes TanStack#406
1 parent bf88dda commit 9846bf1

File tree

3 files changed

+32
-15
lines changed

3 files changed

+32
-15
lines changed

README.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -593,12 +593,21 @@ This makes it extremely easy to add, say... a row click callback!
593593
<ReactTable
594594
getTdProps={(state, rowInfo, column, instance) => {
595595
return {
596-
onClick: e => {
596+
onClick: (e, handleOriginal => {
597597
console.log('A Td Element was clicked!')
598598
console.log('it produced this event:', e)
599599
console.log('It was in this column:', column)
600600
console.log('It was in this row:', rowInfo)
601601
console.log('It was in this table instance:', instance)
602+
603+
// IMPORTANT! React-Table uses onClick internally to trigger
604+
// events like expanding SubComponents and pivots.
605+
// By default a custom 'onClick' handler will override this functionality.
606+
// If you want to fire the original onClick handler, call the
607+
// 'handleOriginal' function.
608+
if (handleOriginal) {
609+
handleOriginal()
610+
}
602611
}
603612
}
604613
}}

src/index.js

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -574,7 +574,7 @@ export default class ReactTable extends Methods(Lifecycle(Component)) {
574574

575575
const value = cellInfo.value
576576

577-
let interactionProps
577+
let useOnExpanderClick
578578
let isBranch
579579
let isPreview
580580

@@ -626,17 +626,10 @@ export default class ReactTable extends Methods(Lifecycle(Component)) {
626626
if (cellInfo.pivoted || cellInfo.expander) {
627627
// Make it expandable by defualt
628628
cellInfo.expandable = true
629-
interactionProps = {
630-
onClick: onExpanderClick,
631-
}
629+
useOnExpanderClick = true
632630
// If pivoted, has no subRows, and does not have a subComponent, do not make expandable
633-
if (cellInfo.pivoted) {
634-
if (!cellInfo.subRows) {
635-
if (!SubComponent) {
636-
cellInfo.expandable = false
637-
interactionProps = {}
638-
}
639-
}
631+
if (cellInfo.pivoted && !cellInfo.subRows && !SubComponent) {
632+
cellInfo.expandable = false
640633
}
641634
}
642635

@@ -693,6 +686,21 @@ export default class ReactTable extends Methods(Lifecycle(Component)) {
693686
}
694687
}
695688

689+
// If there are multiple onClick events, make sure they don't override eachother. This should maybe be expanded to handle all function attributes
690+
const interactionProps = {}
691+
692+
if (tdProps.rest.onClick) {
693+
interactionProps.onClick = e => {
694+
tdProps.rest.onClick(e, () => onExpanderClick(e))
695+
}
696+
}
697+
698+
if (columnProps.rest.onClick) {
699+
interactionProps.onClick = e => {
700+
columnProps.rest.onClick(e, () => onExpanderClick(e))
701+
}
702+
}
703+
696704
// Return the cell
697705
return (
698706
<TdComponent
@@ -709,9 +717,9 @@ export default class ReactTable extends Methods(Lifecycle(Component)) {
709717
width: _.asPx(width),
710718
maxWidth: _.asPx(maxWidth),
711719
}}
712-
{...interactionProps}
713720
{...tdProps.rest}
714721
{...columnProps.rest}
722+
{...interactionProps}
715723
>
716724
{resolvedCell}
717725
</TdComponent>

src/utils.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ function groupBy (xs, key) {
145145

146146
function asPx (value) {
147147
value = Number(value)
148-
return (Number.isNaN(value)) ? null : value + 'px'
148+
return Number.isNaN(value) ? null : value + 'px'
149149
}
150150

151151
function isArray (a) {
@@ -179,7 +179,7 @@ function splitProps ({ className, style, ...rest }) {
179179
return {
180180
className,
181181
style,
182-
rest,
182+
rest: rest || {},
183183
}
184184
}
185185

0 commit comments

Comments
 (0)