ra-core
Version:
Core components of react-admin, a frontend Framework for building admin applications on top of REST services, using ES6, React
103 lines • 5.02 kB
JavaScript
import * as React from 'react';
import { useNavigate } from "../routing/index.js";
import { useResourceContext } from "../core/useResourceContext.js";
import { useRecordContext } from "../controller/record/useRecordContext.js";
import { RecordContextProvider } from "../controller/record/RecordContext.js";
import { useListContext } from "../controller/list/useListContext.js";
import { useFieldValue } from "../util/useFieldValue.js";
import { useEvent } from "../util/useEvent.js";
import { useGetPathForRecordCallback } from "../routing/useGetPathForRecordCallback.js";
import { useDataTableSelectedIdsContext } from "../dataTable/DataTableSelectedIdsContext.js";
import { useDataTableRenderContext, DataTableRenderContext, } from "../dataTable/DataTableRenderContext.js";
import { useDataTableConfigContext } from "../dataTable/DataTableConfigContext.js";
import { useDataTableCallbacksContext } from "../dataTable/DataTableCallbacksContext.js";
import { DataTableBase } from "../dataTable/DataTableBase.js";
const DataTableCol = (props) => {
const renderContext = useDataTableRenderContext();
switch (renderContext) {
case 'header':
return React.createElement(DataTableHeadCell, { ...props });
case 'data':
return React.createElement(DataTableCell, { ...props });
}
};
const DataTableHeadCell = (props) => {
return (React.createElement("th", null, props.label ?? (React.createElement(React.Fragment, null,
props.source?.substring(0, 1).toUpperCase(),
props.source?.substring(1)))));
};
const DataTableCell = (props) => {
const record = useRecordContext();
if (props.render) {
return React.createElement("td", null, props.render(record));
}
if (props.children) {
return React.createElement("td", null, props.children);
}
if (props.field) {
return (React.createElement("td", null, React.createElement(props.field, { source: props.source })));
}
if (props.source) {
return (React.createElement("td", null,
React.createElement(DataTableCellValue, { source: props.source })));
}
};
const DataTableCellValue = (props) => {
const value = useFieldValue(props);
return React.createElement(React.Fragment, null, value?.toString());
};
const DataTableRow = (props) => {
const getPathForRecord = useGetPathForRecordCallback();
const navigate = useNavigate();
const record = useRecordContext(props);
if (!record) {
throw new Error('DataTableRow can only be used within a RecordContext or be passed a record prop');
}
const resource = useResourceContext(props);
if (!resource) {
throw new Error('DataTableRow can only be used within a ResourceContext or be passed a resource prop');
}
const { hasBulkActions = false } = useDataTableConfigContext();
const { handleToggleItem, rowClick } = useDataTableCallbacksContext();
const selectedIds = useDataTableSelectedIdsContext();
const handleClick = useEvent(async (event) => {
event.persist();
const temporaryLink = typeof rowClick === 'function'
? rowClick(record.id, resource, record)
: rowClick;
const link = isPromise(temporaryLink)
? await temporaryLink
: temporaryLink;
const path = await getPathForRecord({
record,
resource,
link,
});
if (path === false || path == null) {
return;
}
navigate(path, {
state: { _scrollToTop: true },
});
});
return (React.createElement("tr", { onClick: handleClick },
hasBulkActions && (React.createElement(DataTableCol, null,
React.createElement("input", { "aria-label": "Select this row", type: "checkbox", checked: selectedIds?.includes(record.id), onChange: event => handleToggleItem(record.id, event) }))),
props.children));
};
const isPromise = (value) => value && typeof value.then === 'function';
export const DataTable = (props) => {
const { data } = useListContext();
return (React.createElement(DataTableBase, { hasBulkActions: false, ...props, empty: null, loading: null },
React.createElement("table", { border: 1, style: { width: '100%', borderCollapse: 'collapse' } },
React.createElement(DataTableRenderContext.Provider, { value: "header" },
React.createElement("thead", null,
React.createElement("tr", null,
props.hasBulkActions ? React.createElement("td", null) : null,
props.children))),
React.createElement(DataTableRenderContext.Provider, { value: "data" },
React.createElement("tbody", null, data?.map(record => (React.createElement(RecordContextProvider, { key: record.id, value: record },
React.createElement(DataTableRow, null, props.children)))))))));
};
DataTable.Col = DataTableCol;
//# sourceMappingURL=DataTable.js.map