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 Field components don't show labels when used in Show views #6564

Merged
merged 4 commits into from
Sep 8, 2021
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
9 changes: 5 additions & 4 deletions packages/ra-ui-materialui/src/field/ArrayField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ const getDataAndIds = (
* );
* TagsField.defaultProps = { addLabel: true };
*/
export const ArrayField = (props: ArrayFieldProps) => {
export const ArrayField = memo<ArrayFieldProps>((props: ArrayFieldProps) => {
const {
addLabel,
basePath,
Expand Down Expand Up @@ -178,12 +178,13 @@ export const ArrayField = (props: ArrayFieldProps) => {
})}
</ListContextProvider>
);
};
});

// @ts-ignore
ArrayField.defaultProps = {
addLabel: true,
};

// @ts-ignore
ArrayField.propTypes = {
...fieldPropTypes,
fieldKey: PropTypes.string,
Expand All @@ -201,4 +202,4 @@ interface State {

ArrayField.displayName = 'ArrayField';

export default memo<ArrayFieldProps>(ArrayField);
export default ArrayField;
10 changes: 6 additions & 4 deletions packages/ra-ui-materialui/src/field/ChipField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const useStyles = makeStyles(
{ name: 'RaChipField' }
);

export const ChipField = (props: ChipFieldProps) => {
export const ChipField = memo<ChipFieldProps>((props: ChipFieldProps) => {
const {
className,
classes: classesOverride,
Expand Down Expand Up @@ -49,13 +49,15 @@ export const ChipField = (props: ChipFieldProps) => {
{...sanitizeFieldRestProps(rest)}
/>
);
};
});

// @ts-ignore
ChipField.defaultProps = {
addLabel: true,
};

// @ts-ignore
ChipField.propTypes = {
// @ts-ignore
...ChipField.propTypes,
...fieldPropTypes,
};
Expand All @@ -65,4 +67,4 @@ export interface ChipFieldProps
InjectedFieldProps,
Omit<ChipProps, 'label'> {}

export default memo<ChipFieldProps>(ChipField);
export default ChipField;
9 changes: 5 additions & 4 deletions packages/ra-ui-materialui/src/field/DateField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ const toLocaleStringSupportsLocales = (() => {
* // renders the record { id: 1234, new Date('2012-11-07') } as
* <span>mercredi 7 novembre 2012</span>
*/
export const DateField = (props: DateFieldProps) => {
export const DateField = memo<DateFieldProps>((props: DateFieldProps) => {
const {
className,
emptyText,
Expand Down Expand Up @@ -89,12 +89,13 @@ export const DateField = (props: DateFieldProps) => {
{dateString}
</Typography>
);
};
});

// @ts-ignore
DateField.defaultProps = {
addLabel: true,
};

// @ts-ignore
DateField.propTypes = {
// @ts-ignore
...Typography.propTypes,
Expand All @@ -116,4 +117,4 @@ export interface DateFieldProps
showTime?: boolean;
}

export default memo<DateFieldProps>(DateField);
export default DateField;
10 changes: 5 additions & 5 deletions packages/ra-ui-materialui/src/field/EmailField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { PublicFieldProps, InjectedFieldProps, fieldPropTypes } from './types';
// useful to prevent click bubbling in a datagrid with rowClick
const stopPropagation = e => e.stopPropagation();

const EmailField = (props: EmailFieldProps) => {
const EmailField = memo<EmailFieldProps>((props: EmailFieldProps) => {
const { className, source, emptyText, ...rest } = props;
const record = useRecordContext(props);
const value = get(record, source);
Expand Down Expand Up @@ -39,17 +39,17 @@ const EmailField = (props: EmailFieldProps) => {
{value}
</Link>
);
};

});
// @ts-ignore
EmailField.defaultProps = {
addLabel: true,
};

// @ts-ignore
EmailField.propTypes = fieldPropTypes;

export interface EmailFieldProps
extends PublicFieldProps,
InjectedFieldProps,
AnchorHTMLAttributes<HTMLAnchorElement> {}

export default memo<EmailFieldProps>(EmailField);
export default EmailField;
73 changes: 35 additions & 38 deletions packages/ra-ui-materialui/src/field/NumberField.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as React from 'react';
import { FC, memo } from 'react';
import { memo } from 'react';
import PropTypes from 'prop-types';
import get from 'lodash/get';
import Typography, { TypographyProps } from '@material-ui/core/Typography';
Expand Down Expand Up @@ -42,58 +42,55 @@ const hasNumberFormat = !!(
* // renders the record { id: 1234, price: 25.99 } as
* <span>25,99 $US</span>
*/
export const NumberField: FC<NumberFieldProps> = memo<NumberFieldProps>(
props => {
const {
className,
emptyText,
source,
locales,
options,
textAlign,
...rest
} = props;
const record = useRecordContext(props);
if (!record) {
return null;
}
const value = get(record, source);
if (value == null) {
return emptyText ? (
<Typography
component="span"
variant="body2"
className={className}
{...sanitizeFieldRestProps(rest)}
>
{emptyText}
</Typography>
) : null;
}

return (
export const NumberField = memo<NumberFieldProps>((props: NumberFieldProps) => {
const {
className,
emptyText,
source,
locales,
options,
textAlign,
...rest
} = props;
const record = useRecordContext(props);
if (!record) {
return null;
}
const value = get(record, source);
if (value == null) {
return emptyText ? (
<Typography
variant="body2"
component="span"
variant="body2"
className={className}
{...sanitizeFieldRestProps(rest)}
>
{hasNumberFormat
? value.toLocaleString(locales, options)
: value}
{emptyText}
</Typography>
);
) : null;
}
);

return (
<Typography
variant="body2"
component="span"
className={className}
{...sanitizeFieldRestProps(rest)}
>
{hasNumberFormat ? value.toLocaleString(locales, options) : value}
</Typography>
);
});

// what? TypeScript loses the displayName if we don't set it explicitly
NumberField.displayName = 'NumberField';

// @ts-ignore
NumberField.defaultProps = {
addLabel: true,
textAlign: 'right',
};

// @ts-ignore
NumberField.propTypes = {
// @ts-ignore
...Typography.propTypes,
Expand Down
11 changes: 6 additions & 5 deletions packages/ra-ui-materialui/src/field/SelectField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ import { PublicFieldProps, InjectedFieldProps, fieldPropTypes } from './types';
*
* **Tip**: <ReferenceField> sets `translateChoice` to false by default.
*/
export const SelectField = (props: SelectFieldProps) => {
export const SelectField = memo<SelectFieldProps>((props: SelectFieldProps) => {
const {
className,
emptyText,
Expand Down Expand Up @@ -113,18 +113,19 @@ export const SelectField = (props: SelectFieldProps) => {
{choiceText}
</Typography>
);
};
});

// @ts-ignore
SelectField.defaultProps = {
optionText: 'name',
optionValue: 'id',
translateChoice: true,
};

// @ts-ignore
SelectField.defaultProps = {
addLabel: true,
};

// @ts-ignore
SelectField.propTypes = {
// @ts-ignore
...Typography.propTypes,
Expand All @@ -147,4 +148,4 @@ export interface SelectFieldProps

SelectField.displayName = 'SelectField';

export default memo<SelectFieldProps>(SelectField);
export default SelectField;
8 changes: 4 additions & 4 deletions packages/ra-ui-materialui/src/field/TextField.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import * as React from 'react';
import { FC, memo, ElementType } from 'react';
import { memo, ElementType } from 'react';
import get from 'lodash/get';
import Typography, { TypographyProps } from '@material-ui/core/Typography';
import { useRecordContext } from 'ra-core';

import sanitizeFieldRestProps from './sanitizeFieldRestProps';
import { PublicFieldProps, InjectedFieldProps, fieldPropTypes } from './types';

const TextField: FC<TextFieldProps> = memo<TextFieldProps>(props => {
const TextField = memo<TextFieldProps>((props: TextFieldProps) => {
const { className, source, emptyText, ...rest } = props;
const record = useRecordContext(props);
const value = get(record, source);
Expand All @@ -28,11 +28,11 @@ const TextField: FC<TextFieldProps> = memo<TextFieldProps>(props => {

// what? TypeScript loses the displayName if we don't set it explicitly
TextField.displayName = 'TextField';

// @ts-ignore
TextField.defaultProps = {
addLabel: true,
};

// @ts-ignore
TextField.propTypes = {
// @ts-ignore
...Typography.propTypes,
Expand Down
9 changes: 5 additions & 4 deletions packages/ra-ui-materialui/src/field/UrlField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { Typography, Link } from '@material-ui/core';
import { useRecordContext } from 'ra-core';
import { PublicFieldProps, InjectedFieldProps, fieldPropTypes } from './types';

const UrlField = (props: UrlFieldProps) => {
const UrlField = memo<UrlFieldProps>((props: UrlFieldProps) => {
const { className, emptyText, source, ...rest } = props;
const record = useRecordContext(props);
const value = get(record, source);
Expand All @@ -33,12 +33,13 @@ const UrlField = (props: UrlFieldProps) => {
{value}
</Link>
);
};
});

// @ts-ignore
UrlField.defaultProps = {
addLabel: true,
};

// @ts-ignore
UrlField.propTypes = fieldPropTypes;
UrlField.displayName = 'UrlField';

Expand All @@ -47,4 +48,4 @@ export interface UrlFieldProps
InjectedFieldProps,
AnchorHTMLAttributes<HTMLAnchorElement> {}

export default memo<UrlFieldProps>(UrlField);
export default UrlField;