Reputation: 555
The MobileDatePicker in the @mui/x-date-pickers pacakge contains accept/change buttons with default text OK/CANCEL.
In v4 the DatePicker component had a acceptLabel/cancelLabel prop to change the text of the buttons, but i find anything like that for the v5 component.
How can i change the text of the buttons in v5?
Upvotes: 6
Views: 6882
Reputation: 8600
I have made this i18n by creating a customPtBRLocaleText. Take a look:
const customPtBRLocaleText: Partial<PickersLocaleText<any>> = {
okButtonLabel: "Ok",
cancelButtonLabel: "Cancelar"
};
export default function ResponsiveDateTimePickers() {
const [value, setValue] = React.useState<Dayjs | null>(
dayjs("2023-01-01T00:00:00.000Z")
);
return (
<LocalizationProvider
dateAdapter={AdapterDateFns}
adapterLocale={ptBR}
localeText={customPtBRLocaleText}
>
<Stack spacing={3}>
<MobileDateTimePicker
label="For mobile"
value={value}
onChange={(newValue) => {
setValue(newValue);
}}
renderInput={(params) => <TextField {...params} />}
/>
</Stack>
</LocalizationProvider>
);
}
Upvotes: 5
Reputation: 445
TLDR; I created a demo of how to achieve this with the either the Theme or LocalizationProvider.
Details
If you want to do this without a custom component you can pass translations VIA the Theme or LocalizationProvider
component. The documentation isn't all that clear but if you reference the source for PickersLocaleText you'll see labels you want. For the enUS example you can import the enUS locale and then provide your own strings.
From the bottom of the docs
To create your own translation or to customize the English text, copy this file to your project, make any changes needed and import the locale from there.
Upvotes: 6
Reputation: 555
I finally managed to this this by writing a custom ActionBar component
const MyActionBar = ({
onAccept,
onCancel,
onClear,
onSetToday,
}: PickersActionBarProps) => {
return (
<DialogActions>
<Button onClick={onClear}> customCleanText </Button>
<Button onClick={onCancel}> customCancelText </Button>
<Button onClick={onAccept}> customAcceptText </Button>
</DialogActions>
);
};
And overriding the bar component in my MobileDatePicker component
<MobileDatePicker
//...
components: {{
ActionBar: MyActionBar
}}
/>
Upvotes: 7