Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to set Button default props via theme #9735

Merged
merged 1 commit into from
Mar 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 28 additions & 3 deletions packages/ra-ui-materialui/src/button/Button.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import * as React from 'react';
import { ReactNode } from 'react';
import { createTheme, ThemeProvider, Stack } from '@mui/material';
import type { PaletteColor } from '@mui/material';
import AddIcon from '@mui/icons-material/Add';

import { Button } from './Button';
import { ReactNode } from 'react';
import { defaultTheme } from '../theme/defaultTheme';

export default { title: 'ra-ui-materialui/button/Button' };

export const Basic = () => (
<ThemeProvider theme={theme}>
<ThemeProvider theme={createTheme(defaultTheme)}>
<UIWrapper>
<Button label="default" />
<Button label="outlined" variant="outlined" />
Expand All @@ -24,7 +26,7 @@ export const Basic = () => (
);

export const WithIcon = () => (
<ThemeProvider theme={theme}>
<ThemeProvider theme={createTheme(defaultTheme)}>
<UIWrapper>
<Button label="button">
<AddIcon />
Expand All @@ -39,6 +41,29 @@ export const WithIcon = () => (
</ThemeProvider>
);

export const WithThemeProps = () => (
<ThemeProvider
theme={createTheme({
...defaultTheme,
components: {
// @ts-ignore
RaButton: {
defaultProps: {
size: 'normal',
color: 'secondary',
},
},
},
})}
>
<UIWrapper>
<Button label="button" />
<Button label="button" variant="outlined" />
<Button label="button" variant="contained" />
</UIWrapper>
</ThemeProvider>
);

export const WithUserDefinedPalette = () => (
<ThemeProvider theme={theme}>
<UIWrapper>
Expand Down
6 changes: 4 additions & 2 deletions packages/ra-ui-materialui/src/button/Button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
useMediaQuery,
Theme,
} from '@mui/material';
import { styled } from '@mui/material/styles';
import { styled, useThemeProps } from '@mui/material/styles';
import PropTypes from 'prop-types';
import { useTranslate } from 'ra-core';
import { Path, To } from 'react-router';
Expand All @@ -26,8 +26,9 @@ import { Path, To } from 'react-router';
*
*/
export const Button = <RootComponent extends React.ElementType = 'button'>(
props: ButtonProps<RootComponent>
inProps: ButtonProps<RootComponent>
) => {
const props = useThemeProps({ props: inProps, name: 'RaButton' });
const {
alignIcon = 'left',
children,
Expand All @@ -39,6 +40,7 @@ export const Button = <RootComponent extends React.ElementType = 'button'>(
to: locationDescriptor,
...rest
} = props;

const translate = useTranslate();
const translatedLabel = label ? translate(label, { _: label }) : undefined;
const linkParams = getLinkParams(locationDescriptor);
Expand Down
Loading