Skip to content

Commit 4828c71

Browse files
authored
Merge pull request #89 from rcpch/mbarton/wrapped-in-form-fix
If wrapped in form don't submit when clicking buttons
2 parents 467d180 + dc18cc5 commit 4828c71

6 files changed

+31
-5
lines changed

src/CentileChart/CentileChart.test.tsx

+16-1
Original file line numberDiff line numberDiff line change
@@ -1005,7 +1005,14 @@ describe("All tests relating to testing the copy button", () => {
10051005
fireEvent.mouseOver(screen.getByTestId('copy-button'));
10061006
expect(screen.getByTestId('copy-button')).toHaveStyle('background-color: ButtonFace');
10071007
})
1008-
1008+
it("should not trigger form submission when copy button clicked", () => {
1009+
const onSubmit = jest.fn();
1010+
render(<form onSubmit={onSubmit}>
1011+
<CentileChart {...props} />
1012+
</form>);
1013+
fireEvent.click(screen.getByTestId('copy-button'));
1014+
expect(onSubmit).not.toHaveBeenCalled();
1015+
});
10091016
})
10101017

10111018
describe("Tests relating to exportChartCallback function", () => {
@@ -1132,6 +1139,14 @@ describe("All tests relating to the zoom functionality where enableZoom needs to
11321139
fireEvent.click(screen.getByTestId('zoom-button'));
11331140
expect(screen.queryByTestId("resetzoom-button")).toBeInTheDocument();
11341141
})
1142+
it("should not trigger form submission when zoom button clicked", () => {
1143+
const onSubmit = jest.fn();
1144+
render(<form onSubmit={onSubmit}>
1145+
<CentileChart {...props} />
1146+
</form>);
1147+
fireEvent.click(screen.getByTestId('zoom-button'));
1148+
expect(onSubmit).not.toHaveBeenCalled();
1149+
});
11351150
})
11361151

11371152
describe("Tests relating to negative settings on the zoom button", () => {

src/SubComponents/CommonButton.tsx

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import * as React from 'react';
2+
3+
export const CommonButton = ({ children, ...props }) => {
4+
// if chart wrapped in a form tag prevent submitting it
5+
// as the default button type=submit
6+
return <button {...props} type="button">{children}</button>;
7+
}

src/SubComponents/StyledErrorButton.tsx

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import styled from 'styled-components';
2+
import { CommonButton } from './CommonButton';
23

3-
export const StyledErrorButton = styled.button<{
4+
export const StyledErrorButton = styled(CommonButton)<{
45
$activeColour: string;
56
$inactiveColour: string;
67
fontFamily: string;

src/SubComponents/StyledFullScreenButton.tsx

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import styled from 'styled-components';
2+
import { CommonButton } from './CommonButton';
23

3-
export const StyledFullScreenButton = styled.button<{
4+
export const StyledFullScreenButton = styled(CommonButton)<{
45
$color?: string,
56
size?: number
67
}>`

src/SubComponents/StyledResetZoomButton.tsx

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import styled from 'styled-components';
2+
import { CommonButton } from './CommonButton';
23

3-
export const StyledResetZoomButton = styled.button<{
4+
export const StyledResetZoomButton = styled(CommonButton)<{
45
$activeColour: string;
56
$inactiveColour: string;
67
$fontFamily: string;

src/SubComponents/StyledShareButton.tsx

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import styled from 'styled-components';
2+
import { CommonButton } from './CommonButton';
23

3-
export const StyledShareButton = styled.button<{
4+
export const StyledShareButton = styled(CommonButton)<{
45
$color?: string,
56
size?: number
67
}>`

0 commit comments

Comments
 (0)