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 { removeNullCondition } from '../filter';
 | 
				
			||||||
import { HTMLEncode } from '../input/shared';
 | 
					import { HTMLEncode } from '../input/shared';
 | 
				
			||||||
import { FilterDynamicComponent } from '../table-v2/FilterDynamicComponent';
 | 
					import { FilterDynamicComponent } from '../table-v2/FilterDynamicComponent';
 | 
				
			||||||
import { isInvariable } from '../variable';
 | 
					 | 
				
			||||||
import { FilterFormDesigner } from './FormItem.FilterFormDesigner';
 | 
					import { FilterFormDesigner } from './FormItem.FilterFormDesigner';
 | 
				
			||||||
import { useEnsureOperatorsValid } from './SchemaSettingOptions';
 | 
					import { useEnsureOperatorsValid } from './SchemaSettingOptions';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -354,70 +353,73 @@ FormItem.Designer = function Designer() {
 | 
				
			|||||||
                type: 'object',
 | 
					                type: 'object',
 | 
				
			||||||
                title: t('Set default value'),
 | 
					                title: t('Set default value'),
 | 
				
			||||||
                properties: {
 | 
					                properties: {
 | 
				
			||||||
                  default: isInvariable(interfaceConfig)
 | 
					                  default:
 | 
				
			||||||
                    ? {
 | 
					                    //  isInvariable(interfaceConfig)
 | 
				
			||||||
                        ...(fieldSchemaWithoutRequired || {}),
 | 
					                    // ? {
 | 
				
			||||||
                        'x-decorator': 'FormItem',
 | 
					                    //     ...(fieldSchemaWithoutRequired || {}),
 | 
				
			||||||
                        'x-component-props': {
 | 
					                    //     'x-decorator': 'FormItem',
 | 
				
			||||||
                          ...fieldSchema['x-component-props'],
 | 
					                    //     'x-component-props': {
 | 
				
			||||||
                          targetField,
 | 
					                    //       ...fieldSchema['x-component-props'],
 | 
				
			||||||
                          component:
 | 
					                    //       targetField,
 | 
				
			||||||
                            collectionField?.target && collectionField?.interface !== 'chinaRegion'
 | 
					                    //       component:
 | 
				
			||||||
                              ? 'AssociationSelect'
 | 
					                    //         collectionField?.target && collectionField?.interface !== 'chinaRegion'
 | 
				
			||||||
                              : undefined,
 | 
					                    //           ? 'AssociationSelect'
 | 
				
			||||||
                          service: {
 | 
					                    //           : undefined,
 | 
				
			||||||
                            resource: collectionField?.target,
 | 
					                    //       service: {
 | 
				
			||||||
                          },
 | 
					                    //         resource: collectionField?.target,
 | 
				
			||||||
                          style: {
 | 
					                    //       },
 | 
				
			||||||
                            width: '100%',
 | 
					                    //       style: {
 | 
				
			||||||
                            verticalAlign: 'top',
 | 
					                    //         width: '100%',
 | 
				
			||||||
                          },
 | 
					                    //         verticalAlign: 'top',
 | 
				
			||||||
                        },
 | 
					                    //       },
 | 
				
			||||||
                        name: 'default',
 | 
					                    //     },
 | 
				
			||||||
                        title: t('Default value'),
 | 
					                    //     name: 'default',
 | 
				
			||||||
                        default: getFieldDefaultValue(fieldSchema, collectionField),
 | 
					                    //     title: t('Default value'),
 | 
				
			||||||
                        'x-read-pretty': false,
 | 
					                    //     default: getFieldDefaultValue(fieldSchema, collectionField),
 | 
				
			||||||
                        'x-disabled': false,
 | 
					                    //     'x-read-pretty': false,
 | 
				
			||||||
                      }
 | 
					                    //     'x-disabled': false,
 | 
				
			||||||
                    : {
 | 
					                    //   }
 | 
				
			||||||
                        ...(fieldSchemaWithoutRequired || {}),
 | 
					                    // :
 | 
				
			||||||
                        'x-decorator': 'FormItem',
 | 
					                    {
 | 
				
			||||||
                        'x-component': 'VariableInput',
 | 
					                      ...(fieldSchemaWithoutRequired || {}),
 | 
				
			||||||
                        'x-component-props': {
 | 
					                      'x-decorator': 'FormItem',
 | 
				
			||||||
                          ...(fieldSchema?.['x-component-props'] || {}),
 | 
					                      'x-component': 'VariableInput',
 | 
				
			||||||
                          targetField,
 | 
					                      'x-component-props': {
 | 
				
			||||||
                          collectionName: collectionField?.collectionName,
 | 
					                        ...(fieldSchema?.['x-component-props'] || {}),
 | 
				
			||||||
                          schema: collectionField?.uiSchema,
 | 
					                        collectionField,
 | 
				
			||||||
                          className: defaultInputStyle,
 | 
					                        targetField,
 | 
				
			||||||
                          renderSchemaComponent: function Com(props) {
 | 
					                        collectionName: collectionField?.collectionName,
 | 
				
			||||||
                            const s = _.cloneDeep(fieldSchemaWithoutRequired) || ({} as Schema);
 | 
					                        schema: collectionField?.uiSchema,
 | 
				
			||||||
                            s.title = '';
 | 
					                        className: defaultInputStyle,
 | 
				
			||||||
                            s['x-read-pretty'] = false;
 | 
					                        renderSchemaComponent: function Com(props) {
 | 
				
			||||||
                            s['x-disabled'] = false;
 | 
					                          const s = _.cloneDeep(fieldSchemaWithoutRequired) || ({} as Schema);
 | 
				
			||||||
 | 
					                          s.title = '';
 | 
				
			||||||
 | 
					                          s['x-read-pretty'] = false;
 | 
				
			||||||
 | 
					                          s['x-disabled'] = false;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                            return (
 | 
					                          return (
 | 
				
			||||||
                              <SchemaComponent
 | 
					                            <SchemaComponent
 | 
				
			||||||
                                schema={{
 | 
					                              schema={{
 | 
				
			||||||
                                  ...(s || {}),
 | 
					                                ...(s || {}),
 | 
				
			||||||
                                  'x-component-props': {
 | 
					                                'x-component-props': {
 | 
				
			||||||
                                    ...s['x-component-props'],
 | 
					                                  ...s['x-component-props'],
 | 
				
			||||||
                                    onChange: props.onChange,
 | 
					                                  onChange: props.onChange,
 | 
				
			||||||
                                    value: props.value,
 | 
					                                  value: props.value,
 | 
				
			||||||
                                    defaultValue: getFieldDefaultValue(s, collectionField),
 | 
					                                  defaultValue: getFieldDefaultValue(s, collectionField),
 | 
				
			||||||
                                    style: {
 | 
					                                  style: {
 | 
				
			||||||
                                      width: '100%',
 | 
					                                    width: '100%',
 | 
				
			||||||
                                      verticalAlign: 'top',
 | 
					                                    verticalAlign: 'top',
 | 
				
			||||||
                                    },
 | 
					 | 
				
			||||||
                                  },
 | 
					                                  },
 | 
				
			||||||
                                }}
 | 
					                                },
 | 
				
			||||||
                              />
 | 
					                              }}
 | 
				
			||||||
                            );
 | 
					                            />
 | 
				
			||||||
                          },
 | 
					                          );
 | 
				
			||||||
                        },
 | 
					                        },
 | 
				
			||||||
                        name: 'default',
 | 
					 | 
				
			||||||
                        title: t('Default value'),
 | 
					 | 
				
			||||||
                        default: getFieldDefaultValue(fieldSchema, collectionField),
 | 
					 | 
				
			||||||
                      },
 | 
					                      },
 | 
				
			||||||
 | 
					                      name: 'default',
 | 
				
			||||||
 | 
					                      title: t('Default value'),
 | 
				
			||||||
 | 
					                      default: getFieldDefaultValue(fieldSchema, collectionField),
 | 
				
			||||||
 | 
					                    },
 | 
				
			||||||
                },
 | 
					                },
 | 
				
			||||||
              } as ISchema
 | 
					              } as ISchema
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
 | 
				
			|||||||
