Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(resize-handle): restore to useCallback, add tests #219

Merged
merged 1 commit into from
Aug 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "patch",
"comment": "fix(resize-handle): restore to useCallback, add tests",
"packageName": "@fluentui-contrib/react-resize-handle",
"email": "[email protected]",
"dependentChangeType": "patch"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import * as React from 'react';
import { test, expect } from '@playwright/experimental-ct-react';

import { TestArea } from './useResizeHandleExample.component-browser-spec';

test.use({ viewport: { width: 500, height: 500 } });

test.describe('useResizeHandle', () => {
test('"onChange" is called after dragging', async ({ mount, page }) => {
const component = await mount(<TestArea />);

const dragHandle = component.getByRole('separator');
const valueEl = component.getByTestId('value');

await expect(valueEl).toContainText('Default value');

// Drag the handle to the right
await dragHandle.hover();
await page.mouse.down();
await page.mouse.move(100, 0);
await page.mouse.up();

await expect(valueEl).toContainText('--width: 83px;');
});
});
11 changes: 4 additions & 7 deletions packages/react-resize-handle/src/hooks/useResizeHandle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ export type UseResizeHandleParams = {
maxValue?: number;
/**
* A callback that will be called when the element is resized.
*
* @remarks The passed function should be memoization for better performance.
*/
onChange?: (value: number) => void;
/**
Expand Down Expand Up @@ -81,11 +83,6 @@ export const useResizeHandle = (params: UseResizeHandleParams) => {
const elementRef = React.useRef<HTMLElement | null>(null);

const currentValue = React.useRef(UNMEASURED);
const handleChange: UseResizeHandleParams['onChange'] = useEventCallback(
(value) => {
onChange?.(value);
}
);

const updateElementsAttrs = React.useCallback(() => {
const a11yValue = relative
Expand Down Expand Up @@ -115,9 +112,9 @@ export const useResizeHandle = (params: UseResizeHandleParams) => {
variableName,
`${currentValue.current}px`
);
handleChange(currentValue.current);
onChange?.(currentValue.current);
}
}, [getA11ValueText, maxValue, minValue, handleChange, variableName]);
}, [getA11ValueText, maxValue, minValue, onChange, variableName]);

// In case the maxValue or minValue is changed, we need to make sure we are not exceeding the new limits
React.useEffect(() => {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import * as React from 'react';
import { useResizeHandle } from './useResizeHandle';

export function TestArea() {
const codeRef = React.useRef<HTMLElement>(null);
const handleChange = React.useCallback((value: number) => {
if (codeRef.current) {
codeRef.current.textContent = `--width: ${value}px;`;
}
}, []);

const handle = useResizeHandle({
variableName: '--width',
growDirection: 'end',
minValue: 50,
maxValue: 400,
onChange: handleChange,
});

return (
<div style={{ display: 'flex', flexDirection: 'column', gap: '12px' }}>
<div
ref={handle.wrapperRef}
style={{
'--width': '50px',
display: 'grid',
gridTemplateColumns: 'var(--width) 10px auto',
width: '100%',
height: '400px',
gap: '4px',
}}
>
<div
ref={handle.elementRef}
style={{
border: '2px dotted blue',
height: '100%',
}}
/>
<div
ref={handle.handleRef}
role="separator"
style={{
cursor: 'ew-resize',
backgroundColor: 'grey',
borderRadius: '4px',
}}
/>
<div
style={{
border: '2px dotted green',
height: '100%',
}}
/>
</div>
<code
ref={codeRef}
style={{ padding: '4px', border: '2px solid orange' }}
data-testid="value"
>
Default value
</code>
</div>
);
}