Skip to content

Commit 895d614

Browse files
committed
Avoid double translation in buttons
1 parent 862d9cb commit 895d614

File tree

7 files changed

+45
-11
lines changed

7 files changed

+45
-11
lines changed

packages/ra-ui-materialui/src/button/Button.tsx

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,12 @@ export const Button = <RootComponent extends React.ElementType = 'button'>(
4545
} = props;
4646

4747
const translate = useTranslate();
48-
const translatedLabel = label ? translate(label, { _: label }) : undefined;
48+
let translatedLabel = label;
49+
if (typeof label === 'string') {
50+
translatedLabel = translate(label, {
51+
_: label,
52+
});
53+
}
4954
const linkParams = getLinkParams(locationDescriptor);
5055

5156
const isXSmall = useMediaQuery((theme: Theme) =>
@@ -56,7 +61,12 @@ export const Button = <RootComponent extends React.ElementType = 'button'>(
5661
label && !disabled ? (
5762
<Tooltip title={translatedLabel}>
5863
<IconButton
59-
aria-label={translatedLabel}
64+
// If users provide a ReactNode as label, its their responsibility to also provide an aria-label should they need it
65+
aria-label={
66+
typeof translatedLabel === 'string'
67+
? translatedLabel
68+
: undefined
69+
}
6070
className={className}
6171
color={color}
6272
size="large"
@@ -83,7 +93,12 @@ export const Button = <RootComponent extends React.ElementType = 'button'>(
8393
className={className}
8494
color={color}
8595
size={size}
86-
aria-label={translatedLabel}
96+
// If users provide a ReactNode as label, its their responsibility to also provide an aria-label should they need it
97+
aria-label={
98+
typeof translatedLabel === 'string'
99+
? translatedLabel
100+
: undefined
101+
}
87102
disabled={disabled}
88103
startIcon={alignIcon === 'left' && children ? children : undefined}
89104
endIcon={alignIcon === 'right' && children ? children : undefined}
@@ -102,7 +117,7 @@ interface Props<RootComponent extends React.ElementType> {
102117
component?: RootComponent;
103118
to?: LocationDescriptor | To;
104119
disabled?: boolean;
105-
label?: string;
120+
label?: React.ReactNode;
106121
size?: 'small' | 'medium' | 'large';
107122
variant?: string;
108123
}

packages/ra-ui-materialui/src/button/CreateButton.tsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,8 @@ const CreateButton = (inProps: CreateButtonProps) => {
9494
// @ts-ignore FabProps ships its own runtime palette `FabPropsColorOverrides` provoking an overlap error with `ButtonProps`
9595
color="primary"
9696
className={clsx(CreateButtonClasses.floating, className)}
97-
aria-label={label}
97+
// If users provide a ReactNode as label, its their responsibility to also provide an aria-label should they need it
98+
aria-label={typeof label === 'string' ? label : undefined}
9899
{...rest}
99100
{...linkParams}
100101
>
@@ -106,7 +107,10 @@ const CreateButton = (inProps: CreateButtonProps) => {
106107
to={createPath({ resource, type: 'create' })}
107108
state={state}
108109
className={clsx(CreateButtonClasses.root, className)}
109-
label={label}
110+
// avoid double translation
111+
label={<>{label}</>}
112+
// If users provide a ReactNode as label, its their responsibility to also provide an aria-label should they need it
113+
aria-label={typeof label === 'string' ? label : undefined}
110114
variant={variant}
111115
{...(rest as any)}
112116
{...linkParams}

packages/ra-ui-materialui/src/button/DeleteWithConfirmButton.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,10 @@ export const DeleteWithConfirmButton = <RecordType extends RaRecord = any>(
9898
<Fragment>
9999
<Button
100100
onClick={handleDialogOpen}
101-
label={label}
101+
// avoid double translation
102+
label={<>{label}</>}
103+
// If users provide a ReactNode as label, its their responsibility to also provide an aria-label should they need it
104+
aria-label={typeof label === 'string' ? label : undefined}
102105
className={clsx('ra-delete-button', className)}
103106
key="button"
104107
color={color}

packages/ra-ui-materialui/src/button/DeleteWithUndoButton.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,10 @@ export const DeleteWithUndoButton = <RecordType extends RaRecord = any>(
6969
<Button
7070
onClick={handleDelete}
7171
disabled={isPending}
72-
label={label}
72+
// avoid double translation
73+
label={<>{label}</>}
74+
// If users provide a ReactNode as label, its their responsibility to also provide an aria-label should they need it
75+
aria-label={typeof label === 'string' ? label : undefined}
7376
className={clsx('ra-delete-button', className)}
7477
key="button"
7578
color={color}

packages/ra-ui-materialui/src/button/EditButton.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,10 @@ export const EditButton = <RecordType extends RaRecord = any>(
8686
component={Link}
8787
to={createPath({ type: 'edit', resource, id: record.id })}
8888
state={scrollStates[String(scrollToTop)]}
89-
label={label}
89+
// avoid double translation
90+
label={<>{label}</>}
91+
// If users provide a ReactNode as label, its their responsibility to also provide an aria-label should they need it
92+
aria-label={typeof label === 'string' ? label : undefined}
9093
onClick={stopPropagation}
9194
className={clsx(EditButtonClasses.root, className)}
9295
{...(rest as any)}

packages/ra-ui-materialui/src/button/ListButton.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,10 @@ export const ListButton = (props: ListButtonProps) => {
7575
component={Link}
7676
to={createPath({ type: 'list', resource })}
7777
state={scrollStates[String(scrollToTop)]}
78-
label={label}
78+
// avoid double translation
79+
label={<>{label}</>}
80+
// If users provide a ReactNode as label, its their responsibility to also provide an aria-label should they need it
81+
aria-label={typeof label === 'string' ? label : undefined}
7982
{...(rest as any)}
8083
>
8184
{icon}

packages/ra-ui-materialui/src/button/ShowButton.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,10 @@ const ShowButton = <RecordType extends RaRecord = any>(
7878
component={Link}
7979
to={createPath({ type: 'show', resource, id: record.id })}
8080
state={scrollStates[String(scrollToTop)]}
81-
label={label}
81+
// avoid double translation
82+
label={<>{label}</>}
83+
// If users provide a ReactNode as label, its their responsibility to also provide an aria-label should they need it
84+
aria-label={typeof label === 'string' ? label : undefined}
8285
onClick={stopPropagation}
8386
{...(rest as any)}
8487
>

0 commit comments

Comments
 (0)