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

Fixes #494 Add optional abort for onResizeStart #495

Merged
merged 1 commit into from Aug 12, 2019
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/index.test.tsx
Expand Up @@ -290,6 +290,24 @@ test.serial('Should only bottomRight is resizable and call onResizeStart when mo
t.is(onResizeStart.getCall(0).args[1], 'bottomRight');
});

test.serial('Should not begin resize when onResizeStart returns false', async t => {
const onResizeStart = () => { return false; };
const onResize = sinon.spy();
const resizable = TestUtils.renderIntoDocument<ResizableProps, Resizable>(
<Resizable
onResizeStart={onResizeStart}
onResize={onResize}
/>,
);
if (!resizable || resizable instanceof Element) return t.fail();
const divs = TestUtils.scryRenderedDOMComponentsWithTag(resizable, 'div') as HTMLDivElement[];
const previousState = resizable.state.isResizing;
TestUtils.Simulate.mouseDown(ReactDOM.findDOMNode(divs[1]) as Element);
mouseMove(200, 220);
t.is(onResize.callCount, 0);
t.is(resizable.state.isResizing, previousState);
});

test.serial('should call onResize with expected args when resize direction right', async t => {
const onResize = sinon.spy();
const onResizeStart = sinon.spy();
Expand Down
7 changes: 5 additions & 2 deletions src/index.tsx
Expand Up @@ -74,7 +74,7 @@ export type ResizeStartCallback = (
e: React.MouseEvent<HTMLDivElement> | React.TouchEvent<HTMLDivElement>,
dir: Direction,
elementRef: HTMLDivElement,
) => void;
) => void | boolean;

export interface ResizableProps {
style?: React.CSSProperties;
Expand Down Expand Up @@ -631,7 +631,10 @@ export class Resizable extends React.Component<ResizableProps, State> {
}
if (this.props.onResizeStart) {
if (this.resizable) {
this.props.onResizeStart(event, direction, this.resizable);
const startResize = this.props.onResizeStart(event, direction, this.resizable);
if (startResize === false) {
return;
}
}
}

Expand Down