Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add spec correctness check for view controller #127

Merged
merged 1 commit into from
Oct 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 1 addition & 6 deletions src/lib/core/components/Form/Controller.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import _ from 'lodash';

import {isCorrectSpec} from '../../helpers';
import {Spec} from '../../types';

import {
Expand Down Expand Up @@ -63,9 +62,5 @@ export const Controller = <Value extends FieldValue, SpecType extends Spec>({
__mirror,
);

if (_.isString(name) && isCorrectSpec(spec) && !spec.viewSpec.hidden) {
return withSearch(render(renderProps));
}

return null;
return withSearch(render(renderProps));
};
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,7 @@ describe('Form/hooks/useSearch', () => {
'name.surname': true,
'name.username': false,
});
expect(mirror.controller['name.surname']?.useSearch?.(null).props.className).toBe(
'df-use-search',
);
expect(mirror.controller['name.surname']?.useSearch?.(null)).toBe(null);

rerender(
<Form initialValues={value} onSubmit={_.noop}>
Expand All @@ -88,8 +86,6 @@ describe('Form/hooks/useSearch', () => {
'name.surname': true,
'name.username': false,
});
expect(mirror.controller['name.surname']?.useSearch?.(null).props.className).toBe(
'df-use-search df-use-search_hidden',
);
expect(mirror.controller['name.surname']?.useSearch?.(null)).toBe(null);
});
});
32 changes: 18 additions & 14 deletions src/lib/core/components/Form/hooks/useRender.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,24 +28,28 @@ export const useRender = <Value extends FieldValue, SpecType extends Spec>({
const render = React.useCallback(
(props: FieldRenderProps<Value>) => {
if (inputEntity && isCorrectSpec(spec) && _.isString(name)) {
if (inputEntity.independent) {
const InputComponent = inputEntity.Component;
if (!spec.viewSpec.hidden) {
if (inputEntity.independent) {
const InputComponent = inputEntity.Component;

return <InputComponent spec={spec} name={name} Layout={Layout} {...props} />;
}
return (
<InputComponent spec={spec} name={name} Layout={Layout} {...props} />
);
}

const InputComponent = inputEntity.Component;
const input = <InputComponent spec={spec} name={name} {...props} />;
const InputComponent = inputEntity.Component;
const input = <InputComponent spec={spec} name={name} {...props} />;

if (Layout) {
return (
<Layout spec={spec} name={name} {...props}>
{input}
</Layout>
);
}
if (Layout) {
return (
<Layout spec={spec} name={name} {...props}>
{input}
</Layout>
);
}

return input;
return input;
}
}

return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ export const useSearch = (spec: Spec, value: FieldValue, name: string) => {
const hidden = React.useMemo(() => isHiddenField(name), [isHiddenField, name]);

const withSearch = React.useCallback(
(children: JSX.Element | null) => <div className={b({hidden: hidden})}>{children}</div>,
(children: JSX.Element | null) =>
children ? <div className={b({hidden: hidden})}>{children}</div> : null,
[hidden],
);

Expand Down
6 changes: 1 addition & 5 deletions src/lib/core/components/View/ViewController.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,5 @@ export const ViewController = <SpecType extends Spec>({
const {viewEntity, Layout} = useComponents(spec, config);
const render = useRender({name, value, spec, viewEntity, Layout, Link});

if (!spec.viewSpec.hidden) {
return <React.Fragment>{render}</React.Fragment>;
}

return null;
return <React.Fragment>{render}</React.Fragment>;
};
56 changes: 29 additions & 27 deletions src/lib/core/components/View/hooks/useRender.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,45 +26,47 @@ export const useRender = <Value extends FormValue, SpecType extends Spec>({
}: UseRenderParams<Value, SpecType>) => {
const render = React.useMemo(() => {
if (viewEntity && isCorrectSpec(spec) && _.isString(name)) {
const currentValue = name ? _.get(value, name) : value;
const linkValue =
isValidElementType(Link) && spec?.viewSpec?.link ? (
<Link value={currentValue} link={spec.viewSpec.link} />
) : undefined;
if (!spec.viewSpec.hidden) {
const currentValue = name ? _.get(value, name) : value;
const linkValue =
isValidElementType(Link) && spec?.viewSpec?.link ? (
<Link value={currentValue} link={spec.viewSpec.link} />
) : undefined;

if (viewEntity.independent) {
const InputComponent = viewEntity.Component;
if (viewEntity.independent) {
const InputComponent = viewEntity.Component;

return (
<InputComponent
spec={spec}
name={name}
Layout={Layout}
value={currentValue}
linkValue={linkValue}
/>
);
}

return (
const InputComponent = viewEntity.Component;
const input = (
<InputComponent
spec={spec}
name={name}
Layout={Layout}
value={currentValue}
linkValue={linkValue}
/>
);
}

const InputComponent = viewEntity.Component;
const input = (
<InputComponent
spec={spec}
name={name}
value={currentValue}
linkValue={linkValue}
/>
);
if (Layout) {
return (
<Layout spec={spec} name={name} value={currentValue}>
{input}
</Layout>
);
}

if (Layout) {
return (
<Layout spec={spec} name={name} value={currentValue}>
{input}
</Layout>
);
return input;
}

return input;
}

return null;
Expand Down
Loading