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

Convert ArrayField component to function component. #4536

Merged
merged 22 commits into from
May 7, 2020
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
148 changes: 70 additions & 78 deletions packages/ra-ui-materialui/src/field/ArrayField.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
import { Component, cloneElement, Children } from 'react';
import {
FunctionComponent,
cloneElement,
Children,
useEffect,
useState,
memo,
} from 'react';
import get from 'lodash/get';
import pure from 'recompose/pure';
import { Identifier } from 'ra-core';

import { FieldProps, InjectedFieldProps, fieldPropTypes } from './types';
import PropTypes from 'prop-types';

interface ArrayFieldProps {
fieldKey?: string;
}

interface State {
data: object;
Expand All @@ -15,6 +26,28 @@ const initialState = {
ids: [],
};

const getDataAndIds = (record: object, source: string, fieldKey: string) => {
const list = get(record, source);
if (!list) {
return initialState;
}
return fieldKey
? {
data: list.reduce((prev, item) => {
prev[item[fieldKey]] = item;
return prev;
}, {}),
ids: list.map(item => item[fieldKey]),
}
: {
data: list.reduce((prev, item) => {
prev[JSON.stringify(item)] = item;
return prev;
}, {}),
ids: list.map(JSON.stringify),
};
};

/**
* Display a collection
*
Expand Down Expand Up @@ -88,86 +121,45 @@ const initialState = {
* )
* TagsField.defaultProps = { addLabel: true };
*/
export class ArrayField extends Component<
FieldProps & InjectedFieldProps,
State
> {
constructor(props: FieldProps & InjectedFieldProps) {
super(props);
this.state = props.record
? this.getDataAndIds(props.record, props.source, props.fieldKey)
: initialState;
}
export const ArrayField: FunctionComponent<
ArrayFieldProps & FieldProps & InjectedFieldProps
> = ({
addLabel,
basePath,
children,
record,
sortable,
source,
fieldKey,
WiXSL marked this conversation as resolved.
Show resolved Hide resolved
...rest
}) => {
const [ids, setIds] = useState();
const [data, setData] = useState();

componentWillReceiveProps(
nextProps: FieldProps & InjectedFieldProps,
prevProps: FieldProps & InjectedFieldProps
) {
if (nextProps.record !== prevProps.record) {
this.setState(
this.getDataAndIds(
nextProps.record,
nextProps.source,
nextProps.fieldKey
)
);
}
}
useEffect(() => {
const { ids, data } = getDataAndIds(record, source, fieldKey);
setIds(ids);
setData(data);
}, [record, source, fieldKey]);
WiXSL marked this conversation as resolved.
Show resolved Hide resolved

getDataAndIds(record: object, source: string, fieldKey: string) {
const list = get(record, source);
if (!list) {
return initialState;
}
return fieldKey
? {
data: list.reduce((prev, item) => {
prev[item[fieldKey]] = item;
return prev;
}, {}),
ids: list.map(item => item[fieldKey]),
}
: {
data: list.reduce((prev, item) => {
prev[JSON.stringify(item)] = item;
return prev;
}, {}),
ids: list.map(JSON.stringify),
};
}

render() {
const {
addLabel,
basePath,
children,
record,
sortable,
source,
fieldKey,
...rest
} = this.props;
const { ids, data } = this.state;

// @ts-ignore
return cloneElement(Children.only(children), {
ids,
data,
loading: false,
basePath,
currentSort: {},
...rest,
});
}
}

const EnhancedArrayField = pure<FieldProps>(ArrayField);
// @ts-ignore
WiXSL marked this conversation as resolved.
Show resolved Hide resolved
return cloneElement(Children.only(children), {
ids,
data,
loading: false,
basePath,
currentSort: {},
...rest,
});
};

EnhancedArrayField.defaultProps = {
ArrayField.defaultProps = {
addLabel: true,
};

EnhancedArrayField.propTypes = fieldPropTypes;
EnhancedArrayField.displayName = 'EnhancedArrayField';
ArrayField.propTypes = {
...fieldPropTypes,
fieldKey: PropTypes.string,
};

export default EnhancedArrayField;
export default memo<ArrayFieldProps & FieldProps>(ArrayField);
2 changes: 0 additions & 2 deletions packages/ra-ui-materialui/src/field/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ export interface FieldProps {
headerClassName?: string;
textAlign?: TextAlign;
emptyText?: string;
fieldKey?: string;
}

// Props injected by react-admin
Expand All @@ -33,5 +32,4 @@ export const fieldPropTypes = {
headerClassName: PropTypes.string,
textAlign: PropTypes.oneOf<TextAlign>(['right', 'left']),
emptyText: PropTypes.string,
fieldKey: PropTypes.string,
};
14 changes: 7 additions & 7 deletions packages/ra-ui-materialui/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"outDir": "lib",
"rootDir": "src"
},
"exclude": ["**/*.spec.ts", "**/*.spec.tsx", "**/*.spec.js"],
"include": ["src"]
"extends": "../../tsconfig.json",
"compilerOptions": {
"outDir": "lib",
"rootDir": "src"
},
"exclude": ["**/*.spec.ts", "**/*.spec.tsx", "**/*.spec.js"],
"include": ["src"]
}