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: nested params form not working in screen #758

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion packages/sdks/web-component/src/lib/helpers/flowInputs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const flattenFormObject = (obj: any, prefix = '') =>
const v = typeof obj[el] === 'object' ? obj[el] : { value: obj[el] };
const fl = { ...res, [prefix + el]: v, [`form.${prefix}${el}`]: v };
if (el === 'displayName') {
return { ...fl, [`${prefix }fullName`]: v, [`form.${prefix}fullName`]: v };
return { ...fl, [`${prefix}fullName`]: v, [`form.${prefix}fullName`]: v };
}
return fl;
}, []);
Expand Down
21 changes: 17 additions & 4 deletions packages/sdks/web-component/src/lib/helpers/templates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,24 @@ const replaceElementInputs = (
/**
* Get object nested path.
* Examples:
* - getByPath({ { a { b: 'rob' } }, 'a.b') => 'hey rob'
* - getByPath({ { a { b: 'rob' } }, 'a.b') => 'rob'
* - getByPath({ { a { 'b.c': 'rob' } }, 'a.b.c') => 'rob'
* - getByPath({}, 'a.b') => ''
*/
const getByPath = (obj: Record<string, any>, path: string) =>
path.split('.').reduce((prev, next) => prev?.[next] || '', obj);
{
Bars92 marked this conversation as resolved.
Show resolved Hide resolved
if (!path) {
return '';
}
if (obj?.[path]) {
return obj[path];
}
const [first, rest] = path.split(/\.(.*)/s);
if (first && obj?.[first] && !(typeof obj[first] == "object")) {
return obj[first];
}
return getByPath(obj[first], rest || '');
};

/**
* Apply template language on text, based on screen state.
Expand Down Expand Up @@ -114,9 +127,9 @@ const replaceHrefByDataType = (

const setFormConfigValues = (
baseEle: DocumentFragment,
formData: Record<string, string>,
formData?: Record<string, string>,
) => {
Object.entries(formData).forEach(([name, config]) => {
Object.entries(formData || {}).forEach(([name, config]) => {
const eles = baseEle.querySelectorAll(`[name="${name}"]`);

eles.forEach((ele) => {
Expand Down
12 changes: 12 additions & 0 deletions packages/sdks/web-component/test/helpers/templates.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { screen } from 'shadow-dom-testing-library';
import {
setNOTPVariable,
updateScreenFromScreenState,
updateTemplateFromScreenState,
} from '../../src/lib/helpers/templates';

describe('templates', () => {
Expand Down Expand Up @@ -34,6 +35,17 @@ describe('templates', () => {
await waitFor(() => screen.getByShadowDisplayValue('email2'));
});

it('should handle nested descope form', async () => {
document.body.innerHTML = `<div>
<descope-text class="descope-text">{{form.test.another}}</descope-text>
</div>`;

updateTemplateFromScreenState(document, {
form: { test: { another: "value" } } as any,
});
await waitFor(() => screen.getByShadowText('value'));
});

it('should set NOTP variable', async () => {
document.body.innerHTML = `<div> </div>`;
const image = 'image';
Expand Down
Loading