@ -1,5 +1,6 @@
 | 
				
			|||||||
import React, { useMemo } from 'react';
 | 
					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';
 | 
					import { useUserVariable } from './hooks/useUserVariable';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type Props = {
 | 
					type Props = {
 | 
				
			||||||
@ -12,15 +13,23 @@ type Props = {
 | 
				
			|||||||
  children?: any;
 | 
					  children?: any;
 | 
				
			||||||
  className?: string;
 | 
					  className?: string;
 | 
				
			||||||
  style?: React.CSSProperties;
 | 
					  style?: React.CSSProperties;
 | 
				
			||||||
 | 
					  collectionField?: CollectionFieldOptions;
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
export const VariableInput = (props: Props) => {
 | 
					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 compile = useCompile();
 | 
				
			||||||
  const userVariable = useUserVariable({ schema, maxDepth: 1 });
 | 
					  const userVariable = useUserVariable({ schema, maxDepth: 1 });
 | 
				
			||||||
  const scope = useMemo(() => {
 | 
					  const scope = useMemo(() => {
 | 
				
			||||||
    return [
 | 
					    const data = [
 | 
				
			||||||
      userVariable,
 | 
					 | 
				
			||||||
      compile({
 | 
					      compile({
 | 
				
			||||||
        label: `{{t("Date variables")}}`,
 | 
					        label: `{{t("Date variables")}}`,
 | 
				
			||||||
        value: '$date',
 | 
					        value: '$date',
 | 
				
			||||||
@ -35,6 +44,10 @@ export const VariableInput = (props: Props) => {
 | 
				
			|||||||
        ],
 | 
					        ],
 | 
				
			||||||
      }),
 | 
					      }),
 | 
				
			||||||
    ];
 | 
					    ];
 | 
				
			||||||
 | 
					    if (collectionField?.target === 'users') {
 | 
				
			||||||
 | 
					      data.unshift(userVariable);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    return data;
 | 
				
			||||||
  }, []);
 | 
					  }, []);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  return (
 | 
					  return (
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
		Reference in New Issue
	
	Block a user