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

View File

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

View File

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

View File

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

View File

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

View File

@ -6,6 +6,9 @@ export const useCompile = () => {
const options = useContext(SchemaOptionsContext);
const scope = useContext(SchemaExpressionScopeContext);
return (source: any) => {
if (!source) {
return source;
}
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
<code src="./demos/demo1.tsx" />

View File

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

View File

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