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

support subFields showIf #2586

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
50 changes: 30 additions & 20 deletions packages/core/src/builder.class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,13 @@ import { omit } from './functions/omit.function';
import { getTopLevelDomain } from './functions/get-top-level-domain';
import { BuilderContent } from './types/content';
import { uuid } from './functions/uuid';
import { parse as urlParse } from './url';
import { emptyUrl, parse as urlParse, UrlLike } from './url';

// Do not change this to a require! It throws runtime errors - rollup
// will preserve the `require` and throw runtime errors
import hash from 'hash-sum';
import { toError } from './functions/to-error';
import { emptyUrl, UrlLike } from './url';
import { DEFAULT_API_VERSION, ApiVersion } from './types/api-version';
import { ApiVersion, DEFAULT_API_VERSION } from './types/api-version';

export type Url = any;

Expand Down Expand Up @@ -1027,27 +1026,38 @@ export class Builder {
}
}

private static convertFnToString({
input,
keysToConvert,
}: {
input: Input;
keysToConvert: string[];
}) {
for (const key of keysToConvert) {
if (input[key] && typeof input[key] === 'function') {
const fn = input[key];
input = {
...input,
[key]: `return (${fn.toString()}).apply(this, arguments)`,
...(input.subFields == null
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there might be a lint rule type coercion with double equeals, maybe we do this instead?

  ...(Array.isArray(input.subFields)
    ? {
        subFields: input.subFields.map((input) => {
          return this.convertFnToString({ keysToConvert, input });
        }),
      }
    : {}),

? null
: {
subFields: input.subFields.map(input => {
return this.convertFnToString({ keysToConvert, input });
}),
}),
};
}
}
return input;
}

private static prepareComponentSpecToSend(spec: Component): Component {
return {
...spec,
...(spec.inputs && {
inputs: spec.inputs.map((input: any) => {
// TODO: do for nexted fields too
// TODO: probably just convert all functions, not just
// TODO: put this in input hooks: { onChange: ..., showIf: ... }
const keysToConvertFnToString = ['onChange', 'showIf'];

for (const key of keysToConvertFnToString) {
if (input[key] && typeof input[key] === 'function') {
const fn = input[key];
input = {
...input,
[key]: `return (${fn.toString()}).apply(this, arguments)`,
};
}
}

return input;
inputs: spec.inputs.map(input => {
return this.convertFnToString({ keysToConvert: ['onChange', 'showIf'], input });
}),
}),
hooks: Object.keys(spec.hooks || {}).reduce((memo, key) => {
Expand Down