This repository has been archived by the owner on Oct 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: move handle resize to its own hook (#342)
- Loading branch information
1 parent
3621785
commit ca15cca
Showing
13 changed files
with
170 additions
and
55 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
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
63 changes: 63 additions & 0 deletions
63
packages/sn-filter-pane/src/components/ListboxGrid/__tests__/use-handle-resize.spec.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 |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { MutableRefObject } from 'react'; | ||
import { act, waitFor, renderHook } from '@testing-library/react'; | ||
import useHandleResize from '../use-handle-resize'; | ||
import { IStore } from '../../../store'; | ||
import { IListboxResource } from '../../../hooks/types'; | ||
import { RenderTrackerService } from '../../../services/render-tracker'; | ||
import * as distResourcesMod from '../distribute-resources'; | ||
|
||
describe('use-handle-resize', () => { | ||
beforeAll(() => { | ||
jest.spyOn(distResourcesMod, 'calculateExpandPriority').mockReturnValue({ columns: [{}, {}, {}], expandedItemsCount: 3 }); | ||
}); | ||
|
||
it('should return a resize handler which, upon call, returns columns and overflowingResources, calculated by other funcs', async () => { | ||
const resources = [{ | ||
layout: {}, | ||
}] as unknown as IListboxResource[]; | ||
|
||
const gridRef = { | ||
current: {}, | ||
} as unknown as MutableRefObject<HTMLObjectElement>; | ||
|
||
const store = { | ||
getState: jest.fn().mockReturnValue({ heyHey: 1 }), | ||
setState: jest.fn(), | ||
} as unknown as IStore; | ||
|
||
const env = { | ||
sense: { | ||
isSmallDevice: () => false, | ||
}, | ||
}; | ||
|
||
const renderTracker = { | ||
setNumberOfListboxes: jest.fn(), | ||
renderedCallback: jest.fn(), | ||
} as unknown as RenderTrackerService; | ||
|
||
const { result, unmount } = renderHook(() => useHandleResize({ | ||
resources, | ||
gridRef, | ||
store, | ||
env, | ||
renderTracker, | ||
})); | ||
|
||
const { handleResize } = result.current; | ||
|
||
expect(result.current.columns).toEqual([]); | ||
expect(result.current.overflowingResources).toEqual([]); | ||
|
||
await act(() => { | ||
handleResize(); | ||
}); | ||
|
||
await waitFor(() => { | ||
expect(result.current.columns).toHaveLength(3); | ||
expect(result.current.overflowingResources).toHaveLength(0); | ||
}); | ||
|
||
unmount(); | ||
}); | ||
}); |
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
71 changes: 71 additions & 0 deletions
71
packages/sn-filter-pane/src/components/ListboxGrid/use-handle-resize.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 |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import { | ||
MutableRefObject, useCallback, useState, | ||
} from 'react'; | ||
import { RenderTrackerService } from '../../services/render-tracker'; | ||
import getWidthHeight from './get-size'; | ||
import { | ||
setDefaultValues, balanceColumns, calculateColumns, calculateExpandPriority, mergeColumnsAndResources, hasHeader, | ||
} from './distribute-resources'; | ||
import { ExpandProps, IColumn, ISize } from './interfaces'; | ||
import { IEnv } from '../../types/types'; | ||
import { IListboxResource } from '../../hooks/types'; | ||
import { IStore } from '../../store'; | ||
|
||
const prepareRenderTracker = (listboxCount: number, renderTracker?: RenderTrackerService) => { | ||
renderTracker?.setNumberOfListboxes(listboxCount); | ||
if (listboxCount === 0) { | ||
renderTracker?.renderedCallback(); | ||
} | ||
}; | ||
|
||
interface IUseHandleResize { | ||
resources: IListboxResource[]; | ||
gridRef: MutableRefObject<HTMLObjectElement | undefined>; | ||
store: IStore; | ||
env: IEnv; | ||
renderTracker?: RenderTrackerService; | ||
} | ||
|
||
export default function useHandleResize({ | ||
resources, | ||
gridRef, | ||
store, | ||
env, | ||
renderTracker, | ||
}: IUseHandleResize) { | ||
const { sense } = env as IEnv; | ||
|
||
const [overflowingResources, setOverflowingResources] = useState<IListboxResource[]>([]); | ||
const [columns, setColumns] = useState<IColumn[]>([]); | ||
|
||
const handleResize = () => { | ||
if (!resources?.length) { | ||
return; | ||
} | ||
const { width, height } = getWidthHeight(gridRef); | ||
const size: ISize = { width, height, dimensionCount: resources.length }; | ||
store.setState({ ...store.getState(), containerSize: size }); | ||
const isSmallDevice = sense?.isSmallDevice?.() ?? false; | ||
const isSingleItem = resources.length === 1; | ||
const expandProps: ExpandProps = { | ||
isSingleGridLayout: isSingleItem && resources[0].layout?.layoutOptions?.dataLayout === 'grid', | ||
hasHeader: hasHeader(resources[0]), | ||
}; | ||
const calculatedColumns = calculateColumns(size, [], isSmallDevice, expandProps); | ||
const balancedColumns = balanceColumns(size, calculatedColumns, isSmallDevice, expandProps); | ||
const resourcesWithDefaultValues = setDefaultValues(resources); | ||
const { columns: mergedColumnsAndResources, overflowing } = mergeColumnsAndResources(balancedColumns, resourcesWithDefaultValues); | ||
setOverflowingResources(overflowing); | ||
const { columns: expandedAndCollapsedColumns, expandedItemsCount } = calculateExpandPriority(mergedColumnsAndResources, size, expandProps); | ||
setColumns(expandedAndCollapsedColumns); | ||
prepareRenderTracker(expandedItemsCount, renderTracker); | ||
}; | ||
|
||
const handleResizeMemo = useCallback(() => handleResize(), [resources]); | ||
|
||
return { | ||
handleResize: handleResizeMemo, | ||
overflowingResources, | ||
columns, | ||
}; | ||
} |
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
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