tachybase_todo/packages/app/src/components/views/index.tsx

78 lines
2.1 KiB
TypeScript
Raw Normal View History

import React, { useEffect, useState } from 'react';
import api from '@/api-client';
import { useRequest } from 'umi';
import { Spin } from '@nocobase/client';
import { SimpleTable } from './SimpleTable';
import { Table } from './Table';
2020-12-08 21:19:39 +08:00
import { Form, DrawerForm, FilterForm } from './Form/index';
import { Details } from './Details';
import './style.less';
import { Login } from './Form/Login';
import { Register } from './Form/Register';
const TEMPLATES = new Map<string, any>();
export function registerView(template: string, Template: any) {
TEMPLATES.set(template, Template);
}
export function getViewTemplate(template: string) {
return TEMPLATES.get(template);
}
2020-12-08 21:19:39 +08:00
registerView('FilterForm', FilterForm)
registerView('DrawerForm', DrawerForm);
registerView('PermissionForm', DrawerForm);
registerView('Form', Form);
registerView('Table', Table);
registerView('SimpleTable', SimpleTable);
registerView('Details', Details);
registerView('Login', Login);
registerView('Register', Register);
export interface ViewProps {
resourceName: string;
resourceKey?: string | number;
associatedName?: string;
associatedKey?: string | number;
viewName?: string;
[key: string]: any;
}
export default function ViewFactory(props: ViewProps) {
const {
associatedName,
associatedKey,
resourceName,
2020-12-25 16:15:58 +08:00
resourceTarget,
viewName,
mode,
reference,
isAssociationView = false,
} = props;
2020-12-13 00:09:25 +08:00
console.log('propspropspropspropspropspropsprops', props);
const { data = {}, loading } = useRequest(() => {
const params = {
resourceKey: viewName,
2020-12-13 00:09:25 +08:00
values: {
associatedKey: associatedKey,
associatedName: associatedName,
mode
},
};
2020-12-25 16:15:58 +08:00
return api.resource(resourceTarget||resourceName).getView(params);
}, {
2020-12-25 16:15:58 +08:00
refreshDeps: [associatedName, resourceTarget, resourceName, viewName],
});
if (loading) {
return <Spin/>;
}
let { template } = data;
if (isAssociationView && template === 'Table') {
template = 'SimpleTable';
}
const Template = getViewTemplate(template);
return Template && <Template {...props} ref={reference} schema={data}/>;
}