Skip to content

Commit

Permalink
Merge pull request #5848 from marmelab/fix-translatable-inputs-display
Browse files Browse the repository at this point in the history
Fix translatable inputs display
  • Loading branch information
fzaninotto committed Feb 2, 2021
2 parents ac6f065 + 6c52afd commit 51f7262
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 20 deletions.
2 changes: 1 addition & 1 deletion packages/ra-core/src/controller/RecordContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export const useRecordContext = <
RecordType extends Record | Omit<Record, 'id'> = Record
>(
props: RecordType
) => {
): RecordType => {
// Can't find a way to specify the RecordType when CreateContext is declared
// @ts-ignore
const context = useContext<RecordType>(RecordContext);
Expand Down
35 changes: 26 additions & 9 deletions packages/ra-ui-materialui/src/form/FormInput.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as React from 'react';
import { FC, HtmlHTMLAttributes, ReactElement } from 'react';
import { HtmlHTMLAttributes, ReactElement } from 'react';
import PropTypes from 'prop-types';
import classnames from 'classnames';
import { makeStyles } from '@material-ui/core/styles';
Expand All @@ -8,7 +8,14 @@ import { Record } from 'ra-core';
import Labeled from '../input/Labeled';
import { ClassesOverride } from '../types';

const sanitizeRestProps = ({ basePath, record, ...rest }) => rest;
const sanitizeRestProps = ({
basePath,
record,
...rest
}: {
basePath?: string;
record?: unknown;
}) => rest;

const useStyles = makeStyles(
theme => ({
Expand All @@ -17,9 +24,12 @@ const useStyles = makeStyles(
{ name: 'RaFormInput' }
);

const FormInput: FC<FormInputProps> = props => {
const FormInput = <RecordType extends Record | Omit<Record, 'id'> = Record>(
props: FormInputProps<RecordType>
) => {
const { input, classes: classesOverride, ...rest } = props;
const classes = useStyles(props);
const { id, ...inputProps } = input ? input.props : { id: undefined };
return input ? (
<div
className={classnames(
Expand All @@ -30,8 +40,8 @@ const FormInput: FC<FormInputProps> = props => {
>
{input.props.addLabel ? (
<Labeled
id={input.props.id || input.props.source}
{...input.props}
id={id || input.props.source}
{...inputProps}
{...sanitizeRestProps(rest)}
>
{React.cloneElement(input, {
Expand Down Expand Up @@ -67,13 +77,20 @@ FormInput.propTypes = {
input: PropTypes.node,
};

export interface FormInputProps extends HtmlHTMLAttributes<HTMLDivElement> {
export interface FormInputProps<
RecordType extends Record | Omit<Record, 'id'> = Record
> extends HtmlHTMLAttributes<HTMLDivElement> {
basePath: string;
classes?: ClassesOverride<typeof useStyles>;
input: ReactElement;
input: ReactElement<{
label?: string;
source?: string;
id?: string;
[key: string]: unknown;
}>;
margin?: 'none' | 'normal' | 'dense';
record: Record;
resource: string;
record?: RecordType;
resource?: string;
variant?: 'standard' | 'outlined' | 'filled';
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,16 @@ import {
ReactElement,
ReactNode,
} from 'react';
import { FormGroupContextProvider, useTranslatableContext } from 'ra-core';
import {
FormGroupContextProvider,
Record,
useRecordContext,
useTranslatableContext,
} from 'ra-core';
import { makeStyles } from '@material-ui/core/styles';
import { ClassesOverride } from '../types';
import { FormInput } from '../form';
import { useResourceContext } from 'ra-core';

/**
* Default container for a group of translatable inputs inside a TranslatableInputs component.
Expand All @@ -17,9 +24,19 @@ import { ClassesOverride } from '../types';
export const TranslatableInputsTabContent = (
props: TranslatableInputsTabContentProps
): ReactElement => {
const { children, groupKey = '', locale, ...other } = props;
const {
basePath,
children,
groupKey = '',
locale,
margin,
variant,
...other
} = props;
const { selectedLocale, getLabel, getSource } = useTranslatableContext();
const classes = useStyles(props);
const record = useRecordContext(props);
const resource = useResourceContext(props);

return (
<FormGroupContextProvider name={`${groupKey}${locale}`}>
Expand All @@ -32,24 +49,38 @@ export const TranslatableInputsTabContent = (
{...other}
>
{Children.map(children, child =>
isValidElement(child)
? cloneElement(child, {
...child.props,
label: getLabel(child.props.source),
source: getSource(child.props.source, locale),
})
: null
isValidElement(child) ? (
<FormInput
basePath={basePath}
input={cloneElement(child, {
...child.props,
label: getLabel(child.props.source),
source: getSource(child.props.source, locale),
})}
record={record}
resource={resource}
variant={child.props.variant || variant}
margin={child.props.margin || margin}
/>
) : null
)}
</div>
</FormGroupContextProvider>
);
};

export type TranslatableInputsTabContentProps = {
export type TranslatableInputsTabContentProps<
RecordType extends Record | Omit<Record, 'id'> = Record
> = {
basePath?: string;
children: ReactNode;
classes?: ClassesOverride<typeof useStyles>;
groupKey?: string;
locale: string;
record?: RecordType;
resource?: string;
margin?: 'none' | 'normal' | 'dense';
variant?: 'standard' | 'outlined' | 'filled';
};

const useStyles = makeStyles(
Expand Down

0 comments on commit 51f7262

Please sign in to comment.