Skip to content

Commit f131e63

Browse files
authored
Merge pull request desktop#16114 from desktop/change-list-context-menu-invoking
Invoking Context Menu from ListRow component
2 parents 1200634 + 1c8dcaa commit f131e63

File tree

4 files changed

+38
-13
lines changed

4 files changed

+38
-13
lines changed

app/src/ui/changes/changed-file.tsx

+1-11
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,6 @@ interface IChangedFileProps {
1515
readonly disableSelection: boolean
1616
readonly checkboxTooltip?: string
1717
readonly onIncludeChanged: (path: string, include: boolean) => void
18-
19-
/** Callback called when user right-clicks on an item */
20-
readonly onContextMenu: (
21-
file: WorkingDirectoryFileChange,
22-
event: React.MouseEvent<HTMLDivElement>
23-
) => void
2418
}
2519

2620
/** a changed file in the working directory for a given repository */
@@ -59,7 +53,7 @@ export class ChangedFile extends React.Component<IChangedFileProps, {}> {
5953
statusWidth
6054

6155
return (
62-
<div className="file" onContextMenu={this.onContextMenu}>
56+
<div className="file">
6357
<TooltippedContent
6458
tooltip={checkboxTooltip}
6559
direction={TooltipDirection.EAST}
@@ -92,8 +86,4 @@ export class ChangedFile extends React.Component<IChangedFileProps, {}> {
9286
</div>
9387
)
9488
}
95-
96-
private onContextMenu = (event: React.MouseEvent<HTMLDivElement>) => {
97-
this.props.onContextMenu(this.props.file, event)
98-
}
9989
}

app/src/ui/changes/changes-list.tsx

+5-2
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,6 @@ export class ChangesList extends React.Component<
318318
file={file}
319319
include={isPartiallyCommittableSubmodule && include ? null : include}
320320
key={file.id}
321-
onContextMenu={this.onItemContextMenu}
322321
onIncludeChanged={onIncludeChanged}
323322
availableWidth={availableWidth}
324323
disableSelection={disableSelection}
@@ -678,9 +677,12 @@ export class ChangesList extends React.Component<
678677
}
679678

680679
private onItemContextMenu = (
681-
file: WorkingDirectoryFileChange,
680+
row: number,
682681
event: React.MouseEvent<HTMLDivElement>
683682
) => {
683+
const { workingDirectory } = this.props
684+
const file = workingDirectory.files[row]
685+
684686
if (this.props.isCommitting) {
685687
return
686688
}
@@ -965,6 +967,7 @@ export class ChangesList extends React.Component<
965967
onScroll={this.onScroll}
966968
setScrollTop={this.props.changesListScrollTop}
967969
onRowKeyDown={this.onRowKeyDown}
970+
onRowContextMenu={this.onItemContextMenu}
968971
/>
969972
{this.renderStashedChanges()}
970973
{this.renderCommitMessageForm()}

app/src/ui/lib/list/list-row.tsx

+12
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,13 @@ interface IListRowProps {
5050
e: React.FocusEvent<HTMLDivElement>
5151
) => void
5252

53+
/** Called back for when the context menu is invoked (user right clicks of
54+
* uses keyboard shortcuts) */
55+
readonly onContextMenu?: (
56+
index: number,
57+
e: React.MouseEvent<HTMLDivElement>
58+
) => void
59+
5360
/**
5461
* Whether or not this list row is going to be selectable either through
5562
* keyboard navigation, pointer clicks, or both. This is used to determine
@@ -102,6 +109,10 @@ export class ListRow extends React.Component<IListRowProps, {}> {
102109
this.props.onRowBlur?.(this.props.rowIndex, e)
103110
}
104111

112+
private onContextMenu = (e: React.MouseEvent<HTMLDivElement>) => {
113+
this.props.onContextMenu?.(this.props.rowIndex, e)
114+
}
115+
105116
public render() {
106117
const selected = this.props.selected
107118
const className = classNames(
@@ -139,6 +150,7 @@ export class ListRow extends React.Component<IListRowProps, {}> {
139150
style={style}
140151
onFocus={this.onFocus}
141152
onBlur={this.onBlur}
153+
onContextMenu={this.onContextMenu}
142154
>
143155
{this.props.children}
144156
</div>

app/src/ui/lib/list/list.tsx

+20
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,18 @@ interface IListProps {
189189
*/
190190
readonly onRowMouseDown?: (row: number, event: React.MouseEvent<any>) => void
191191

192+
/**
193+
* A handler called whenever a context menu event is received on the
194+
* row container element.
195+
*
196+
* The context menu is invoked when a user right clicks the row or
197+
* uses keyboard shortcut.
198+
*/
199+
readonly onRowContextMenu?: (
200+
row: number,
201+
event: React.MouseEvent<HTMLDivElement>
202+
) => void
203+
192204
/**
193205
* A handler called whenever the user drops items on the list to be inserted.
194206
*
@@ -631,6 +643,13 @@ export class List extends React.Component<IListProps, IListState> {
631643
}
632644
}
633645

646+
private onRowContextMenu = (
647+
row: number,
648+
e: React.MouseEvent<HTMLDivElement>
649+
) => {
650+
this.props.onRowContextMenu?.(row, e)
651+
}
652+
634653
private onRowMouseOver = (row: number, event: React.MouseEvent<any>) => {
635654
if (this.props.selectOnHover && this.canSelectRow(row)) {
636655
if (!this.props.selectedRows.includes(row)) {
@@ -943,6 +962,7 @@ export class List extends React.Component<IListProps, IListState> {
943962
onRowMouseOver={this.onRowMouseOver}
944963
onRowFocus={this.onRowFocus}
945964
onRowBlur={this.onRowBlur}
965+
onContextMenu={this.onRowContextMenu}
946966
style={params.style}
947967
tabIndex={tabIndex}
948968
children={element}

0 commit comments

Comments
 (0)