Skip to content

Commit

Permalink
Merge pull request #6029 from marmelab/fix-sortbutton-default-label
Browse files Browse the repository at this point in the history
Fix SortButton labels must have a valid translation message
  • Loading branch information
JulienMattiussi authored Mar 12, 2021
2 parents a171deb + 3e6b82c commit 3c40409
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
8 changes: 4 additions & 4 deletions packages/ra-core/src/util/getFieldLabelTranslationArgs.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import inflection from 'inflection';

interface Args {
label: string;
resource: string;
source: string;
label?: string;
resource?: string;
source?: string;
}

type TranslationArguments = [string, any?];
Expand All @@ -17,7 +17,7 @@ type TranslationArguments = [string, any?];
* {translate(...getFieldLabelTranslationArgs({ label, resource, source }))}
* </span>
*/
export default (options: Args): TranslationArguments => {
export default (options?: Args): TranslationArguments => {
if (!options) {
return [''];
}
Expand Down
20 changes: 17 additions & 3 deletions packages/ra-ui-materialui/src/button/SortButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@ import {
import SortIcon from '@material-ui/icons/Sort';
import ArrowDropDownIcon from '@material-ui/icons/ArrowDropDown';
import { shallowEqual } from 'react-redux';
import { useListSortContext, useTranslate } from 'ra-core';
import {
useListSortContext,
useTranslate,
getFieldLabelTranslationArgs,
} from 'ra-core';

/**
* A button allowing to change the sort field and order.
Expand Down Expand Up @@ -72,7 +76,12 @@ const SortButton: FC<SortButtonProps> = ({
};

const buttonLabel = translate(label, {
field: translate(`resources.${resource}.fields.${currentSort.field}`),
field: translate(
...getFieldLabelTranslationArgs({
resource,
source: currentSort.field,
})
),
order: translate(`ra.sort.${currentSort.order}`),
_: label,
});
Expand Down Expand Up @@ -115,7 +124,12 @@ const SortButton: FC<SortButtonProps> = ({
data-sort={field}
key={field}
>
{translate(`resources.${resource}.fields.${field}`)}{' '}
{translate(
...getFieldLabelTranslationArgs({
resource,
source: field,
})
)}{' '}
{translate(
`ra.sort.${
currentSort.field === field
Expand Down

0 comments on commit 3c40409

Please sign in to comment.