Skip to content

Commit 644971d

Browse files
committed
fix(dialog): DialogFooter align prop applies correct classes
1 parent b50d745 commit 644971d

File tree

3 files changed

+217
-306
lines changed

3 files changed

+217
-306
lines changed

packages/dialog/src/DialogFooter.tsx

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,24 @@ import React, { forwardRef, HTMLAttributes } from "react";
22
import cn from "classnames";
33
import { bem } from "@react-md/utils";
44

5+
/**
6+
* An optional alignment for the content within the footer. Since the majority
7+
* of dialog footers are used to contain action buttons, the default alignment
8+
* is near the end.
9+
*
10+
* @remarks \@since 3.1.0
11+
*/
12+
export type DialogFooterAlignment =
13+
| "none"
14+
| "start"
15+
| "end"
16+
| "between"
17+
| "stacked-start"
18+
| "stacked-end";
19+
520
export interface DialogFooterProps extends HTMLAttributes<HTMLDivElement> {
6-
/**
7-
* An optional alignment for the content within the footer. Since the majority
8-
* of dialog footers are used to contain action buttons, the default alignment
9-
* is near the end.
10-
*/
11-
align?:
12-
| "none"
13-
| "start"
14-
| "end"
15-
| "between"
16-
| "stacked-start"
17-
| "stacked-end";
21+
/** {@inheritDoc DialogFooterAlignment} */
22+
align?: DialogFooterAlignment;
1823
}
1924

2025
const block = bem("rmd-dialog");
@@ -28,7 +33,10 @@ export const DialogFooter = forwardRef<HTMLDivElement, DialogFooterProps>(
2833
className={cn(
2934
block("footer", {
3035
flex: align !== "none",
31-
[align]: align !== "none",
36+
"flex-v": align === "stacked-start" || align === "stacked-end",
37+
start: align === "start" || align === "stacked-start",
38+
between: align === "between",
39+
end: align === "end" || align === "stacked-end",
3240
}),
3341
className
3442
)}
Lines changed: 21 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1-
import React from "react";
1+
import React, { ReactElement } from "react";
2+
import { Button } from "@react-md/button";
23
import { render } from "@testing-library/react";
3-
import { create } from "react-test-renderer";
44

5-
import { DialogFooter, DialogFooterProps } from "../DialogFooter";
5+
import {
6+
DialogFooter,
7+
DialogFooterAlignment,
8+
DialogFooterProps,
9+
} from "../DialogFooter";
610

7-
const aligns: DialogFooterProps["align"][] = [
11+
const aligns: readonly DialogFooterAlignment[] = [
812
"none",
913
"start",
1014
"end",
@@ -13,73 +17,23 @@ const aligns: DialogFooterProps["align"][] = [
1317
"stacked-end",
1418
];
1519

20+
function Test({ align }: DialogFooterProps): ReactElement {
21+
return (
22+
<DialogFooter align={align}>
23+
<Button>First Button</Button>
24+
<Button>Second Button</Button>
25+
</DialogFooter>
26+
);
27+
}
28+
1629
describe("DialogFooter", () => {
1730
it("should render correctly", () => {
18-
const { container } = render(<DialogFooter>Footer</DialogFooter>);
19-
const footer = container.querySelector("footer") as HTMLElement;
20-
21-
expect(footer).not.toBeNull();
22-
expect(footer.textContent).toBe("Footer");
23-
});
24-
25-
it('should apply the "rmd-dialog__footer--flex" className if the align prop is not "none"', () => {
26-
const { container, rerender } = render(<DialogFooter align="none" />);
27-
const footer = container.querySelector("footer") as HTMLElement;
28-
expect(footer.className).not.toContain("rmd-dialog__footer--flex");
29-
30-
aligns.slice(1).forEach((align) => {
31-
rerender(<DialogFooter align={align} />);
32-
expect(footer.className).toContain("rmd-dialog__footer--flex");
33-
});
34-
});
35-
36-
it('should apply the "rmd-dialog__footer--ALIGN" className if the align prop is not "none"', () => {
37-
const { container, rerender } = render(<DialogFooter align="none" />);
38-
const footer = container.querySelector("footer") as HTMLElement;
39-
expect(footer.className).not.toContain("rmd-dialog__footer--none");
31+
const { container, rerender } = render(<Test />);
32+
expect(container).toMatchSnapshot();
4033

41-
aligns.slice(1).forEach((align) => {
42-
rerender(<DialogFooter align={align} />);
43-
expect(footer.className).toContain(`rmd-dialog__footer--${align}`);
44-
});
45-
});
46-
47-
it('should default to align="end"', () => {
48-
const { container } = render(<DialogFooter />);
49-
50-
const footer = container.querySelector("footer") as HTMLElement;
51-
expect(footer).not.toBeNull();
52-
53-
expect(footer.className).toContain("rmd-dialog__footer--end");
54-
});
55-
56-
it("should render correctly (with snapshots)", () => {
5734
aligns.forEach((align) => {
58-
expect(
59-
create(
60-
<DialogFooter id="dialog-footer" align={align}>
61-
<button id="close-dialog" type="button">
62-
Close
63-
</button>
64-
<button id="confirm-dialog" type="button">
65-
Confirm
66-
</button>
67-
</DialogFooter>
68-
).toJSON()
69-
).toMatchSnapshot();
70-
71-
expect(
72-
create(
73-
<DialogFooter id="dialog-footer" className="custom-class names-here">
74-
<button id="close-dialog" type="button">
75-
Close
76-
</button>
77-
<button id="confirm-dialog" type="button">
78-
Confirm
79-
</button>
80-
</DialogFooter>
81-
).toJSON()
82-
).toMatchSnapshot();
35+
rerender(<Test align={align} />);
36+
expect(container).toMatchSnapshot();
8337
});
8438
});
8539
});

0 commit comments

Comments
 (0)