@nocobase/client-v2 2.3.0-beta.27 → 2.3.0-beta.29

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -7,46 +7,71 @@
7
7
  * For more information, please refer to: https://www.nocobase.com/agreement.
8
8
  */
9
9
 
10
- import { FlowEngine } from '@nocobase/flow-engine';
10
+ import { FlowEngine, FlowModel, Tracker } from '@nocobase/flow-engine';
11
11
  import { render } from '@testing-library/react';
12
12
  import React from 'react';
13
13
  import { expect, it, vi } from 'vitest';
14
14
  import { DetailsItemModel } from '../DetailsItemModel';
15
15
 
16
16
  it.each([
17
- [true, 'horizontal', ''],
18
- [true, 'vertical', '12px'],
19
- [false, 'horizontal', '12px'],
20
- ] as const)('sets field spacing for isMobileLayout=%s and layout=%s', (isMobileLayout, layout, inlineMargin) => {
21
- const model = new DetailsItemModel({ uid: 'detail-field', flowEngine: new FlowEngine() });
22
- model.context.defineProperty('isMobileLayout', { value: isMobileLayout });
23
- model.context.defineProperty('blockModel', { value: { props: { layout } } });
24
- model.setProps('label', 'Name');
25
- model.setStepParams('fieldSettings', 'init', { fieldPath: 'name' });
26
- const Field = vi.fn(() => <span>Value</span>);
17
+ ['DetailsBlockModel', true, 'horizontal', 'DisplayTextFieldModel', 'text', '3px', '29px', true],
18
+ ['GridCardBlockModel', true, 'horizontal', 'DisplayTextFieldModel', 'text', '8px', '24px', true],
19
+ ['GridCardBlockModel', true, 'horizontal', 'DisplayTextFieldModel', 'tag', '8px', '24px', false],
20
+ ['DetailsBlockModel', true, 'horizontal', 'DisplayHtmlFieldModel', 'text', '3px', '29px', false],
21
+ ['CCTaskCardDetailsModel', true, 'horizontal', 'DisplayTextFieldModel', 'text', '', '', false],
22
+ ['DetailsBlockModel', false, 'horizontal', 'DisplayTextFieldModel', 'text', '', '', false],
23
+ ['GridCardBlockModel', true, 'vertical', 'DisplayTextFieldModel', 'text', '', '', false],
24
+ ] as const)(
25
+ 'selects phone styles for %s, mobile=%s, %s, %s (%s)',
26
+ (block, mobile, layout, field, displayStyle, margin, minHeight, plainText) => {
27
+ const engine = new FlowEngine();
28
+ engine.registerModels({ [field]: FlowModel });
29
+ const model = new DetailsItemModel({ uid: 'detail-field', flowEngine: engine });
30
+ model.context.defineProperty('blockModel', { value: { use: block, props: { layout } } });
31
+ model.context.defineProperty('isMobileLayout', { value: mobile });
32
+ model.setProps('label', 'Name');
33
+ model.setStepParams('fieldSettings', 'init', { fieldPath: 'name' });
34
+ model.setSubModel('field', { use: field, props: { displayStyle } });
27
35
 
28
- const { container } = render(React.cloneElement(model.renderItem(), {}, <Field />));
36
+ const { container } = render(React.cloneElement(model.renderItem(), {}, <span>Value</span>));
37
+ const item = container.querySelector<HTMLElement>('.ant-form-item');
38
+ const value = item.querySelector('.ant-form-item-control-input-content');
39
+ const rules = Array.from(document.styleSheets).flatMap((sheet) => Array.from(sheet.cssRules));
40
+ const narrowRules = rules.flatMap((rule) =>
41
+ rule instanceof CSSMediaRule && rule.conditionText === '(max-width: 575px)' ? Array.from(rule.cssRules) : [],
42
+ );
43
+ const declarations = (element: Element, property: string) =>
44
+ narrowRules
45
+ .flatMap((rule) =>
46
+ rule instanceof CSSStyleRule && element.matches(rule.selectorText)
47
+ ? [rule.style.getPropertyValue(property)]
48
+ : [],
49
+ )
50
+ .filter(Boolean);
29
51
 
30
- const item = container.querySelector<HTMLElement>('.ant-form-item');
31
- expect(item.style.marginBottom).toBe(inlineMargin);
32
- const rules = Array.from(document.styleSheets).flatMap((sheet) => Array.from(sheet.cssRules));
33
- const matchesMargin = (rule: CSSRule, value: string) =>
34
- rule instanceof CSSStyleRule &&
35
- rule.style.getPropertyValue('margin-bottom') === value &&
36
- item.matches(rule.selectorText);
52
+ expect(item.style.marginBottom).toBe(margin ? '' : '12px');
53
+ expect(declarations(item, 'margin-bottom')).toEqual(margin ? [margin] : []);
54
+ expect(declarations(item, 'min-height')).toEqual(minHeight ? [minHeight] : []);
55
+ expect(declarations(value, 'line-height')).toEqual(plainText ? ['max(20px, 1.4em)'] : []);
56
+ },
57
+ );
37
58
 
