Skip to content

Commit

Permalink
Merge pull request #8689 from marmelab/fix-Labeled-no-label-fullWidth
Browse files Browse the repository at this point in the history
Fix `Labeled` ignores `fullWidth` when `label` is `false`
  • Loading branch information
djhi authored Mar 2, 2023
2 parents 41ad493 + 9ea7c8f commit 8f76cd3
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 21 deletions.
29 changes: 29 additions & 0 deletions packages/ra-ui-materialui/src/Labeled.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as React from 'react';
import { RecordContextProvider, ResourceContext } from 'ra-core';
import { TextField } from './field';
import { Labeled } from './Labeled';
import { Box, Stack } from '@mui/material';

export default { title: 'ra-ui-materialui/detail/Labeled' };

Expand Down Expand Up @@ -71,3 +72,31 @@ export const NoDoubleLabel = () => (
</RecordContextProvider>
</ResourceContext.Provider>
);

export const FullWidth = () => (
<Stack alignItems="flex-start">
<ResourceContext.Provider value="books">
<RecordContextProvider value={record}>
<Labeled label="title" fullWidth>
<Box border="1px solid">
<TextField source="title" />
</Box>
</Labeled>
</RecordContextProvider>
</ResourceContext.Provider>
</Stack>
);

export const FullWidthNoLabel = () => (
<Stack alignItems="flex-start">
<ResourceContext.Provider value="books">
<RecordContextProvider value={record}>
<Labeled label={false} fullWidth>
<Box border="1px solid">
<TextField source="title" />
</Box>
</Labeled>
</RecordContextProvider>
</ResourceContext.Provider>
</Stack>
);
41 changes: 20 additions & 21 deletions packages/ra-ui-materialui/src/Labeled.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,22 @@ export const Labeled = ({
resource,
source,
...rest
}: LabeledProps) =>
label !== false &&
children.props.label !== false &&
typeof children.type !== 'string' &&
// @ts-ignore
children.type?.displayName !== 'Labeled' &&
// @ts-ignore
children.type?.displayName !== 'Labeled' ? (
<Root
// @ts-ignore https://github.com/mui/material-ui/issues/29875
component={component}
className={clsx(className, {
[LabeledClasses.fullWidth]: fullWidth,
})}
{...rest}
>
}: LabeledProps) => (
<Root
// @ts-ignore https://github.com/mui/material-ui/issues/29875
component={component}
className={clsx(className, {
[LabeledClasses.fullWidth]: fullWidth,
})}
{...rest}
>
{label !== false &&
children.props.label !== false &&
typeof children.type !== 'string' &&
// @ts-ignore
children.type?.displayName !== 'Labeled' &&
// @ts-ignore
children.type?.displayName !== 'Labeled' ? (
<Typography color={color} className={LabeledClasses.label}>
<FieldTitle
label={label || children.props.label}
Expand All @@ -55,11 +55,10 @@ export const Labeled = ({
isRequired={isRequired}
/>
</Typography>
{children}
</Root>
) : (
<div className={className}>{children}</div>
);
) : null}
{children}
</Root>
);

Labeled.displayName = 'Labeled';

Expand Down

0 comments on commit 8f76cd3

Please sign in to comment.