();
+ const [pagination, setPagination] = usePagination();
+ const { selectedRowKeys, setSelectedRowKeys } = useSelectedRowKeys();
+ console.log('props.useSelectedRowKeys', selectedRowKeys);
+ const [, refresh] = useState(uid());
+ const { resource } = useResource();
+ const { sortableField } = useCollectionContext();
+ const dragSort = props.dragSort;
+ const collectionFields = useCollectionFields(schema);
+ // console.log({ collectionFields, pagination });
+ const getDefaultParams = () => {
+ const defaultParams = { ...pagination };
+ if (dragSort) {
+ defaultParams['sort'] = [sortableField || 'sort'];
+ } else {
+ defaultParams['sort'] = (props.defaultSort || []).join(',');
+ }
+ defaultParams['defaultAppends'] = [
+ ...(props.defaultAppends || []),
+ ...collectionFields,
+ ];
+ if (props.defaultFilter) {
+ defaultParams['defaultFilter'] = props.defaultFilter;
+ }
+ console.log({ defaultParams });
+ return defaultParams;
+ };
+ const service = useRequest(
+ (params?: any) => {
+ if (!resource) {
+ return Promise.resolve({
+ list: field.value,
+ total: field?.value?.length,
+ });
+ }
+ return resource.list(params).then((res) => {
+ return {
+ list: res?.data || [],
+ total: res?.meta?.count || res?.data?.length,
+ };
+ });
+ },
+ {
+ onSuccess(data: any) {
+ field.setValue(data?.list || []);
+ },
+ manual: true,
+ // defaultParams: [getDefaultParams()],
+ },
+ );
+ useDeepCompareEffectNoCheck(() => {
+ service.run(getDefaultParams());
+ }, [
+ pagination?.pageSize,
+ pagination?.page,
+ props.dragSort,
+ props.defaultSort,
+ props.defaultFilter,
+ ]);
+ return (
+ {
+ const { page = 1, pageSize } = pagination;
+ const total = props.clientSidePagination
+ ? field?.value?.length
+ : service?.data?.total;
+ const maxPage = Math.ceil(total / pageSize);
+ if (page > maxPage) {
+ setPagination({ page: maxPage });
+ } else {
+ refresh(uid());
+ }
+ },
+ selectedRowKeys,
+ setSelectedRowKeys,
+ pagination,
+ setPagination,
+ service,
+ field,
+ schema,
+ props: { ...others, rowKey, dataRequest },
+ }}
+ >
+
+
+ );
+};
export const Table: any = observer((props: any) => {
const [visible, setVisible] = useState(false);
@@ -38,24 +957,1703 @@ export const Table: any = observer((props: any) => {
);
});
-Table.Pagination = TablePagination;
-Table.ActionBar = TableActionBar;
-Table.Filter = TableFilter;
-Table.Filter.DesignableBar = TableFilterDesignableBar;
-Table.ExportActionDesignableBar = TableExportActionDesignableBar;
-Table.Operation = TableOperation;
-Table.Operation.Cell = TableOperationCell;
-Table.Operation.DesignableBar = TableOperationDesignableBar;
-Table.Action = () => null;
-Table.Action.DesignableBar = TableActionDesignableBar;
-Table.Cell = TableCell;
-Table.Column = TableColumn;
-Table.SortHandle = TableSortHandle;
-Table.DesignableBar = TableDesignableBar;
-Table.SimpleDesignableBar = SimpleDesignableBar;
+const useTotal = () => {
+ const {
+ field,
+ service,
+ props: { clientSidePagination },
+ } = useTable();
+ return clientSidePagination ? field?.value?.length : service?.data?.total;
+};
+
+Table.Pagination = observer(() => {
+ const { service, pagination, setPagination, props } = useTable();
+ if (!pagination || Object.keys(pagination).length === 0) {
+ return null;
+ }
+ const { clientSidePagination } = props;
+ const total = useTotal();
+ const { page = 1 } = pagination;
+ return (
+
+
{
+ const page = pagination.pageSize !== pageSize ? 1 : current;
+ setPagination({
+ page,
+ pageSize,
+ });
+ // if (clientSidePagination) {
+ // return;
+ // }
+ // service.run({
+ // ...service.params[0],
+ // page,
+ // pageSize,
+ // });
+ }}
+ />
+
+ );
+});
+
+function generateActionSchema(type) {
+ const actions: { [key: string]: ISchema } = {
+ filter: {
+ key: uid(),
+ name: uid(),
+ type: 'void',
+ title: "{{ t('Filter') }}",
+ 'x-align': 'left',
+ 'x-decorator': 'AddNew.Displayed',
+ 'x-decorator-props': {
+ displayName: 'filter',
+ },
+ 'x-component': 'Table.Filter',
+ 'x-designable-bar': 'Table.Filter.DesignableBar',
+ 'x-component-props': {
+ fieldNames: [],
+ },
+ },
+ export: {
+ key: uid(),
+ type: 'void',
+ name: uid(),
+ title: "{{ t('Export') }}",
+ 'x-align': 'right',
+ 'x-decorator': 'AddNew.Displayed',
+ 'x-decorator-props': {
+ displayName: 'export',
+ },
+ 'x-action-type': 'export',
+ 'x-component': 'Action',
+ 'x-designable-bar': 'Table.ExportActionDesignableBar',
+ 'x-component-props': {
+ fieldNames: [],
+ icon: 'ExportOutlined',
+ useAction: '{{ Table.useTableExportAction }}',
+ },
+ },
+ create: {
+ key: uid(),
+ type: 'void',
+ name: uid(),
+ title: "{{ t('Add new') }}",
+ 'x-align': 'right',
+ 'x-decorator': 'AddNew.Displayed',
+ 'x-decorator-props': {
+ displayName: 'create',
+ },
+ 'x-component': 'Action',
+ 'x-component-props': {
+ type: 'primary',
+ icon: 'PlusOutlined',
+ },
+ 'x-designable-bar': 'Table.Action.DesignableBar',
+ properties: {
+ modal: {
+ type: 'void',
+ title: "{{ t('Add record') }}",
+ 'x-decorator': 'Form',
+ 'x-component': 'Action.Drawer',
+ 'x-component-props': {
+ useOkAction: '{{ Table.useTableCreateAction }}',
+ },
+ properties: {
+ [uid()]: {
+ type: 'void',
+ 'x-component': 'Grid',
+ 'x-component-props': {
+ addNewComponent: 'AddNew.FormItem',
+ },
+ },
+ },
+ },
+ },
+ },
+ destroy: {
+ key: uid(),
+ type: 'void',
+ name: uid(),
+ title: "{{ t('Delete') }}",
+ 'x-align': 'right',
+ 'x-decorator': 'AddNew.Displayed',
+ 'x-decorator-props': {
+ displayName: 'destroy',
+ },
+ 'x-action-type': 'destroy',
+ 'x-component': 'Action',
+ 'x-designable-bar': 'Table.Action.DesignableBar',
+ 'x-component-props': {
+ confirm: {
+ title: "{{ t('Delete record') }}",
+ content: "{{ t('Are you sure you want to delete it?') }}",
+ },
+ useAction: '{{ Table.useTableDestroyAction }}',
+ },
+ },
+ view: {},
+ update: {},
+ };
+ return actions[type];
+}
+
+function generateMenuActionSchema(type) {
+ const actions: { [key: string]: ISchema } = {
+ view: {
+ key: uid(),
+ name: uid(),
+ type: 'void',
+ title: "{{ t('View') }}",
+ 'x-component': 'Action',
+ 'x-component-props': {
+ type: 'link',
+ },
+ 'x-designable-bar': 'Table.Action.DesignableBar',
+ 'x-action-type': 'view',
+ properties: {
+ [uid()]: {
+ type: 'void',
+ title: "{{ t('View record') }}",
+ 'x-component': 'Action.Drawer',
+ 'x-component-props': {
+ bodyStyle: {
+ background: '#f0f2f5',
+ // paddingTop: 0,
+ },
+ },
+ properties: {
+ [uid()]: {
+ type: 'void',
+ 'x-component': 'Tabs',
+ 'x-designable-bar': 'Tabs.DesignableBar',
+ properties: {
+ [uid()]: {
+ type: 'void',
+ title: "{{ t('Details') }}",
+ 'x-designable-bar': 'Tabs.TabPane.DesignableBar',
+ 'x-component': 'Tabs.TabPane',
+ 'x-component-props': {},
+ properties: {
+ [uid()]: {
+ type: 'void',
+ 'x-component': 'Grid',
+ 'x-component-props': {
+ addNewComponent: 'AddNew.PaneItem',
+ },
+ },
+ },
+ },
+ },
+ },
+ },
+ },
+ },
+ },
+ update: {
+ key: uid(),
+ name: uid(),
+ type: 'void',
+ title: "{{ t('Edit') }}",
+ 'x-component': 'Action',
+ 'x-component-props': {
+ type: 'link',
+ },
+ 'x-designable-bar': 'Table.Action.DesignableBar',
+ 'x-action-type': 'update',
+ properties: {
+ [uid()]: {
+ type: 'void',
+ title: "{{ t('Edit record') }}",
+ 'x-decorator': 'Form',
+ 'x-decorator-props': {
+ useResource: '{{ Table.useResource }}',
+ useValues: '{{ Table.useTableRowRecord }}',
+ },
+ 'x-component': 'Action.Drawer',
+ 'x-component-props': {
+ useOkAction: '{{ Table.useTableUpdateAction }}',
+ },
+ properties: {
+ [uid()]: {
+ type: 'void',
+ 'x-component': 'Grid',
+ 'x-component-props': {
+ addNewComponent: 'AddNew.FormItem',
+ },
+ },
+ },
+ },
+ },
+ },
+ destroy: {
+ key: uid(),
+ name: uid(),
+ type: 'void',
+ title: "{{ t('Delete') }}",
+ 'x-component': 'Action',
+ 'x-designable-bar': 'Table.Action.DesignableBar',
+ 'x-action-type': 'destroy',
+ 'x-component-props': {
+ useAction: '{{ Table.useTableDestroyAction }}',
+ type: 'link',
+ confirm: {
+ title: "{{ t('Delete record') }}",
+ content: "{{ t('Are you sure you want to delete it?') }}",
+ },
+ },
+ },
+ };
+ return actions[type];
+}
+
+function AddActionButton() {
+ const { t } = useTranslation();
+ const [visible, setVisible] = useState(false);
+ const displayed = useDisplayedMapContext();
+ const { appendChild, remove } = useDesignable();
+ const { schema, designable } = useDesignable();
+ const { createSchema, removeSchema, updateSchema } = useClient();
+
+ if (!designable || !schema['x-designable-bar']) {
+ return null;
+ }
+ return (
+
+
+ {[
+ { title: t('Filter'), name: 'filter' },
+ { title: t('Export'), name: 'export' },
+ { title: t('Add new'), name: 'create' },
+ { title: t('Delete'), name: 'destroy' },
+ ].map((item) => (
+ {
+ if (!checked) {
+ const s = displayed.get(item.name) as Schema;
+ const path = getSchemaPath(s);
+ displayed.remove(item.name);
+ const removed = remove(path);
+ await removeSchema(removed);
+ } else {
+ const s = generateActionSchema(item.name);
+ const data = appendChild(s);
+ await createSchema(data);
+ }
+ }}
+ />
+ ))}
+
+
+
+ {t('Function')}
+ {t('Popup form')}
+ {t('Flexible popup')}
+
+
+ }
+ >
+ }
+ >
+ {t('Configure actions')}
+
+
+ );
+}
+
+function Actions(props: any) {
+ const { align = 'left' } = props;
+ const { schema, designable } = useDesignable();
+ return (
+
+
+ {schema.mapProperties((s) => {
+ const currentAlign = s['x-align'] || 'left';
+ if (currentAlign !== align) {
+ return null;
+ }
+ return (
+
+
+
+ );
+ })}
+
+
+ );
+}
+
+Table.ActionBar = observer((props: any) => {
+ const { align = 'top' } = props;
+ // const { schema, designable } = useDesignable();
+ const { root, schema, insertAfter, remove, appendChild } = useDesignable();
+ const moveToAfter = (path1, path2, extra = {}) => {
+ if (!path1 || !path2) {
+ return;
+ }
+ if (path1.join('.') === path2.join('.')) {
+ return;
+ }
+ const data = findPropertyByPath(root, path1);
+ if (!data) {
+ return;
+ }
+ remove(path1);
+ return insertAfter(
+ {
+ ...data.toJSON(),
+ ...extra,
+ },
+ path2,
+ );
+ };
+ const { createSchema, removeSchema, updateSchema } = useClient();
+
+ const [dragOverlayContent, setDragOverlayContent] = useState('');
+ return (
+ {
+ setDragOverlayContent(event.active.data?.current?.title || '');
+ // const previewRef = event.active.data?.current?.previewRef;
+ // if (previewRef) {
+ // setDragOverlayContent(previewRef?.current?.innerHTML);
+ // } else {
+ // setDragOverlayContent('');
+ // }
+ }}
+ onDragEnd={async (event) => {
+ const path1 = event.active?.data?.current?.path;
+ const path2 = event.over?.data?.current?.path;
+ const align = event.over?.data?.current?.align;
+ const draggable = event.over?.data?.current?.draggable;
+ if (!path1 || !path2) {
+ return;
+ }
+ if (path1.join('.') === path2.join('.')) {
+ return;
+ }
+ if (!draggable) {
+ console.log('alignalignalignalign', align);
+ const p = findPropertyByPath(root, path1);
+ if (!p) {
+ return;
+ }
+ remove(path1);
+ const data = appendChild(
+ {
+ ...p.toJSON(),
+ 'x-align': align,
+ },
+ path2,
+ );
+ await updateSchema(data);
+ } else {
+ const data = moveToAfter(path1, path2, {
+ 'x-align': align,
+ });
+ await updateSchema(data);
+ }
+ }}
+ >
+
+ {dragOverlayContent}
+ {/* */}
+
+
+
+
+
+ );
+});
+
+const fieldsToFilterColumns = (fields: any[], options: any = {}) => {
+ const { fieldNames = [] } = options;
+ const properties = {};
+ fields.forEach((field, index) => {
+ if (fieldNames?.length && !fieldNames.includes(field.name)) {
+ return;
+ }
+ const fieldOption = interfaces.get(field.interface);
+ if (!fieldOption?.operations) {
+ return;
+ }
+ properties[`column${index}`] = {
+ type: 'void',
+ title: field?.uiSchema?.title,
+ 'x-component': 'Filter.Column',
+ 'x-component-props': {
+ operations: fieldOption.operations,
+ },
+ properties: {
+ [field.name]: {
+ ...field.uiSchema,
+ 'x-decorator': 'FormilyFormItem',
+ title: null,
+ },
+ },
+ };
+ });
+ return properties;
+};
+
+const fieldsToSortColumns = (fields: any[]) => {
+ const dataSource = [];
+
+ fields.forEach((field) => {
+ const fieldOption = interfaces.get(field.interface);
+ if (!fieldOption?.sortable) {
+ return;
+ }
+ dataSource.push({
+ value: field.name,
+ label: field?.uiSchema?.title,
+ });
+ });
+
+ return dataSource;
+};
+
+Table.Filter = observer((props: any) => {
+ const { service } = useTable();
+ const { fieldNames = [] } = props;
+ const compile = useCompile();
+ const { t } = useTranslation();
+ const { schema, DesignableBar } = useDesignable();
+ const form = useMemo(() => createForm(), []);
+ const { fields = [] } = useCollectionContext();
+ const [visible, setVisible] = useState(false);
+ const obj = flatten(form.values.filter || {});
+ console.log('flatten', obj, Object.values(obj));
+ const count = Object.values(obj).filter((i) =>
+ Array.isArray(i) ? i.length : i,
+ ).length;
+ const icon = props.icon || 'FilterOutlined';
+ const properties = fieldsToFilterColumns(fields, { fieldNames });
+ schema.mapProperties((p) => {
+ properties[p.name] = p;
+ });
+ return (
+
+
+
+
+
+ {
+ const { filter } = form.values;
+ console.log('Table.Filter', form.values);
+ setVisible(false);
+ return service.run({
+ ...service.params[0],
+ filter,
+ });
+ }}
+ >
+ {t('Submit')}
+
+
+
+
+ }
+ >
+ }>
+ {count > 0 ? t('{{count}} filter items', { count }) : compile(schema.title)}
+
+
+
+ );
+});
+
+Table.Filter.DesignableBar = () => {
+ const { t } = useTranslation();
+ const { schema, remove, refresh, insertAfter } = useDesignable();
+ const [visible, setVisible] = useState(false);
+ const displayed = useDisplayedMapContext();
+ const { fields } = useCollectionContext();
+ const field = useField();
+ const { createSchema, removeSchema, updateSchema } = useClient();
+ let fieldNames = field.componentProps.fieldNames || [];
+ if (fieldNames.length === 0) {
+ fieldNames = fields.map((field) => field.name);
+ }
+ return (
+
+ {
+ e.stopPropagation();
+ }}
+ className={cls('designable-bar-actions', { active: visible })}
+ >
+
+
+ {
+ setVisible(visible);
+ }}
+ overlay={
+
+ }
+ >
+
+
+
+
+
+ );
+};
+
+Table.ExportActionDesignableBar = () => {
+ const { t } = useTranslation();
+ const { schema, remove, refresh, insertAfter } = useDesignable();
+ const [visible, setVisible] = useState(false);
+ const displayed = useDisplayedMapContext();
+ const { fields } = useCollectionContext();
+ const field = useField();
+ const { createSchema, removeSchema, updateSchema } = useClient();
+ let fieldNames = field.componentProps.fieldNames || [];
+ if (fieldNames.length === 0) {
+ fieldNames = fields.map((field) => field.name);
+ }
+ return (
+
+ {
+ e.stopPropagation();
+ }}
+ className={cls('designable-bar-actions', { active: visible })}
+ >
+
+
+ {
+ setVisible(visible);
+ }}
+ overlay={
+
+ }
+ >
+
+
+
+
+
+ );
+};
+
+Table.Operation = observer((props: any) => {
+ const { designable, schema } = useDesignable();
+ const { t } = useTranslation();
+ return (
+
+ );
+});
+
+Table.Operation.Cell = observer((props: any) => {
+ const ctx = useContext(TableRowContext);
+ const schema = props.schema;
+ return (
+
+
+
+ );
+});
+
+Table.Operation.DesignableBar = () => {
+ const { t } = useTranslation();
+ const { schema: columnSchema } = useDesignable();
+ const groupSchema = Object.values(columnSchema.properties || {}).shift();
+ const groupPath = getSchemaPath(groupSchema);
+ const { schema, remove, refresh, appendChild } = useDesignable(groupPath);
+ const [visible, setVisible] = useState(false);
+ const { createSchema, removeSchema, updateSchema } = useClient();
+
+ const map = new Map();
+ schema.mapProperties((s) => {
+ if (!s['x-action-type']) {
+ return;
+ }
+ map.set(s['x-action-type'], s.name);
+ });
+ const path = getSchemaPath(schema);
+ return (
+
+ {
+ e.stopPropagation();
+ }}
+ className={cls('designable-bar-actions', { active: visible })}
+ >
+
+
+ {
+ setVisible(visible);
+ }}
+ overlay={
+
+ }
+ >
+
+
+
+
+
+ );
+};
+
+Table.Action = () => null;
+
+Table.Action.DesignableBar = () => {
+ const { t } = useTranslation();
+ const { schema, remove, refresh, insertAfter } = useDesignable();
+ const [visible, setVisible] = useState(false);
+ const isPopup = Object.keys(schema.properties || {}).length > 0;
+ const popupSchema = Object.values(schema.properties || {}).shift();
+ const inActionBar = schema.parent['x-component'] === 'Table.ActionBar';
+ const displayed = useDisplayedMapContext();
+ const field = useField();
+ const { createSchema, removeSchema, updateSchema } = useClient();
+ const popupComponent = popupSchema?.['x-component'] || 'Action.Drawer';
+ return (
+
+ {
+ e.stopPropagation();
+ }}
+ className={cls('designable-bar-actions', { active: visible })}
+ >
+
+
+ {
+ setVisible(visible);
+ }}
+ overlay={
+
+ }
+ >
+
+
+
+
+
+ );
+};
+
+Table.Cell = observer((props: any) => {
+ const ctx = useContext(TableRowContext);
+ const schema = props.schema;
+ const collectionField = useContext(CollectionFieldContext);
+ if (schema['x-component'] === 'Table.Operation') {
+ return ;
+ }
+ let uiSchema = collectionField?.uiSchema as Schema;
+ if (uiSchema?.['x-component'] === 'Upload.Attachment') {
+ uiSchema = cloneDeepWith(uiSchema);
+ set(uiSchema, 'x-component-props.size', 'small');
+ }
+ const componentProps = merge(
+ uiSchema?.['x-component-props'] || {},
+ schema?.['x-component-props'] || {},
+ {
+ arrayMerge: (t, s) => s,
+ },
+ );
+ console.log('Table.Cell', collectionField?.interface, componentProps);
+ return (
+
+
+
+ );
+});
+
+Table.Column = observer((props: any) => {
+ const collectionField = useContext(CollectionFieldContext);
+ const { schema, DesignableBar } = useDesignable();
+ const compile = useCompile();
+ const displayed = useDisplayedMapContext();
+ useEffect(() => {
+ if (collectionField?.name) {
+ displayed.set(collectionField.name, schema);
+ }
+ }, [collectionField, schema]);
+ return (
+
+ {compile(schema.title || collectionField?.uiSchema?.title)}
+
+
+ );
+});
+
+Table.Column.DesignableBar = () => {
+ const field = useField();
+ const { t } = useTranslation();
+ const compile = useCompile();
+ const { service, refresh: refreshTable } = useTable();
+ // const fieldSchema = useFieldSchema();
+ const { schema, remove, refresh, insertAfter } = useDesignable();
+ const [visible, setVisible] = useState(false);
+ const displayed = useDisplayedMapContext();
+ const { getFieldsByCollection } = useCollectionsContext();
+ const collectionField = useContext(CollectionFieldContext);
+ const { createSchema, removeSchema, updateSchema } = useClient();
+ console.log('displayed.map', displayed.map);
+ return (
+
+
{
+ e.stopPropagation();
+ }}
+ className={cls('designable-bar-actions', { active: visible })}
+ >
+
+
+ {
+ setVisible(visible);
+ }}
+ overlay={
+
+ }
+ >
+
+
+
+
+
+ );
+};
+
+Table.Index = observer(() => {
+ const index = useTableIndex();
+ return {index + 1};
+});
+
+Table.SortHandle = observer((props: any) => {
+ return ;
+});
+
+Table.DesignableBar = observer((props) => {
+ const { t } = useTranslation();
+ const compile = useCompile();
+ const field = useField();
+ const { schema, refresh, deepRemove } = useDesignable();
+ const [visible, setVisible] = useState(false);
+ const { dragRef } = useContext(DraggableBlockContext);
+ const defaultPageSize =
+ field?.componentProps?.pagination?.defaultPageSize || 10;
+ const collectionName = field?.componentProps?.collectionName;
+ const { collection, fields } = useCollection({ collectionName });
+ const { createSchema, removeSchema, updateSchema } = useClient();
+ console.log({ collectionName });
+ return (
+
+
+ {compile(collection?.title || collection?.name)}
+
+
{
+ e.stopPropagation();
+ }}
+ className={cls('designable-bar-actions', { active: visible })}
+ >
+
+
+
+ {
+ setVisible(visible);
+ }}
+ overlay={
+
+ }
+ >
+
+
+
+
+
+ );
+});
+
+Table.useResource = ({ onSuccess, manual = true }) => {
+ const { props } = useTable();
+ const { collection } = useCollectionContext();
+ const ctx = useContext(TableRowContext);
+ const resource = useResourceRequest({
+ resourceName: collection?.name || props.collectionName,
+ resourceKey: ctx.record[props.rowKey],
+ });
+ const { schema } = useDesignable();
+ const fieldFields = (schema: Schema) => {
+ const names = [];
+ schema.reduceProperties((buf, current) => {
+ if (current['x-component'] === 'Form.Field') {
+ const fieldName = current['x-component-props']?.['fieldName'];
+ if (fieldName) {
+ buf.push(fieldName);
+ }
+ } else {
+ const fieldNames = fieldFields(current);
+ buf.push(...fieldNames);
+ }
+ return buf;
+ }, names);
+ return names;
+ };
+ console.log(
+ 'collection?.name || props.collectionName',
+ collection?.name || props.collectionName,
+ // fieldFields(schema),
+ );
+ const service = useRequest(
+ (params?: any) => {
+ console.log('Table.useResource', params);
+ return resource.get({ ...params, appends: fieldFields(schema) });
+ },
+ {
+ formatResult: (result) => result?.data,
+ onSuccess,
+ manual,
+ },
+ );
+ return { resource, service, initialValues: service.data, ...service };
+};
+
+Table.useActionLogDetailsResource = ({ onSuccess }) => {
+ const { props } = useTable();
+ const { collection } = useCollectionContext();
+ const ctx = useContext(TableRowContext);
+ const resource = useResourceRequest({
+ resourceName: 'action_logs',
+ resourceKey: ctx.record[props.rowKey],
+ });
+ const service = useRequest(
+ (params?: any) => {
+ return resource.get({
+ ...params,
+ appends: ['changes', 'user', 'collection'],
+ });
+ },
+ {
+ formatResult: (result) => result?.data,
+ onSuccess,
+ manual: true,
+ },
+ );
+ const [visible] = useContext(VisibleContext);
+
+ useEffect(() => {
+ if (visible) {
+ service.run({});
+ }
+ }, [visible]);
+
+ return { resource, service, initialValues: service.data, ...service };
+};
+
+const useActionLogsResource = (options: any = {}) => {
+ const { props } = useTable();
+ const ctx = useContext(TableRowContext);
+
+ class ActionLogoResource extends Resource {
+ list(options?: ListOptions) {
+ console.log({ options });
+ let defaultFilter = options?.defaultFilter;
+ if (ctx?.record) {
+ const extra = {
+ index: ctx?.record?.id,
+ collection_name: props.collectionName,
+ };
+ if (defaultFilter) {
+ defaultFilter = { and: [defaultFilter, extra] };
+ } else {
+ defaultFilter = extra;
+ }
+ }
+ return super.list({ ...options, defaultFilter });
+ }
+ }
+
+ const resource = useResourceRequest('action_logs', ActionLogoResource);
+
+ return {
+ resource,
+ };
+};
-Table.useResource = useResource;
-Table.useActionLogDetailsResource = useActionLogDetailsResource;
Table.useActionLogsResource = useActionLogsResource;
Table.useTableFilterAction = useTableFilterAction;
Table.useTableCreateAction = useTableCreateAction;
@@ -64,3 +2662,4 @@ Table.useTableDestroyAction = useTableDestroyAction;
Table.useTableExportAction = useTableExportAction;
Table.useTableIndex = useTableIndex;
Table.useTableRowRecord = useTableRowRecord;
+Table.SimpleDesignableBar = SimpleDesignableBar;
diff --git a/packages/client/src/schemas/table/types.ts b/packages/client/src/schemas/table/types.ts
deleted file mode 100644
index e14abd146..000000000
--- a/packages/client/src/schemas/table/types.ts
+++ /dev/null
@@ -1,22 +0,0 @@
-import { ArrayField } from '@formily/core';
-import { FormProvider, observer, RecursionField, Schema, useField, useForm } from '@formily/react';
-import { BaseResult } from '@ahooksjs/use-request/lib/types';
-import { ListOptions, Resource } from '../../resource';
-
-export interface ITableContext {
- props: any;
- field: ArrayField;
- schema: Schema;
- service: BaseResult;
- selectedRowKeys?: any;
- setSelectedRowKeys?: any;
- pagination?: any;
- setPagination?: any;
- refresh?: any;
- resource?: Resource;
-}
-
-export interface ITableRowContext {
- index: number;
- record: any;
-}
diff --git a/packages/client/src/schemas/table/utils.ts b/packages/client/src/schemas/table/utils.ts
deleted file mode 100644
index b59e8f10e..000000000
--- a/packages/client/src/schemas/table/utils.ts
+++ /dev/null
@@ -1,164 +0,0 @@
-import { Schema } from '@formily/react';
-import { uid } from '@formily/shared';
-import { interfaces } from '../database-field/interfaces';
-import { ISchema } from '..';
-
-export const fieldsToSortColumns = (fields: any[]) => {
- const dataSource = [];
-
- fields.forEach((field) => {
- const fieldOption = interfaces.get(field.interface);
- if (!fieldOption?.sortable) {
- return;
- }
- dataSource.push({
- value: field.name,
- label: field?.uiSchema?.title,
- });
- });
-
- return dataSource;
-};
-
-export const fieldsToFilterColumns = (fields: any[], options: any = {}) => {
- const { fieldNames = [] } = options;
- const properties = {};
- fields.forEach((field, index) => {
- if (fieldNames?.length && !fieldNames.includes(field.name)) {
- return;
- }
- const fieldOption = interfaces.get(field.interface);
- if (!fieldOption?.operations) {
- return;
- }
- properties[`column${index}`] = {
- type: 'void',
- title: field?.uiSchema?.title,
- 'x-component': 'Filter.Column',
- 'x-component-props': {
- operations: fieldOption.operations,
- },
- properties: {
- [field.name]: {
- ...field.uiSchema,
- 'x-decorator': 'FormilyFormItem',
- title: null,
- },
- },
- };
- });
- return properties;
-};
-
-export const generateActionSchema = (type) => {
- const actions: { [key: string]: ISchema } = {
- filter: {
- key: uid(),
- name: uid(),
- type: 'void',
- title: "{{ t('Filter') }}",
- 'x-align': 'left',
- 'x-decorator': 'AddNew.Displayed',
- 'x-decorator-props': {
- displayName: 'filter',
- },
- 'x-component': 'Table.Filter',
- 'x-designable-bar': 'Table.Filter.DesignableBar',
- 'x-component-props': {
- fieldNames: [],
- },
- },
- export: {
- key: uid(),
- type: 'void',
- name: uid(),
- title: "{{ t('Export') }}",
- 'x-align': 'right',
- 'x-decorator': 'AddNew.Displayed',
- 'x-decorator-props': {
- displayName: 'export',
- },
- 'x-action-type': 'export',
- 'x-component': 'Action',
- 'x-designable-bar': 'Table.ExportActionDesignableBar',
- 'x-component-props': {
- fieldNames: [],
- icon: 'ExportOutlined',
- useAction: '{{ Table.useTableExportAction }}',
- },
- },
- create: {
- key: uid(),
- type: 'void',
- name: uid(),
- title: "{{ t('Add new') }}",
- 'x-align': 'right',
- 'x-decorator': 'AddNew.Displayed',
- 'x-decorator-props': {
- displayName: 'create',
- },
- 'x-component': 'Action',
- 'x-component-props': {
- type: 'primary',
- icon: 'PlusOutlined',
- },
- 'x-designable-bar': 'Table.Action.DesignableBar',
- properties: {
- modal: {
- type: 'void',
- title: "{{ t('Add record') }}",
- 'x-decorator': 'Form',
- 'x-component': 'Action.Drawer',
- 'x-component-props': {
- useOkAction: '{{ Table.useTableCreateAction }}',
- },
- properties: {
- [uid()]: {
- type: 'void',
- 'x-component': 'Grid',
- 'x-component-props': {
- addNewComponent: 'AddNew.FormItem',
- },
- },
- },
- },
- },
- },
- destroy: {
- key: uid(),
- type: 'void',
- name: uid(),
- title: "{{ t('Delete') }}",
- 'x-align': 'right',
- 'x-decorator': 'AddNew.Displayed',
- 'x-decorator-props': {
- displayName: 'destroy',
- },
- 'x-action-type': 'destroy',
- 'x-component': 'Action',
- 'x-designable-bar': 'Table.Action.DesignableBar',
- 'x-component-props': {
- confirm: {
- title: "{{ t('Delete record') }}",
- content: "{{ t('Are you sure you want to delete it?') }}",
- },
- useAction: '{{ Table.useTableDestroyAction }}',
- },
- },
- view: {},
- update: {},
- };
- return actions[type];
-};
-
-export function isColumn(schema: Schema) {
- return ['Table.Column'].includes(schema['x-component']);
-}
-
-export function isColumnComponent(component: string) {
- return ['Table.Operation', 'Table.Column'].includes(component);
-}
-
-export function isOperationColumn(schema: Schema) {
- return ['Table.Operation'].includes(schema['x-component']);
-}