import React from 'react';
import { Component } from 'react';
import { SimpleShowLayout } from 'react-admin';
import PropTypes from 'prop-types';
/**
* A container component that shows embedded array elements as a list of input sets
*
* You must define the fields and pass them as children.
*
* @example Display all the items of an order
* // order = {
* // id: 123,
* // items: [
* // { qty: 1, price: 10 },
* // { qty: 3, price: 15 },
* // ],
* // }
*
*
*
*
*
*/
export class EmbeddedField extends Component<{
[key: string]: any;
}> {
static defaultProps;
static propTypes;
render() {
const { resource, children, source, record } = this.props;
const layoutProps = { resource, basePath: '/', record };
return (
{React.Children.map(
children,
(child: React.ReactElement<{ source: string; record: any }>) =>
React.cloneElement(child, {
record,
source: `${source}.${child.props.source}`,
}),
)}
);
}
}
EmbeddedField.propTypes = {
addLabel: PropTypes.bool,
basePath: PropTypes.string,
children: PropTypes.node.isRequired,
data: PropTypes.object,
label: PropTypes.string,
record: PropTypes.object,
resource: PropTypes.string,
source: PropTypes.string.isRequired,
};
EmbeddedField.defaultProps = {
addLabel: true,
};
export default EmbeddedField;