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:
		
							parent
							
								
									a9aab8ed92
								
							
						
					
					
						commit
						f567f887de
					
				@ -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,37 +353,40 @@ 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,
 | 
			
		||||
                      }
 | 
			
		||||
                    : {
 | 
			
		||||
                  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,
 | 
			
		||||
 | 
			
		||||
@ -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 (
 | 
			
		||||
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user