Skip to content

Commit a1f3601

Browse files
authored
fix: add spec correctness check for view controller (#127)
1 parent cb1ad79 commit a1f3601

File tree

6 files changed

+53
-59
lines changed

6 files changed

+53
-59
lines changed

src/lib/core/components/Form/Controller.tsx

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import _ from 'lodash';
22

3-
import {isCorrectSpec} from '../../helpers';
43
import {Spec} from '../../types';
54

65
import {
@@ -63,9 +62,5 @@ export const Controller = <Value extends FieldValue, SpecType extends Spec>({
6362
__mirror,
6463
);
6564

66-
if (_.isString(name) && isCorrectSpec(spec) && !spec.viewSpec.hidden) {
67-
return withSearch(render(renderProps));
68-
}
69-
70-
return null;
65+
return withSearch(render(renderProps));
7166
};

src/lib/core/components/Form/hooks/__tests__/useSearch.test.tsx

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,7 @@ describe('Form/hooks/useSearch', () => {
6464
'name.surname': true,
6565
'name.username': false,
6666
});
67-
expect(mirror.controller['name.surname']?.useSearch?.(null).props.className).toBe(
68-
'df-use-search',
69-
);
67+
expect(mirror.controller['name.surname']?.useSearch?.(null)).toBe(null);
7068

7169
rerender(
7270
<Form initialValues={value} onSubmit={_.noop}>
@@ -88,8 +86,6 @@ describe('Form/hooks/useSearch', () => {
8886
'name.surname': true,
8987
'name.username': false,
9088
});
91-
expect(mirror.controller['name.surname']?.useSearch?.(null).props.className).toBe(
92-
'df-use-search df-use-search_hidden',
93-
);
89+
expect(mirror.controller['name.surname']?.useSearch?.(null)).toBe(null);
9490
});
9591
});

src/lib/core/components/Form/hooks/useRender.tsx

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,24 +28,28 @@ export const useRender = <Value extends FieldValue, SpecType extends Spec>({
2828
const render = React.useCallback(
2929
(props: FieldRenderProps<Value>) => {
3030
if (inputEntity && isCorrectSpec(spec) && _.isString(name)) {
31-
if (inputEntity.independent) {
32-
const InputComponent = inputEntity.Component;
31+
if (!spec.viewSpec.hidden) {
32+
if (inputEntity.independent) {
33+
const InputComponent = inputEntity.Component;
3334

34-
return <InputComponent spec={spec} name={name} Layout={Layout} {...props} />;
35-
}
35+
return (
36+
<InputComponent spec={spec} name={name} Layout={Layout} {...props} />
37+
);
38+
}
3639

37-
const InputComponent = inputEntity.Component;
38-
const input = <InputComponent spec={spec} name={name} {...props} />;
40+
const InputComponent = inputEntity.Component;
41+
const input = <InputComponent spec={spec} name={name} {...props} />;
3942

40-
if (Layout) {
41-
return (
42-
<Layout spec={spec} name={name} {...props}>
43-
{input}
44-
</Layout>
45-
);
46-
}
43+
if (Layout) {
44+
return (
45+
<Layout spec={spec} name={name} {...props}>
46+
{input}
47+
</Layout>
48+
);
49+
}
4750

48-
return input;
51+
return input;
52+
}
4953
}
5054

5155
return null;

src/lib/core/components/Form/hooks/useSearch/useSearch.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ export const useSearch = (spec: Spec, value: FieldValue, name: string) => {
2020
const hidden = React.useMemo(() => isHiddenField(name), [isHiddenField, name]);
2121

2222
const withSearch = React.useCallback(
23-
(children: JSX.Element | null) => <div className={b({hidden: hidden})}>{children}</div>,
23+
(children: JSX.Element | null) =>
24+
children ? <div className={b({hidden: hidden})}>{children}</div> : null,
2425
[hidden],
2526
);
2627

src/lib/core/components/View/ViewController.tsx

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,5 @@ export const ViewController = <SpecType extends Spec>({
1919
const {viewEntity, Layout} = useComponents(spec, config);
2020
const render = useRender({name, value, spec, viewEntity, Layout, Link});
2121

22-
if (!spec.viewSpec.hidden) {
23-
return <React.Fragment>{render}</React.Fragment>;
24-
}
25-
26-
return null;
22+
return <React.Fragment>{render}</React.Fragment>;
2723
};

src/lib/core/components/View/hooks/useRender.tsx

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -26,45 +26,47 @@ export const useRender = <Value extends FormValue, SpecType extends Spec>({
2626
}: UseRenderParams<Value, SpecType>) => {
2727
const render = React.useMemo(() => {
2828
if (viewEntity && isCorrectSpec(spec) && _.isString(name)) {
29-
const currentValue = name ? _.get(value, name) : value;
30-
const linkValue =
31-
isValidElementType(Link) && spec?.viewSpec?.link ? (
32-
<Link value={currentValue} link={spec.viewSpec.link} />
33-
) : undefined;
29+
if (!spec.viewSpec.hidden) {
30+
const currentValue = name ? _.get(value, name) : value;
31+
const linkValue =
32+
isValidElementType(Link) && spec?.viewSpec?.link ? (
33+
<Link value={currentValue} link={spec.viewSpec.link} />
34+
) : undefined;
3435

35-
if (viewEntity.independent) {
36-
const InputComponent = viewEntity.Component;
36+
if (viewEntity.independent) {
37+
const InputComponent = viewEntity.Component;
38+
39+
return (
40+
<InputComponent
41+
spec={spec}
42+
name={name}
43+
Layout={Layout}
44+
value={currentValue}
45+
linkValue={linkValue}
46+
/>
47+
);
48+
}
3749

38-
return (
50+
const InputComponent = viewEntity.Component;
51+
const input = (
3952
<InputComponent
4053
spec={spec}
4154
name={name}
42-
Layout={Layout}
4355
value={currentValue}
4456
linkValue={linkValue}
4557
/>
4658
);
47-
}
4859

49-
const InputComponent = viewEntity.Component;
50-
const input = (
51-
<InputComponent
52-
spec={spec}
53-
name={name}
54-
value={currentValue}
55-
linkValue={linkValue}
56-
/>
57-
);
60+
if (Layout) {
61+
return (
62+
<Layout spec={spec} name={name} value={currentValue}>
63+
{input}
64+
</Layout>
65+
);
66+
}
5867

59-
if (Layout) {
60-
return (
61-
<Layout spec={spec} name={name} value={currentValue}>
62-
{input}
63-
</Layout>
64-
);
68+
return input;
6569
}
66-
67-
return input;
6870
}
6971

7072
return null;

0 commit comments

Comments
 (0)