fix: linkage rule fireImmediately should be true (#4303)

* fix: linkagerule fireImmediately

* test: test improve

* fix: bug
This commit is contained in:
katherinehhh 2024-05-10 20:33:38 +08:00 committed by GitHub
parent 60c9abf305
commit e2922bed9e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 12 additions and 19 deletions

View File

@ -2,9 +2,7 @@
"version": "1.0.0-alpha.9", "version": "1.0.0-alpha.9",
"npmClient": "yarn", "npmClient": "yarn",
"useWorkspaces": true, "useWorkspaces": true,
"npmClientArgs": [ "npmClientArgs": ["--ignore-engines"],
"--ignore-engines"
],
"command": { "command": {
"version": { "version": {
"forcePublish": true, "forcePublish": true,

View File

@ -34,11 +34,7 @@ export interface FormProps extends IFormLayoutProps {
form?: FormilyForm; form?: FormilyForm;
disabled?: boolean; disabled?: boolean;
} }
function hasInitialValues(obj, rule) {
const type = Object.keys(rule.condition)[0] || '$and';
const conditions = rule.condition[type];
return Object.values(obj).some((value) => value !== null) || !conditions.length;
}
const FormComponent: React.FC<FormProps> = (props) => { const FormComponent: React.FC<FormProps> = (props) => {
const { form, children, ...others } = props; const { form, children, ...others } = props;
const field = useField(); const field = useField();
@ -163,11 +159,10 @@ const WithForm = (props: WithFormProps) => {
const result = [fieldValuesInCondition, variableValuesInCondition, variableValuesInExpression] const result = [fieldValuesInCondition, variableValuesInCondition, variableValuesInExpression]
.map((item) => JSON.stringify(item)) .map((item) => JSON.stringify(item))
.join(','); .join(',');
return result; return result;
}, },
getSubscriber(action, field, rule, variables, localVariables), getSubscriber(action, field, rule, variables, localVariables),
{ fireImmediately: hasInitialValues(form.initialValues, rule) }, { fireImmediately: true },
), ),
); );
}); });

View File

@ -173,13 +173,11 @@ describe('Form', () => {
await userEvent.type(document.querySelector('.ant-input'), 'test'); await userEvent.type(document.querySelector('.ant-input'), 'test');
await waitFor(() => { await waitFor(() => {
expect(document.querySelectorAll('.ant-input')).toHaveLength(1); const element = document.querySelector(
}); '[data-label="block-item-CollectionField-users-form-users.nickname-Nickname"]',
);
await userEvent.clear(document.querySelector('.ant-input')); expect(element).toBeNull();
await waitFor(() => {
expect(document.querySelectorAll('.ant-input')).toHaveLength(2);
}); });
}); });
}); });

View File

@ -74,7 +74,7 @@ export const useCurrentFormVariable = ({
}: Props = {}) => { }: Props = {}) => {
// const { getActiveFieldsName } = useFormActiveFields() || {}; // const { getActiveFieldsName } = useFormActiveFields() || {};
const { t } = useTranslation(); const { t } = useTranslation();
const { form, collectionName } = useFormBlockContext(); const { form, collectionName, service } = useFormBlockContext();
const currentFormSettings = useBaseVariable({ const currentFormSettings = useBaseVariable({
collectionField, collectionField,
uiSchema: schema, uiSchema: schema,
@ -98,12 +98,14 @@ export const useCurrentFormVariable = ({
}); });
const formInstance = _form || form; const formInstance = _form || form;
return { return {
/** 变量配置 */ /** 变量配置 */
currentFormSettings, currentFormSettings,
/** 变量值 */ /** 变量值 */
currentFormCtx: formInstance?.values, currentFormCtx:
formInstance?.values && Object.keys(formInstance?.values)?.length
? formInstance?.values
: service?.data?.data || formInstance?.values,
/** 用来判断是否可以显示`当前表单`变量 */ /** 用来判断是否可以显示`当前表单`变量 */
shouldDisplayCurrentForm: formInstance && !formInstance.readPretty, shouldDisplayCurrentForm: formInstance && !formInstance.readPretty,
}; };