feat: improve code

This commit is contained in:
chenos 2022-03-02 21:53:13 +08:00
parent 271111902f
commit 78ada4d6ec
9 changed files with 39 additions and 9 deletions

View File

@ -82,7 +82,6 @@ export class APIClient {
request<T = any, R = AxiosResponse<T>, D = any>(config: AxiosRequestConfig<D> | ResourceActionOptions): Promise<R> { request<T = any, R = AxiosResponse<T>, D = any>(config: AxiosRequestConfig<D> | ResourceActionOptions): Promise<R> {
const { resource, resourceOf, action, params } = config as any; const { resource, resourceOf, action, params } = config as any;
console.log('config', config);
if (resource) { if (resource) {
return this.resource(resource, resourceOf)[action](params); return this.resource(resource, resourceOf)[action](params);
} }
@ -102,8 +101,15 @@ export class APIClient {
config['method'] = 'post'; config['method'] = 'post';
} }
return async (params?: ActionParams) => { return async (params?: ActionParams) => {
const { values, ...others } = params || {}; const { values, filter, ...others } = params || {};
config['params'] = others; config['params'] = others;
if (filter) {
if (typeof filter === 'string') {
config['params']['filter'] = filter;
} else {
config['params']['filter'] = JSON.stringify(filter);
}
}
if (config.method !== 'get') { if (config.method !== 'get') {
config['data'] = values || {}; config['data'] = values || {};
} }

View File

@ -23,7 +23,6 @@ export function useRequest<P>(
) { ) {
// 缓存用途 // 缓存用途
const [state, setState] = useSetState({}); const [state, setState] = useSetState({});
console.log('state, setState', state);
const api = useContext(APIClientContext); const api = useContext(APIClientContext);
if (typeof service === 'function') { if (typeof service === 'function') {
const result = useReq(service, { const result = useReq(service, {

View File

@ -32,7 +32,13 @@ export const useCollectionFilterOptions = (collectionName: string) => {
const collection = getCollection(collectionName); const collection = getCollection(collectionName);
const fields = collection?.fields || []; const fields = collection?.fields || [];
const field2option = (field) => { const field2option = (field) => {
if (!field.interface) {
return;
}
const fieldInterface = getInterface(field.interface); const fieldInterface = getInterface(field.interface);
if (!fieldInterface.operators) {
return;
}
const option = { const option = {
name: field.name, name: field.name,
title: field?.uiSchema?.title || field.name, title: field?.uiSchema?.title || field.name,
@ -41,7 +47,10 @@ export const useCollectionFilterOptions = (collectionName: string) => {
return option; return option;
}; };
fields.forEach((field) => { fields.forEach((field) => {
options.push(field2option(field)); const option = field2option(field);
if (option) {
options.push(option);
}
}); });
return options; return options;
}; };

View File

@ -112,7 +112,6 @@ export const Calendar: any = observer((props: any) => {
} }
return buf; return buf;
}, null); }, null);
return ( return (
<AsyncDataProvider value={result}> <AsyncDataProvider value={result}>
<CalendarContext.Provider value={{ field, props, record }}> <CalendarContext.Provider value={{ field, props, record }}>

View File

@ -14,7 +14,7 @@ import {
} from '@formily/react'; } from '@formily/react';
import { toArr } from '@formily/shared'; import { toArr } from '@formily/shared';
import { Button, Drawer, Select, Space } from 'antd'; import { Button, Drawer, Select, Space } from 'antd';
import React, { createContext, useContext, useMemo, useState } from 'react'; import React, { createContext, useContext, useEffect, useMemo, useState } from 'react';
import { useTranslation } from 'react-i18next'; import { useTranslation } from 'react-i18next';
import { useAttach } from '../../hooks/useAttach'; import { useAttach } from '../../hooks/useAttach';
import { ActionContext, useActionContext } from '../action'; import { ActionContext, useActionContext } from '../action';
@ -40,6 +40,9 @@ const InputRecordPicker: React.FC<any> = (props) => {
const field = useField<Field>(); const field = useField<Field>();
const s = findRowSelection(fieldSchema); const s = findRowSelection(fieldSchema);
const [value, setValue] = useState(field.value); const [value, setValue] = useState(field.value);
useEffect(() => {
setValue(field.value);
}, [JSON.stringify(field.value)]);
const form = useMemo( const form = useMemo(
() => () =>
createForm({ createForm({
@ -55,7 +58,7 @@ const InputRecordPicker: React.FC<any> = (props) => {
}); });
}, },
}), }),
[], [JSON.stringify(field.value)],
); );
const toValue = (value) => { const toValue = (value) => {
if (!value) { if (!value) {

View File

@ -6,6 +6,9 @@ export const useCompile = () => {
const options = useContext(SchemaOptionsContext); const options = useContext(SchemaOptionsContext);
const scope = useContext(SchemaExpressionScopeContext); const scope = useContext(SchemaExpressionScopeContext);
return (source: any) => { return (source: any) => {
if (!source) {
return source;
}
return compile(source, { ...options.scope, ...scope }); return compile(source, { ...options.scope, ...scope });
}; };
}; };

View File

@ -119,6 +119,15 @@ insertAdjacent('afterEnd', schema);
} }
``` ```
## 扩展的 Formily Schema 属性
- `x-uid` 可缺失,前端无感。主要用于插件 `plugin-ui-schema-storage`,用于后端 schema 的增删改查
- `x-designer` schema 设计器
- `x-initializer` schema 初始化器
- `x-collection-field` 用来标记 [CollectionField](collection-manager#collectionfield)
- `x-action` 用来标记 [Action](schema-components/action)
- `x-align` ActionBar 里用于 action 的布局,包括 `left``right`
## Examples ## Examples
<code src="./demos/demo1.tsx" /> <code src="./demos/demo1.tsx" />

View File

@ -54,8 +54,8 @@ export const CalendarActionInitializers = {
}, },
{ {
type: 'item', type: 'item',
title: 'Filter', title: "{{t('Filter')}}",
component: 'ActionInitializer', component: 'FilterActionInitializer',
schema: { schema: {
title: '{{ t("Filter") }}', title: '{{ t("Filter") }}',
'x-action': 'filter', 'x-action': 'filter',

View File

@ -14,6 +14,7 @@ export const PopupFormActionInitializers = {
title: '{{ t("Submit") }}', title: '{{ t("Submit") }}',
'x-action': 'submit', 'x-action': 'submit',
'x-component': 'Action', 'x-component': 'Action',
'x-designer': 'Action.Designer',
'x-component-props': { 'x-component-props': {
type: 'primary', type: 'primary',
useAction: '{{ cm.useCreateAction }}', useAction: '{{ cm.useCreateAction }}',
@ -28,6 +29,7 @@ export const PopupFormActionInitializers = {
title: '{{ t("Cancel") }}', title: '{{ t("Cancel") }}',
'x-action': 'cancel', 'x-action': 'cancel',
'x-component': 'Action', 'x-component': 'Action',
'x-designer': 'Action.Designer',
'x-component-props': { 'x-component-props': {
useAction: '{{ cm.useCancelAction }}', useAction: '{{ cm.useCancelAction }}',
}, },