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

Fix TranslatableInputs should support fullWitdh and sx props #9104

Merged
merged 1 commit into from
Jul 19, 2023
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
55 changes: 55 additions & 0 deletions packages/ra-ui-materialui/src/input/TranslatableInputs.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import * as React from 'react';
import polyglotI18nProvider from 'ra-i18n-polyglot';
import englishMessages from 'ra-language-english';

import { AdminContext } from '../AdminContext';
import { Create } from '../detail';
import { SimpleForm } from '../form';
import { TranslatableInputs } from './TranslatableInputs';
import { FormInspector } from './common';
import { TextInput } from './TextInput';

export default { title: 'ra-ui-materialui/input/TranslatableInputs' };

export const Basic = () => (
<Wrapper>
<TranslatableInputs locales={['en', 'fr']}>
<TextInput source="title" />
<TextInput source="description" />
</TranslatableInputs>
</Wrapper>
);

export const FullWidth = () => (
<Wrapper>
<TranslatableInputs locales={['en', 'fr']} fullWidth>
<TextInput source="title" />
<TextInput source="description" />
</TranslatableInputs>
</Wrapper>
);

export const Sx = () => (
<Wrapper>
<TranslatableInputs
locales={['en', 'fr']}
sx={{ border: 'solid 1px red' }}
>
<TextInput source="title" />
<TextInput source="description" />
</TranslatableInputs>
</Wrapper>
);

const i18nProvider = polyglotI18nProvider(() => englishMessages);

const Wrapper = ({ children }) => (
<AdminContext i18nProvider={i18nProvider}>
<Create resource="posts">
<SimpleForm>
{children}
<FormInspector name="published" />
</SimpleForm>
</Create>
</AdminContext>
);
25 changes: 21 additions & 4 deletions packages/ra-ui-materialui/src/input/TranslatableInputs.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import * as React from 'react';
import { styled } from '@mui/material/styles';
import { SxProps, styled } from '@mui/material/styles';
import { ReactElement, ReactNode } from 'react';
import {
TranslatableContextProvider,
useTranslatable,
UseTranslatableOptions,
} from 'ra-core';
import clsx from 'clsx';
import { TranslatableInputsTabs } from './TranslatableInputsTabs';
import { TranslatableInputsTabContent } from './TranslatableInputsTabContent';

Expand All @@ -29,15 +30,15 @@ import { TranslatableInputsTabContent } from './TranslatableInputsTabContent';
* <TranslatableInputs locales={['en', 'fr']}>
* <TextInput source="title" />
* <RichTextInput source="description" />
* </Translatable>
* </TranslatableInputs>
*
* @example <caption>With a custom language selector</caption>
* <TranslatableInputs
* selector={<MyLanguageSelector />}
* locales={['en', 'fr']}
* >
* <TextInput source="title" />
* </Translatable>
* </TranslatableInputs>
*
* const MyLanguageSelector = () => {
* const {
Expand Down Expand Up @@ -74,11 +75,17 @@ export const TranslatableInputs = (
children,
variant,
margin,
sx,
} = props;
const context = useTranslatable({ defaultLocale, locales });

return (
<Root className={className}>
<Root
className={clsx(className, TranslatableInputsClasses.root, {
[TranslatableInputsClasses.fullWidth]: props.fullWidth,
})}
sx={sx}
>
<TranslatableContextProvider value={context}>
{selector}
{locales.map(locale => (
Expand All @@ -101,18 +108,28 @@ export interface TranslatableInputsProps extends UseTranslatableOptions {
className?: string;
selector?: ReactElement;
children: ReactNode;
fullWidth?: boolean;
groupKey?: string;
margin?: 'none' | 'normal' | 'dense';
variant?: 'standard' | 'outlined' | 'filled';
sx?: SxProps;
}

const PREFIX = 'RaTranslatableInputs';

export const TranslatableInputsClasses = {
root: `${PREFIX}-root`,
fullWidth: `${PREFIX}-fullWidth`,
};
const Root = styled('div', {
name: PREFIX,
overridesResolver: (props, styles) => styles.root,
})(({ theme }) => ({
flexGrow: 1,
marginTop: theme.spacing(1),
marginBottom: theme.spacing(0.5),

[`&.${TranslatableInputsClasses.fullWidth}`]: {
width: '100%',
},
}));