Skip to content

Commit 4d63e96

Browse files
committed
fix: review comments
1 parent 7092df7 commit 4d63e96

File tree

7 files changed

+32
-64
lines changed

7 files changed

+32
-64
lines changed

src/Common/Hooks/UseRegisterShortcut/UseRegisterShortcutProvider.tsx

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,8 @@ const DEFAULT_TIMEOUT = 300
2626
const UseRegisterShortcutProvider = ({
2727
ignoreTags,
2828
preventDefault = false,
29-
stopPropagation = false,
3029
shortcutTimeout,
3130
children,
32-
eventListenerTargetRef,
3331
}: UseRegisterShortcutProviderType) => {
3432
const disableShortcutsRef = useRef<boolean>(false)
3533
const shortcutsRef = useRef<Record<string, ShortcutType>>({})
@@ -122,10 +120,6 @@ const UseRegisterShortcutProvider = ({
122120
event.preventDefault()
123121
}
124122

125-
if (stopPropagation) {
126-
event.stopPropagation()
127-
}
128-
129123
if (
130124
ignoredTags.map((tag) => tag.toUpperCase()).indexOf((event.target as HTMLElement).tagName.toUpperCase()) >
131125
-1 ||
@@ -162,16 +156,14 @@ const UseRegisterShortcutProvider = ({
162156
}, [])
163157

164158
useEffect(() => {
165-
const target = eventListenerTargetRef?.current ?? window
166-
167-
target.addEventListener('keydown', handleKeydownEvent)
168-
target.addEventListener('keyup', handleKeyupEvent)
169-
target.addEventListener('blur', handleBlur)
159+
window.addEventListener('keydown', handleKeydownEvent)
160+
window.addEventListener('keyup', handleKeyupEvent)
161+
window.addEventListener('blur', handleBlur)
170162

171163
return () => {
172-
target.removeEventListener('keydown', handleKeydownEvent)
173-
target.removeEventListener('keyup', handleKeyupEvent)
174-
target.removeEventListener('blur', handleBlur)
164+
window.removeEventListener('keydown', handleKeydownEvent)
165+
window.removeEventListener('keyup', handleKeyupEvent)
166+
window.removeEventListener('blur', handleBlur)
175167

176168
if (keyDownTimeoutRef.current > -1) {
177169
clearTimeout(keyDownTimeoutRef.current)

src/Common/Hooks/UseRegisterShortcut/types.ts

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -93,13 +93,4 @@ export interface UseRegisterShortcutProviderType {
9393
* If true, call preventDefault on the event
9494
*/
9595
preventDefault?: boolean
96-
/**
97-
* If true, call stopPropagation on the event
98-
*/
99-
stopPropagation?: boolean
100-
/**
101-
* The target to which the event listener is attached
102-
* If not provided, the event listener will be attached to window
103-
*/
104-
eventListenerTargetRef?: React.RefObject<HTMLElement>
10596
}

src/Shared/Components/Table/InternalTable.tsx

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,10 @@ const InternalTable = ({
4141
handleToggleBulkSelectionOnRow,
4242
paginationVariant,
4343
RowActionsOnHoverComponent,
44-
tableContainerRef,
4544
}: InternalTableProps) => {
4645
const rowsContainerRef = useRef<HTMLDivElement>(null)
4746
const parentRef = useRef<HTMLDivElement>(null)
47+
const activeRowRef = useRef<HTMLDivElement>(null)
4848

4949
const { BulkActionsComponent } = bulkSelectionConfig ?? {}
5050

@@ -138,15 +138,8 @@ const InternalTable = ({
138138
)
139139

140140
useEffectAfterMount(() => {
141-
rowsContainerRef.current.scrollTo({
142-
top: 0,
143-
left: 0,
144-
behavior: 'smooth',
145-
})
146-
147-
// !FIXME: this will scroll to top but without smooth animation
148141
setActiveRowIndex(0)
149-
}, [offset])
142+
}, [offset, visibleRows])
150143

151144
useEffect(() => {
152145
setIdentifiers?.(
@@ -163,13 +156,14 @@ const InternalTable = ({
163156
handleSorting(newSortBy)
164157
}
165158

166-
const scrollIntoViewActiveRowRefCallback = (node: HTMLDivElement) => {
167-
if (!node || node.dataset.active !== 'true') {
168-
return
169-
}
170-
171-
node.focus()
172-
}
159+
useEffect(() => {
160+
// Focus the active row only when activeRowIndex changes. This ensures that focus is not stolen from other elements,
161+
// such as the search bar, when it is already focused. For example, when typing in the search bar, the rows may be replaced
162+
// with loading shimmers, causing activeRowRef to be null. During this time, activeRowIndex might reset to 0, but focus
163+
// will not shift. However, when navigating with arrow keys, activeRowIndex changes, and activeRowRef will point to the
164+
// correct row once it is mounted, allowing focus to update appropriately.
165+
activeRowRef.current?.focus()
166+
}, [activeRowIndex])
173167

174168
const showPagination =
175169
paginationVariant === PaginationEnum.PAGINATED && filteredRows?.length > DEFAULT_BASE_PAGE_SIZE
@@ -225,12 +219,12 @@ const InternalTable = ({
225219
return (
226220
<div
227221
key={row.id}
228-
ref={scrollIntoViewActiveRowRefCallback}
222+
ref={isRowActive ? activeRowRef : null}
229223
onClick={handleChangeActiveRowIndex}
230-
className={`dc__grid px-20 form__checkbox-parent ${
224+
className={`dc__grid px-20 checkbox__parent-container ${
231225
showSeparatorBetweenRows ? 'border__secondary--bottom' : ''
232226
} fs-13 fw-4 lh-20 cn-9 generic-table__row dc__gap-16 ${
233-
isRowActive ? 'generic-table__row--active form__checkbox-parent--active' : ''
227+
isRowActive ? 'generic-table__row--active checkbox__parent-container--active' : ''
234228
} ${RowActionsOnHoverComponent ? 'dc__position-rel dc__opacity-hover dc__opacity-hover--parent' : ''} ${
235229
isRowBulkSelected ? 'generic-table__row--bulk-selected' : ''
236230
}`}
@@ -299,7 +293,7 @@ const InternalTable = ({
299293
}
300294

301295
return (
302-
<>
296+
<div tabIndex={0} role="grid" className="generic-table flexbox-col dc__overflow-hidden flex-grow-1">
303297
<div className="flexbox-col flex-grow-1 w-100 dc__overflow-auto" ref={parentRef}>
304298
<div className="bg__primary dc__min-width-fit-content px-20 border__secondary--bottom">
305299
{loading && !visibleColumns.length ? (
@@ -369,7 +363,7 @@ const InternalTable = ({
369363
size={filteredRows.length}
370364
/>
371365
)}
372-
</>
366+
</div>
373367
)
374368
}
375369

@@ -388,9 +382,7 @@ const InternalTable = ({
388382
: {}),
389383
}}
390384
>
391-
<div ref={tableContainerRef} className="generic-table flexbox-col dc__overflow-hidden flex-grow-1">
392-
{renderContent()}
393-
</div>
385+
{renderContent()}
394386
</Wrapper>
395387
)
396388
}

src/Shared/Components/Table/Table.component.tsx

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useCallback, useEffect, useMemo, useRef, useState } from 'react'
1+
import { useCallback, useEffect, useMemo, useState } from 'react'
22
import {
33
noop,
44
UseRegisterShortcutProvider,
@@ -25,10 +25,10 @@ import InternalTable from './InternalTable'
2525
import './styles.scss'
2626

2727
const UseResizableTableConfigWrapper = (props: InternalTableProps) => {
28-
const { columns } = props
28+
const { visibleColumns } = props
2929

3030
const resizableConfig = useResizableTableConfig({
31-
headersConfig: columns.map(({ label, size }) => {
31+
headersConfig: visibleColumns.map(({ label, size }) => {
3232
if (size.range) {
3333
const {
3434
range: { minWidth, maxWidth, startWidth },
@@ -189,25 +189,20 @@ const UseUrlFilterWrapper = (props: FilterWrapperProps) => {
189189

190190
const TableWrapper = (tableProps: TableProps) => {
191191
const { filtersVariant } = tableProps
192-
const tableContainerRef = useRef<HTMLDivElement>(null)
193192

194193
const renderContent = () => {
195-
if (filtersVariant === FiltersTypeEnum.NONE) {
196-
return <UseStateFilterWrapper {...{ ...tableProps, tableContainerRef }} />
194+
if (filtersVariant === FiltersTypeEnum.STATE) {
195+
return <UseStateFilterWrapper {...tableProps} />
197196
}
198197

199198
if (filtersVariant === FiltersTypeEnum.URL) {
200-
return <UseUrlFilterWrapper {...{ ...tableProps, tableContainerRef }} />
199+
return <UseUrlFilterWrapper {...tableProps} />
201200
}
202201

203-
return <VisibleColumnsWrapper {...{ ...tableProps, tableContainerRef, filterData: null }} />
202+
return <VisibleColumnsWrapper {...{ ...tableProps, filterData: null }} />
204203
}
205204

206-
return (
207-
<UseRegisterShortcutProvider eventListenerTargetRef={tableContainerRef} shortcutTimeout={50} stopPropagation>
208-
{renderContent()}
209-
</UseRegisterShortcutProvider>
210-
)
205+
return <UseRegisterShortcutProvider shortcutTimeout={50}>{renderContent()}</UseRegisterShortcutProvider>
211206
}
212207

213208
export default TableWrapper

src/Shared/Components/Table/constants.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export const EVENT_TARGET = new EventTarget()
1111
export const DRAG_SELECTOR_IDENTIFIER = 'table-drag-selector'
1212

1313
export const TABLE_ID_MAP = {
14-
STORYBOOK: 'storybook',
14+
STORYBOOK: 'storybook-table',
1515
} as const
1616

1717
export const SHIMMER_DUMMY_ARRAY = [1, 2, 3]

src/Shared/Components/Table/types.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,8 +190,6 @@ export type InternalTableProps = Required<Pick<ConfigurableColumnsType, 'visible
190190
showSeparatorBetweenRows: boolean
191191
}
192192

193-
tableContainerRef: React.RefObject<HTMLDivElement>
194-
195193
/**
196194
* Use this component to display additional content at the end of a row when it is hovered over.
197195
*/

src/Shared/Components/Table/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export const searchAndSortRows = (
2222
filterData: UseFiltersReturnType,
2323
comparator?: Column['comparator'],
2424
) => {
25-
const { searchKey, sortBy, sortOrder } = filterData
25+
const { searchKey, sortBy, sortOrder } = filterData ?? {}
2626

2727
const filteredRows = searchKey ? rows.filter((row) => filter(row, filterData)) : rows
2828

0 commit comments

Comments
 (0)