Skip to content

Commit

Permalink
If wrapped in form don't submit when clicking buttons
Browse files Browse the repository at this point in the history
  • Loading branch information
mbarton committed Jun 24, 2024
1 parent 2926d80 commit ece4bad
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 5 deletions.
17 changes: 16 additions & 1 deletion src/CentileChart/CentileChart.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1005,7 +1005,14 @@ describe("All tests relating to testing the copy button", () => {
fireEvent.mouseOver(screen.getByTestId('copy-button'));
expect(screen.getByTestId('copy-button')).toHaveStyle('background-color: ButtonFace');
})

it("should not trigger form submission when copy button clicked", () => {
const onSubmit = jest.fn();
render(<form onSubmit={onSubmit}>
<CentileChart {...props} />
</form>);
fireEvent.click(screen.getByTestId('copy-button'));
expect(onSubmit).not.toHaveBeenCalled();
});
})

describe("Tests relating to exportChartCallback function", () => {
Expand Down Expand Up @@ -1132,6 +1139,14 @@ describe("All tests relating to the zoom functionality where enableZoom needs to
fireEvent.click(screen.getByTestId('zoom-button'));
expect(screen.queryByTestId("resetzoom-button")).toBeInTheDocument();
})
it("should not trigger form submission when zoom button clicked", () => {
const onSubmit = jest.fn();
render(<form onSubmit={onSubmit}>
<CentileChart {...props} />
</form>);
fireEvent.click(screen.getByTestId('zoom-button'));
expect(onSubmit).not.toHaveBeenCalled();
});
})

describe("Tests relating to negative settings on the zoom button", () => {
Expand Down
7 changes: 7 additions & 0 deletions src/SubComponents/CommonButton.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import * as React from 'react';

export const CommonButton = ({ children, ...props }) => {
// if chart wrapped in a form tag prevent submitting it
// as the default button type=submit
return <button {...props} type="button">{children}</button>;
}
3 changes: 2 additions & 1 deletion src/SubComponents/StyledErrorButton.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import styled from 'styled-components';
import { CommonButton } from './CommonButton';

export const StyledErrorButton = styled.button<{
export const StyledErrorButton = styled(CommonButton)<{
$activeColour: string;
$inactiveColour: string;
fontFamily: string;
Expand Down
3 changes: 2 additions & 1 deletion src/SubComponents/StyledFullScreenButton.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import styled from 'styled-components';
import { CommonButton } from './CommonButton';

export const StyledFullScreenButton = styled.button<{
export const StyledFullScreenButton = styled(CommonButton)<{
$color?: string,
size?: number
}>`
Expand Down
3 changes: 2 additions & 1 deletion src/SubComponents/StyledResetZoomButton.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import styled from 'styled-components';
import { CommonButton } from './CommonButton';

export const StyledResetZoomButton = styled.button<{
export const StyledResetZoomButton = styled(CommonButton)<{
$activeColour: string;
$inactiveColour: string;
$fontFamily: string;
Expand Down
3 changes: 2 additions & 1 deletion src/SubComponents/StyledShareButton.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import styled from 'styled-components';
import { CommonButton } from './CommonButton';

export const StyledShareButton = styled.button<{
export const StyledShareButton = styled(CommonButton)<{
$color?: string,
size?: number
}>`
Expand Down

0 comments on commit ece4bad

Please sign in to comment.