38
- const responsive = isMobileLayout && layout === 'horizontal';
39
- expect(rules.some((rule) => matchesMargin(rule, '11px'))).toBe(responsive);
40
- expect(
41
- rules.some(
42
- (rule) =>
43
- rule instanceof CSSMediaRule &&
44
- rule.conditionText === '(max-width: 575px)' &&
45
- Array.from(rule.cssRules).some((child) => matchesMargin(child, '3px')),
46
- ),
47
- ).toBe(responsive);
48
- expect(container.querySelector<HTMLElement>('.ant-form-item-label')?.style.paddingBottom).toBe(
49
- layout === 'vertical' ? '0px' : '',
50
- );
51
- expect(Field).toHaveBeenCalledWith(expect.not.objectContaining({ itemMarginClassName: expect.any(String) }), {});
59
+ it('does not subscribe the grid card item to field renderer prop writes', () => {
60
+ const engine = new FlowEngine();
61
+ engine.registerModels({ DisplayTextFieldModel: FlowModel });
62
+ const item = new DetailsItemModel({ uid: 'card-field', flowEngine: engine });
63
+ item.context.defineProperty('blockModel', { value: { use: 'GridCardBlockModel', props: { layout: 'horizontal' } } });
64
+ item.context.defineProperty('isMobileLayout', { value: true });
65
+ item.context.defineProperty('fieldIndex', { value: 0 });
66
+ item.setStepParams('fieldSettings', 'init', { fieldPath: 'name' });
67
+ const field = item.setSubModel('field', { use: 'DisplayTextFieldModel' });
68
+ const invalidate = vi.fn();
69
+ const tracker = new Tracker(invalidate);
70
+ try {
71
+ tracker.track(() => item.renderItem());
72
+ field.getFork('0').setProps({ value: 'First row' });
73
+ expect(invalidate).not.toHaveBeenCalled();
74
+ } finally {
75
+ tracker.dispose();
76
+ }
52
77
  });
@@ -0,0 +1,46 @@
1
+ /**
2
+ * This file is part of the NocoBase (R) project.
3
+ * Copyright (c) 2020-2024 NocoBase Co., Ltd.
4
+ * Authors: NocoBase Team.
5
+ *
6
+ * This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
7
+ * For more information, please refer to: https://www.nocobase.com/agreement.
8
+ */
9
+
10
+ import { useRequest } from 'ahooks';
11
+ import type { FlowModel } from '@nocobase/flow-engine';
12
+ import React, { useEffect } from 'react';
13
+ import { isRouteContentConfirmed } from './FlowModelRepository';
14
+
15
+ // Keep the current renderer mounted while the first editor confirms the stored page or grid.
16
+ export function useRouteContentRequest(
17
+ load: (forEditing: boolean) => Promise<FlowModel>,
18
+ editing: boolean,
19
+ refreshDeps: unknown[],
20
+ ) {
21
+ const { data, error, loading, refresh } = useRequest(
22
+ async () => ({ model: await load(editing), confirmed: editing, refreshDeps }),
23
+ { refreshDeps },
24
+ );
25
+ const current = data && refreshDeps.every((dep, i) => Object.is(dep, data.refreshDeps[i])) ? data : undefined;
26
+ const needsConfirmation =
27
+ editing && !!current?.model && !current.confirmed && !isRouteContentConfirmed(current.model);
28
+ useEffect(() => {
29
+ if (needsConfirmation && !loading && !error) refresh();
30
+ }, [needsConfirmation, loading, error, refresh]);
31
+
32
+ return { model: current?.model, error, loading, needsConfirmation };
33
+ }
34
+
35
+ export function RouteContent({
36
+ pending,
37
+ className,
38
+ children,
39
+ }: React.PropsWithChildren<{ pending: boolean; className?: string }>) {
40
+ // React 18 forwards inert as a string attribute. Keep the same DOM and component state during initialization.
41
+ return (
42
+ <div className={className} style={{ display: 'contents' }} {...(pending ? { inert: '' } : {})}>
43
+ {children}
44
+ </div>
45
+ );
46
+ }