fix(default-value): should not show 'N/A' when a normal value is selected. (#2365)

* fix(default-value): fix N/A

* fix: fix invalid selection
This commit is contained in:
被雨水过滤的空气-Rain 2023-08-01 16:19:15 +08:00 committed by GitHub
parent 7151345c40
commit 4e4c4eae27
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 42 additions and 34 deletions

View File

@ -2,7 +2,7 @@ import { LoadingOutlined, PlusOutlined } from '@ant-design/icons';
import { RecursionField, connect, mapProps, observer, useField, useFieldSchema, useForm } from '@formily/react'; import { RecursionField, connect, mapProps, observer, useField, useFieldSchema, useForm } from '@formily/react';
import { Space, message } from 'antd'; import { Space, message } from 'antd';
import { isFunction } from 'mathjs'; import { isFunction } from 'mathjs';
import React from 'react'; import React, { useState } from 'react';
import { useTranslation } from 'react-i18next'; import { useTranslation } from 'react-i18next';
import { RecordProvider, useAPIClient } from '../../../'; import { RecordProvider, useAPIClient } from '../../../';
import { isVariable } from '../../common/utils/uitls'; import { isVariable } from '../../common/utils/uitls';
@ -22,6 +22,10 @@ const InternalAssociationSelect = observer((props: AssociationSelectProps) => {
const { options: collectionField } = useAssociationFieldContext(); const { options: collectionField } = useAssociationFieldContext();
const initValue = isVariable(props.value) ? undefined : props.value; const initValue = isVariable(props.value) ? undefined : props.value;
const value = Array.isArray(initValue) ? initValue.filter(Boolean) : initValue; const value = Array.isArray(initValue) ? initValue.filter(Boolean) : initValue;
// 因为通过 Schema 的形式书写的组件,在值变更的时候 `value` 的值没有改变,所以需要维护一个 `innerValue` 来变更值
const [innerValue, setInnerValue] = useState(value);
const addMode = fieldSchema['x-component-props']?.addMode; const addMode = fieldSchema['x-component-props']?.addMode;
const isAllowAddNew = fieldSchema['x-add-new']; const isAllowAddNew = fieldSchema['x-add-new'];
const { t } = useTranslation(); const { t } = useTranslation();
@ -29,6 +33,7 @@ const InternalAssociationSelect = observer((props: AssociationSelectProps) => {
const form = useForm(); const form = useForm();
const api = useAPIClient(); const api = useAPIClient();
const resource = api.resource(collectionField.target); const resource = api.resource(collectionField.target);
const handleCreateAction = async (props) => { const handleCreateAction = async (props) => {
const { search: value, callBack } = props; const { search: value, callBack } = props;
const { const {
@ -63,6 +68,7 @@ const InternalAssociationSelect = observer((props: AssociationSelectProps) => {
</div> </div>
); );
}; };
return ( return (
<div key={fieldSchema.name}> <div key={fieldSchema.name}>
<Space.Compact style={{ display: 'flex', lineHeight: '32px' }}> <Space.Compact style={{ display: 'flex', lineHeight: '32px' }}>
@ -71,8 +77,12 @@ const InternalAssociationSelect = observer((props: AssociationSelectProps) => {
{...props} {...props}
size={'middle'} size={'middle'}
objectValue={objectValue} objectValue={objectValue}
value={value} value={value || innerValue}
service={service} service={service}
onChange={(value) => {
setInnerValue(value);
props.onChange?.(value);
}}
CustomDropdownRender={addMode === 'quickAdd' && QuickAddContent} CustomDropdownRender={addMode === 'quickAdd' && QuickAddContent}
></RemoteSelect> ></RemoteSelect>
@ -99,7 +109,7 @@ interface AssociationSelectInterface {
FilterDesigner: React.FC; FilterDesigner: React.FC;
} }
export const AssociationSelect = InternalAssociationSelect as unknown as AssociationSelectInterface; export const AssociationSelect = (InternalAssociationSelect as unknown) as AssociationSelectInterface;
export const AssociationSelectReadPretty = connect( export const AssociationSelectReadPretty = connect(
(props: any) => { (props: any) => {

View File

@ -1,7 +1,7 @@
import { css, cx } from '@emotion/css'; import { css, cx } from '@emotion/css';
import { ArrayCollapse, ArrayItems, FormItem as Item, FormLayout } from '@formily/antd-v5'; import { ArrayCollapse, ArrayItems, FormLayout, FormItem as Item } from '@formily/antd-v5';
import { Field } from '@formily/core'; import { Field } from '@formily/core';
import { ISchema, observer, Schema, useField, useFieldSchema } from '@formily/react'; import { ISchema, observer, useField, useFieldSchema } from '@formily/react';
import { dayjs } from '@nocobase/utils/client'; import { dayjs } from '@nocobase/utils/client';
import { Select } from 'antd'; import { Select } from 'antd';
import _ from 'lodash'; import _ from 'lodash';
@ -20,7 +20,7 @@ import {
} from '../../../collection-manager'; } from '../../../collection-manager';
import { isTitleField } from '../../../collection-manager/Configuration/CollectionFields'; import { isTitleField } from '../../../collection-manager/Configuration/CollectionFields';
import { GeneralSchemaItems } from '../../../schema-items/GeneralSchemaItems'; import { GeneralSchemaItems } from '../../../schema-items/GeneralSchemaItems';
import { GeneralSchemaDesigner, isPatternDisabled, isShowDefaultValue, SchemaSettings } from '../../../schema-settings'; import { GeneralSchemaDesigner, SchemaSettings, isPatternDisabled, isShowDefaultValue } from '../../../schema-settings';
import { useIsShowMultipleSwitch } from '../../../schema-settings/hooks/useIsShowMultipleSwitch'; import { useIsShowMultipleSwitch } from '../../../schema-settings/hooks/useIsShowMultipleSwitch';
import { isVariable, parseVariables, useVariablesCtx } from '../../common/utils/uitls'; import { isVariable, parseVariables, useVariablesCtx } from '../../common/utils/uitls';
import { useCompile, useDesignable, useFieldModeOptions } from '../../hooks'; import { useCompile, useDesignable, useFieldModeOptions } from '../../hooks';

View File

@ -98,8 +98,6 @@ interface SchemaSettingsContextProps {
collectionName?: any; collectionName?: any;
} }
const mouseEnterDelay = 150;
const SchemaSettingsContext = createContext<SchemaSettingsContextProps>(null); const SchemaSettingsContext = createContext<SchemaSettingsContextProps>(null);
export const useSchemaSettings = () => { export const useSchemaSettings = () => {
@ -1443,7 +1441,7 @@ export const findParentFieldSchema = (fieldSchema: Schema) => {
} }
}; };
SchemaSettings.DefaultValue = function DefaultvalueConfigure(props) { SchemaSettings.DefaultValue = function DefaultValueConfigure(props) {
const variablesCtx = useVariablesCtx(); const variablesCtx = useVariablesCtx();
const currentSchema = useFieldSchema(); const currentSchema = useFieldSchema();
const fieldSchema = props?.fieldSchema ?? currentSchema; const fieldSchema = props?.fieldSchema ?? currentSchema;
@ -1463,9 +1461,10 @@ SchemaSettings.DefaultValue = function DefaultvalueConfigure(props) {
const parentFieldSchema = collectionField?.interface === 'm2o' && findParentFieldSchema(fieldSchema); const parentFieldSchema = collectionField?.interface === 'm2o' && findParentFieldSchema(fieldSchema);
const parentCollectionField = parentFieldSchema && getCollectionJoinField(parentFieldSchema?.['x-collection-field']); const parentCollectionField = parentFieldSchema && getCollectionJoinField(parentFieldSchema?.['x-collection-field']);
const tableCtx = useTableBlockContext(); const tableCtx = useTableBlockContext();
const isAllowContexVariable = const isAllowContextVariable =
collectionField?.interface === 'm2m' || collectionField?.interface === 'm2m' ||
(parentCollectionField?.type === 'hasMany' && collectionField?.interface === 'm2o'); (parentCollectionField?.type === 'hasMany' && collectionField?.interface === 'm2o');
return ( return (
<SchemaSettings.ModalItem <SchemaSettings.ModalItem
title={t('Set default value')} title={t('Set default value')}
@ -1477,31 +1476,31 @@ SchemaSettings.DefaultValue = function DefaultvalueConfigure(props) {
title: t('Set default value'), title: t('Set default value'),
properties: { properties: {
default: { default: {
...(fieldSchemaWithoutRequired || {}),
'x-decorator': 'FormItem', 'x-decorator': 'FormItem',
'x-component': 'VariableInput', 'x-component': 'VariableInput',
'x-component-props': { 'x-component-props': {
...(fieldSchema?.['x-component-props'] || {}), ...(fieldSchema?.['x-component-props'] || {}),
collectionField, collectionField,
targetField, contextCollectionName: isAllowContextVariable && tableCtx.collection,
collectionName: collectionField?.collectionName,
contextCollectionName: isAllowContexVariable && tableCtx.collection,
schema: collectionField?.uiSchema, schema: collectionField?.uiSchema,
className: defaultInputStyle, className: defaultInputStyle,
renderSchemaComponent: function Com(props) { renderSchemaComponent: function Com(props) {
const s = _.cloneDeep(fieldSchemaWithoutRequired) || ({} as Schema); const s = _.cloneDeep(fieldSchemaWithoutRequired) || ({} as Schema);
s.title = ''; s.title = '';
// 任何一个非空字符串都可以
s.name = 'default';
s['x-read-pretty'] = false; s['x-read-pretty'] = false;
s['x-disabled'] = false; s['x-disabled'] = false;
return ( const schema = {
<SchemaComponent
schema={{
...(s || {}), ...(s || {}),
'x-component-props': { 'x-component-props': {
...s['x-component-props'], ...s['x-component-props'],
collectionName: collectionField?.collectionName,
targetField,
onChange: props.onChange, onChange: props.onChange,
value: props.value,
defaultValue: getFieldDefaultValue(s, collectionField), defaultValue: getFieldDefaultValue(s, collectionField),
style: { style: {
width: '100%', width: '100%',
@ -1509,12 +1508,11 @@ SchemaSettings.DefaultValue = function DefaultvalueConfigure(props) {
minWidth: '200px', minWidth: '200px',
}, },
}, },
}} };
/>
); return <SchemaComponent schema={schema} />;
}, },
}, },
name: 'default',
title: t('Default value'), title: t('Default value'),
default: getFieldDefaultValue(fieldSchema, collectionField), default: getFieldDefaultValue(fieldSchema, collectionField),
}, },