refactor: the default value setting of association field supports variables (#2138)

* refactor: association field support variablein default value

* refactor: the current user variable only supports the target collection  us users
This commit is contained in:
katherinehhh 2023-07-06 17:57:35 +08:00 committed by GitHub
parent a9aab8ed92
commit f567f887de
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 80 additions and 65 deletions

View File

@ -30,7 +30,6 @@ import { BlockItem } from '../block-item';
import { removeNullCondition } from '../filter';
import { HTMLEncode } from '../input/shared';
import { FilterDynamicComponent } from '../table-v2/FilterDynamicComponent';
import { isInvariable } from '../variable';
import { FilterFormDesigner } from './FormItem.FilterFormDesigner';
import { useEnsureOperatorsValid } from './SchemaSettingOptions';
@ -354,70 +353,73 @@ FormItem.Designer = function Designer() {
type: 'object',
title: t('Set default value'),
properties: {
default: isInvariable(interfaceConfig)
? {
...(fieldSchemaWithoutRequired || {}),
'x-decorator': 'FormItem',
'x-component-props': {
...fieldSchema['x-component-props'],
targetField,
component:
collectionField?.target && collectionField?.interface !== 'chinaRegion'
? 'AssociationSelect'
: undefined,
service: {
resource: collectionField?.target,
},
style: {
width: '100%',
verticalAlign: 'top',
},
},
name: 'default',
title: t('Default value'),
default: getFieldDefaultValue(fieldSchema, collectionField),
'x-read-pretty': false,
'x-disabled': false,
}
: {
...(fieldSchemaWithoutRequired || {}),
'x-decorator': 'FormItem',
'x-component': 'VariableInput',
'x-component-props': {
...(fieldSchema?.['x-component-props'] || {}),
targetField,
collectionName: collectionField?.collectionName,
schema: collectionField?.uiSchema,
className: defaultInputStyle,
renderSchemaComponent: function Com(props) {
const s = _.cloneDeep(fieldSchemaWithoutRequired) || ({} as Schema);
s.title = '';
s['x-read-pretty'] = false;
s['x-disabled'] = false;
default:
// isInvariable(interfaceConfig)
// ? {
// ...(fieldSchemaWithoutRequired || {}),
// 'x-decorator': 'FormItem',
// 'x-component-props': {
// ...fieldSchema['x-component-props'],
// targetField,
// component:
// collectionField?.target && collectionField?.interface !== 'chinaRegion'
// ? 'AssociationSelect'
// : undefined,
// service: {
// resource: collectionField?.target,
// },
// style: {
// width: '100%',
// verticalAlign: 'top',
// },
// },
// name: 'default',
// title: t('Default value'),
// default: getFieldDefaultValue(fieldSchema, collectionField),
// 'x-read-pretty': false,
// 'x-disabled': false,
// }
// :
{
...(fieldSchemaWithoutRequired || {}),
'x-decorator': 'FormItem',
'x-component': 'VariableInput',
'x-component-props': {
...(fieldSchema?.['x-component-props'] || {}),
collectionField,
targetField,
collectionName: collectionField?.collectionName,
schema: collectionField?.uiSchema,
className: defaultInputStyle,
renderSchemaComponent: function Com(props) {
const s = _.cloneDeep(fieldSchemaWithoutRequired) || ({} as Schema);
s.title = '';
s['x-read-pretty'] = false;
s['x-disabled'] = false;
return (
<SchemaComponent
schema={{
...(s || {}),
'x-component-props': {
...s['x-component-props'],
onChange: props.onChange,
value: props.value,
defaultValue: getFieldDefaultValue(s, collectionField),
style: {
width: '100%',
verticalAlign: 'top',
},
return (
<SchemaComponent
schema={{
...(s || {}),
'x-component-props': {
...s['x-component-props'],
onChange: props.onChange,
value: props.value,
defaultValue: getFieldDefaultValue(s, collectionField),
style: {
width: '100%',
verticalAlign: 'top',
},
}}
/>
);
},
},
}}
/>
);
},
name: 'default',
title: t('Default value'),
default: getFieldDefaultValue(fieldSchema, collectionField),
},
name: 'default',
title: t('Default value'),
default: getFieldDefaultValue(fieldSchema, collectionField),
},
},
} as ISchema
}

View File

@ -1,5 +1,6 @@
import React, { useMemo } from 'react';
import { Variable, useCompile } from '../../schema-component';
import { CollectionFieldOptions } from '../../collection-manager';
import { useCompile, Variable } from '../../schema-component';
import { useUserVariable } from './hooks/useUserVariable';
type Props = {
@ -12,15 +13,23 @@ type Props = {
children?: any;
className?: string;
style?: React.CSSProperties;
collectionField?: CollectionFieldOptions;
};
export const VariableInput = (props: Props) => {
const { value, onChange, renderSchemaComponent: RenderSchemaComponent, style, schema, className } = props;
const {
value,
onChange,
renderSchemaComponent: RenderSchemaComponent,
style,
schema,
className,
collectionField,
} = props;
const compile = useCompile();
const userVariable = useUserVariable({ schema, maxDepth: 1 });
const scope = useMemo(() => {
return [
userVariable,
const data = [
compile({
label: `{{t("Date variables")}}`,
value: '$date',
@ -35,6 +44,10 @@ export const VariableInput = (props: Props) => {
],
}),
];
if (collectionField?.target === 'users') {
data.unshift(userVariable);
}
return data;
}, []);
return (