-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rename code for active-row to active-item
Signed-off-by: Mike Turley <[email protected]>
- Loading branch information
Showing
13 changed files
with
127 additions
and
132 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 18 additions & 18 deletions
36
client/src/app/hooks/table-controls/active-item/getActiveItemDerivedState.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,32 @@ | ||
import { KeyWithValueType } from "@app/utils/type-utils"; | ||
import { IActiveRowState } from "./useActiveItemState"; | ||
import { IActiveItemState } from "./useActiveItemState"; | ||
|
||
export interface IActiveRowDerivedStateArgs<TItem> { | ||
export interface IActiveItemDerivedStateArgs<TItem> { | ||
currentPageItems: TItem[]; | ||
idProperty: KeyWithValueType<TItem, string | number>; | ||
activeRowState: IActiveRowState; | ||
activeItemState: IActiveItemState; | ||
} | ||
|
||
export interface IActiveRowDerivedState<TItem> { | ||
activeRowItem: TItem | null; | ||
setActiveRowItem: (item: TItem | null) => void; | ||
clearActiveRow: () => void; | ||
isActiveRowItem: (item: TItem) => boolean; | ||
export interface IActiveItemDerivedState<TItem> { | ||
activeItem: TItem | null; | ||
setActiveItem: (item: TItem | null) => void; | ||
clearActiveItem: () => void; | ||
isActiveItem: (item: TItem) => boolean; | ||
} | ||
|
||
// Note: This is not named `getLocalActiveRowDerivedState` because it is always local, | ||
// Note: This is not named `getLocalActiveItemDerivedState` because it is always local, | ||
// and it is still used when working with server-managed tables. | ||
export const getActiveRowDerivedState = <TItem>({ | ||
export const getActiveItemDerivedState = <TItem>({ | ||
currentPageItems, | ||
idProperty, | ||
activeRowState: { activeRowId, setActiveRowId }, | ||
}: IActiveRowDerivedStateArgs<TItem>): IActiveRowDerivedState<TItem> => ({ | ||
activeRowItem: | ||
currentPageItems.find((item) => item[idProperty] === activeRowId) || null, | ||
setActiveRowItem: (item: TItem | null) => { | ||
activeItemState: { activeItemId, setActiveItemId }, | ||
}: IActiveItemDerivedStateArgs<TItem>): IActiveItemDerivedState<TItem> => ({ | ||
activeItem: | ||
currentPageItems.find((item) => item[idProperty] === activeItemId) || null, | ||
setActiveItem: (item: TItem | null) => { | ||
const itemId = (item?.[idProperty] ?? null) as string | number | null; // TODO Assertion shouldn't be necessary here but TS isn't fully inferring item[idProperty]? | ||
setActiveRowId(itemId); | ||
setActiveItemId(itemId); | ||
}, | ||
clearActiveRow: () => setActiveRowId(null), | ||
isActiveRowItem: (item) => item[idProperty] === activeRowId, | ||
clearActiveItem: () => setActiveItemId(null), | ||
isActiveItem: (item) => item[idProperty] === activeItemId, | ||
}); |
28 changes: 14 additions & 14 deletions
28
client/src/app/hooks/table-controls/active-item/useActiveItemEffects.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,23 @@ | ||
import * as React from "react"; | ||
import { getActiveRowDerivedState } from "./getActiveItemDerivedState"; | ||
import { IActiveRowState } from "./useActiveItemState"; | ||
import { getActiveItemDerivedState } from "./getActiveItemDerivedState"; | ||
import { IActiveItemState } from "./useActiveItemState"; | ||
|
||
export interface IUseActiveRowEffectsArgs<TItem> { | ||
export interface IUseActiveItemEffectsArgs<TItem> { | ||
isLoading?: boolean; | ||
activeRowState: IActiveRowState; | ||
activeRowDerivedState: ReturnType<typeof getActiveRowDerivedState<TItem>>; | ||
activeItemState: IActiveItemState; | ||
activeItemDerivedState: ReturnType<typeof getActiveItemDerivedState<TItem>>; | ||
} | ||
|
||
export const useActiveRowEffects = <TItem>({ | ||
export const useActiveItemEffects = <TItem>({ | ||
isLoading, | ||
activeRowState: { activeRowId }, | ||
activeRowDerivedState: { activeRowItem, clearActiveRow }, | ||
}: IUseActiveRowEffectsArgs<TItem>) => { | ||
activeItemState: { activeItemId }, | ||
activeItemDerivedState: { activeItem, clearActiveItem }, | ||
}: IUseActiveItemEffectsArgs<TItem>) => { | ||
React.useEffect(() => { | ||
// If some state change (e.g. refetch, pagination) causes the active row to disappear, | ||
// remove its id from state so the drawer won't automatically reopen if the row comes back. | ||
if (!isLoading && activeRowId && !activeRowItem) { | ||
clearActiveRow(); | ||
// If some state change (e.g. refetch, pagination) causes the active item to disappear, | ||
// remove its id from state so the drawer won't automatically reopen if the item comes back. | ||
if (!isLoading && activeItemId && !activeItem) { | ||
clearActiveItem(); | ||
} | ||
}, [isLoading, activeRowId, activeRowItem]); | ||
}, [isLoading, activeItemId, activeItem]); | ||
}; |
42 changes: 21 additions & 21 deletions
42
client/src/app/hooks/table-controls/active-item/useActiveItemPropHelpers.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,47 @@ | ||
import { TrProps } from "@patternfly/react-table"; | ||
import { | ||
IActiveRowDerivedStateArgs, | ||
getActiveRowDerivedState, | ||
IActiveItemDerivedStateArgs, | ||
getActiveItemDerivedState, | ||
} from "./getActiveItemDerivedState"; | ||
import { IActiveRowState } from "./useActiveItemState"; | ||
import { IActiveItemState } from "./useActiveItemState"; | ||
import { | ||
IUseActiveRowEffectsArgs, | ||
useActiveRowEffects, | ||
IUseActiveItemEffectsArgs, | ||
useActiveItemEffects, | ||
} from "./useActiveItemEffects"; | ||
|
||
// Args that should be passed into useTableControlProps | ||
export type IActiveRowPropHelpersExternalArgs<TItem> = | ||
IActiveRowDerivedStateArgs<TItem> & | ||
Omit<IUseActiveRowEffectsArgs<TItem>, "activeRowDerivedState"> & { | ||
export type IActiveItemPropHelpersExternalArgs<TItem> = | ||
IActiveItemDerivedStateArgs<TItem> & | ||
Omit<IUseActiveItemEffectsArgs<TItem>, "activeItemDerivedState"> & { | ||
isLoading?: boolean; | ||
activeRowState: IActiveRowState; | ||
activeItemState: IActiveItemState; | ||
}; | ||
|
||
export const useActiveRowPropHelpers = <TItem>( | ||
args: IActiveRowPropHelpersExternalArgs<TItem> | ||
export const useActiveItemPropHelpers = <TItem>( | ||
args: IActiveItemPropHelpersExternalArgs<TItem> | ||
) => { | ||
const activeRowDerivedState = getActiveRowDerivedState(args); | ||
const { isActiveRowItem, setActiveRowItem, clearActiveRow } = | ||
activeRowDerivedState; | ||
const activeItemDerivedState = getActiveItemDerivedState(args); | ||
const { isActiveItem, setActiveItem, clearActiveItem } = | ||
activeItemDerivedState; | ||
|
||
useActiveRowEffects({ ...args, activeRowDerivedState }); | ||
useActiveItemEffects({ ...args, activeItemDerivedState }); | ||
|
||
const getActiveRowTrProps = ({ | ||
const getActiveItemTrProps = ({ | ||
item, | ||
}: { | ||
item: TItem; | ||
}): Omit<TrProps, "ref"> => ({ | ||
isSelectable: true, | ||
isClickable: true, | ||
isRowSelected: item && isActiveRowItem(item), | ||
isRowSelected: item && isActiveItem(item), | ||
onRowClick: () => { | ||
if (!isActiveRowItem(item)) { | ||
setActiveRowItem(item); | ||
if (!isActiveItem(item)) { | ||
setActiveItem(item); | ||
} else { | ||
clearActiveRow(); | ||
clearActiveItem(); | ||
} | ||
}, | ||
}); | ||
|
||
return { activeRowDerivedState, getActiveRowTrProps }; | ||
return { activeItemDerivedState, getActiveItemTrProps }; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.