Skip to content

Commit

Permalink
Merge pull request #6585 from marmelab/TS-Field-components-memo-FC
Browse files Browse the repository at this point in the history
[TypeScript] Fix Field components types doesn't expose defaultProps & propTypes
  • Loading branch information
djhi authored Sep 14, 2021
2 parents cdc6928 + 679e520 commit 1cb9a8d
Show file tree
Hide file tree
Showing 9 changed files with 28 additions and 36 deletions.
6 changes: 3 additions & 3 deletions packages/ra-ui-materialui/src/field/ArrayField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
useEffect,
useState,
memo,
FC,
ReactElement,
} from 'react';
import get from 'lodash/get';
Expand Down Expand Up @@ -117,7 +118,7 @@ const getDataAndIds = (
* );
* TagsField.defaultProps = { addLabel: true };
*/
export const ArrayField = memo<ArrayFieldProps>((props: ArrayFieldProps) => {
export const ArrayField: FC<ArrayFieldProps> = memo(props => {
const {
addLabel,
basePath,
Expand Down Expand Up @@ -180,11 +181,10 @@ export const ArrayField = memo<ArrayFieldProps>((props: ArrayFieldProps) => {
);
});

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

ArrayField.propTypes = {
...fieldPropTypes,
fieldKey: PropTypes.string,
Expand Down
6 changes: 2 additions & 4 deletions packages/ra-ui-materialui/src/field/BooleanField.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as React from 'react';
import { memo } from 'react';
import { memo, FC } from 'react';
import { SvgIconComponent } from '@material-ui/icons';
import PropTypes from 'prop-types';
import get from 'lodash/get';
Expand All @@ -25,7 +25,7 @@ const useStyles = makeStyles(
}
);

const BooleanField = memo<BooleanFieldProps>((props: BooleanFieldProps) => {
const BooleanField: FC<BooleanFieldProps> = memo(props => {
const {
className,
classes: classesOverride,
Expand Down Expand Up @@ -84,15 +84,13 @@ const BooleanField = memo<BooleanFieldProps>((props: BooleanFieldProps) => {
);
});

// @ts-ignore
BooleanField.defaultProps = {
addLabel: true,
TrueIcon: DoneIcon,
FalseIcon: ClearIcon,
looseValue: false,
};

// @ts-ignore
BooleanField.propTypes = {
// @ts-ignore
...Typography.propTypes,
Expand Down
7 changes: 3 additions & 4 deletions packages/ra-ui-materialui/src/field/ChipField.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as React from 'react';
import { memo } from 'react';
import { memo, FC } from 'react';
import get from 'lodash/get';
import Chip, { ChipProps } from '@material-ui/core/Chip';
import Typography from '@material-ui/core/Typography';
Expand All @@ -17,7 +17,7 @@ const useStyles = makeStyles(
{ name: 'RaChipField' }
);

export const ChipField = memo<ChipFieldProps>((props: ChipFieldProps) => {
export const ChipField: FC<ChipFieldProps> = memo(props => {
const {
className,
classes: classesOverride,
Expand Down Expand Up @@ -51,11 +51,10 @@ export const ChipField = memo<ChipFieldProps>((props: ChipFieldProps) => {
);
});

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

ChipField.propTypes = {
// @ts-ignore
...ChipField.propTypes,
Expand Down
7 changes: 3 additions & 4 deletions packages/ra-ui-materialui/src/field/DateField.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as React from 'react';
import { memo } from 'react';
import { memo, FC } 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,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 = memo<DateFieldProps>((props: DateFieldProps) => {
export const DateField: FC<DateFieldProps> = memo(props => {
const {
className,
emptyText,
Expand Down Expand Up @@ -91,11 +91,10 @@ export const DateField = memo<DateFieldProps>((props: DateFieldProps) => {
);
});

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

DateField.propTypes = {
// @ts-ignore
...Typography.propTypes,
Expand Down
8 changes: 4 additions & 4 deletions packages/ra-ui-materialui/src/field/EmailField.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as React from 'react';
import { AnchorHTMLAttributes, memo } from 'react';
import { AnchorHTMLAttributes, memo, FC } from 'react';
import get from 'lodash/get';
import Typography from '@material-ui/core/Typography';
import { Link } from '@material-ui/core';
Expand All @@ -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 = memo<EmailFieldProps>((props: EmailFieldProps) => {
const EmailField: FC<EmailFieldProps> = memo(props => {
const { className, source, emptyText, ...rest } = props;
const record = useRecordContext(props);
const value = get(record, source);
Expand Down Expand Up @@ -41,11 +41,11 @@ const EmailField = memo<EmailFieldProps>((props: EmailFieldProps) => {
</Link>
);
});
// @ts-ignore

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

EmailField.propTypes = fieldPropTypes;

export interface EmailFieldProps
Expand Down
6 changes: 2 additions & 4 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 { memo } from 'react';
import { memo, FC } 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,7 +42,7 @@ const hasNumberFormat = !!(
* // renders the record { id: 1234, price: 25.99 } as
* <span>25,99 $US</span>
*/
export const NumberField = memo<NumberFieldProps>((props: NumberFieldProps) => {
export const NumberField: FC<NumberFieldProps> = memo(props => {
const {
className,
emptyText,
Expand Down Expand Up @@ -85,12 +85,10 @@ export const NumberField = memo<NumberFieldProps>((props: NumberFieldProps) => {
// 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
9 changes: 4 additions & 5 deletions packages/ra-ui-materialui/src/field/SelectField.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as React from 'react';
import { memo } from 'react';
import { memo, FC } from 'react';
import PropTypes from 'prop-types';
import get from 'lodash/get';
import { ChoicesProps, useChoices, useRecordContext } from 'ra-core';
Expand Down Expand Up @@ -67,7 +67,7 @@ import { PublicFieldProps, InjectedFieldProps, fieldPropTypes } from './types';
*
* **Tip**: <ReferenceField> sets `translateChoice` to false by default.
*/
export const SelectField = memo<SelectFieldProps>((props: SelectFieldProps) => {
export const SelectField: FC<SelectFieldProps> = memo(props => {
const {
className,
emptyText,
Expand Down Expand Up @@ -115,17 +115,16 @@ export const SelectField = memo<SelectFieldProps>((props: SelectFieldProps) => {
);
});

// @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 Down
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 { memo, ElementType } from 'react';
import { memo, FC, 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 = memo<TextFieldProps>((props: TextFieldProps) => {
const TextField: FC<TextFieldProps> = memo(props => {
const { className, source, emptyText, ...rest } = props;
const record = useRecordContext(props);
const value = get(record, source);
Expand All @@ -28,11 +28,11 @@ const TextField = memo<TextFieldProps>((props: TextFieldProps) => {

// 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
7 changes: 3 additions & 4 deletions packages/ra-ui-materialui/src/field/UrlField.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import * as React from 'react';
import { AnchorHTMLAttributes, memo } from 'react';
import { AnchorHTMLAttributes, memo, FC } from 'react';
import get from 'lodash/get';
import sanitizeFieldRestProps from './sanitizeFieldRestProps';
import { Typography, Link } from '@material-ui/core';
import { useRecordContext } from 'ra-core';
import { PublicFieldProps, InjectedFieldProps, fieldPropTypes } from './types';

const UrlField = memo<UrlFieldProps>((props: UrlFieldProps) => {
const UrlField: FC<UrlFieldProps> = memo(props => {
const { className, emptyText, source, ...rest } = props;
const record = useRecordContext(props);
const value = get(record, source);
Expand Down Expand Up @@ -36,11 +36,10 @@ const UrlField = memo<UrlFieldProps>((props: UrlFieldProps) => {
);
});

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

UrlField.propTypes = fieldPropTypes;
UrlField.displayName = 'UrlField';

Expand Down

0 comments on commit 1cb9a8d

Please sign in to comment.