Skip to content

Commit 3a6ab33

Browse files
committed
fix(form): FileInput correctly center the icon when children aren't provided
1 parent 7c123ef commit 3a6ab33

File tree

3 files changed

+50
-4
lines changed

3 files changed

+50
-4
lines changed

packages/form/src/file-input/FileInput.tsx

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,11 @@ export interface FileInputProps extends ButtonThemeProps, InputAttributes {
3838
* Boolean if the children should not have some spacing between the icon and
3939
* itself. The default behavior is to use the `<TextIconSpacing>` component
4040
* for text styled input buttons, but this can be disabled if you want to use
41-
* a screenreader only accessible label.
41+
* a screen-reader only accessible label.
42+
*
43+
* Note: This will default to `false` if {@link children} are provided.
44+
*
45+
* @defaultValue `true`
4246
*/
4347
disableIconSpacing?: boolean;
4448

@@ -48,6 +52,14 @@ export interface FileInputProps extends ButtonThemeProps, InputAttributes {
4852
* selected.
4953
*/
5054
disableRepeatableFiles?: boolean;
55+
56+
/**
57+
* Children should generally be provided when the {@link buttonType} is
58+
* set to `"text"`. This defaults to a screen-reader only accessible text.
59+
*
60+
* @defaultValue `<SrOnly>Upload</SrOnly>`
61+
*/
62+
children?: ReactNode;
5163
}
5264

5365
const block = bem("rmd-file-input");
@@ -63,12 +75,12 @@ export const FileInput = forwardRef<HTMLInputElement, FileInputProps>(
6375
className: propClassName,
6476
icon: propIcon,
6577
iconAfter = false,
66-
children = <SrOnly>Upload</SrOnly>,
78+
children: propChildren,
6779
theme = "primary",
6880
themeType = "contained",
6981
buttonType = "icon",
7082
multiple = false,
71-
disableIconSpacing = false,
83+
disableIconSpacing: propDisableIconSpacing,
7284
disableRepeatableFiles = false,
7385
onKeyDown,
7486
onKeyUp,
@@ -86,6 +98,13 @@ export const FileInput = forwardRef<HTMLInputElement, FileInputProps>(
8698
) {
8799
const { id, disabled } = props;
88100
const icon = useIcon("download", propIcon);
101+
const disableIconSpacing =
102+
propDisableIconSpacing ?? typeof propChildren === "undefined";
103+
104+
let children = propChildren;
105+
if (typeof propChildren === "undefined") {
106+
children = <SrOnly>Upload</SrOnly>;
107+
}
89108

90109
const { ripples, className, handlers } = useInteractionStates({
91110
handlers: {

packages/form/src/file-input/__tests__/FileInput.tsx

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,31 @@ describe("FileInput", () => {
2020
);
2121
expect(container).toMatchSnapshot();
2222
});
23+
24+
it("should only render the screen-reader only accessible Upload text if there are no children", () => {
25+
const props = { id: "file-input", onChange: () => {} };
26+
const { container, getByText, rerender } = render(<FileInput {...props} />);
27+
expect(() => getByText("Upload")).not.toThrow();
28+
expect(container.querySelector(".rmd-icon--before")).toBe(null);
29+
expect(container.querySelector(".rmd-icon--after")).toBe(null);
30+
31+
rerender(<FileInput {...props}>Custom Children</FileInput>);
32+
expect(() => getByText("Upload")).toThrow();
33+
expect(container.querySelector(".rmd-icon--before")).not.toBe(null);
34+
expect(container.querySelector(".rmd-icon--after")).toBe(null);
35+
36+
rerender(
37+
<FileInput {...props} icon={null}>
38+
Custom Children
39+
</FileInput>
40+
);
41+
expect(() => getByText("Upload")).toThrow();
42+
expect(container.querySelector(".rmd-icon--before")).toBe(null);
43+
expect(container.querySelector(".rmd-icon--after")).toBe(null);
44+
45+
rerender(<FileInput {...props} icon={null} />);
46+
expect(() => getByText("Upload")).not.toThrow();
47+
expect(container.querySelector(".rmd-icon--before")).toBe(null);
48+
expect(container.querySelector(".rmd-icon--after")).toBe(null);
49+
});
2350
});

packages/form/src/file-input/__tests__/__snapshots__/FileInput.tsx.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ exports[`FileInput should render correctly 1`] = `
1414
>
1515
<i
1616
aria-hidden="true"
17-
class="rmd-icon rmd-icon--font material-icons rmd-icon--before"
17+
class="rmd-icon rmd-icon--font material-icons"
1818
>
1919
file_download
2020
</i>

0 commit comments

Comments
 (0)