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

EDU-159: Remix Gen1 - Advanced child sub components #3750

Open
wants to merge 5 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
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { test, verifyTabContent } from '../helpers/index.js';

test.describe('Advanced child sub components', () => {
test('Display two buttons with label Tab 1 and Tab 2', async ({ page, packageName }) => {
test.skip(!['react', 'angular', 'angular-ssr'].includes(packageName));
test.skip(!['react', 'angular', 'angular-ssr', 'gen1-remix'].includes(packageName));

await page.goto('/advanced-child');

Expand Down
21 changes: 20 additions & 1 deletion packages/sdks-tests/src/snippet-tests/custom-child.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { test } from '../helpers/index.js';

test.describe('Div with Hero class, and text', () => {
test('should render the page without 404', async ({ page, packageName }) => {
test.skip(!['react', 'angular', 'angular-ssr'].includes(packageName));
test.skip(!['react', 'angular', 'angular-ssr', 'gen1-remix'].includes(packageName));

const response = await page.goto('/custom-child');
expect(response?.status()).toBeLessThan(400);
Expand Down Expand Up @@ -31,4 +31,23 @@ test.describe('Div with Hero class, and text', () => {
const builderText = await builderTextDiv.textContent();
expect(builderText?.trim()).toBe('This is Builder text');
});

test('should verify builder-block with specific text in gen1-remix', async ({
page,
packageName,
}) => {
test.skip(!['gen1-remix'].includes(packageName));

await page.goto('/custom-child');
await page.waitForLoadState('networkidle');

const builderBlock = page.locator('div.builder-block').first();
await expect(builderBlock).toBeVisible();

const column1Text = page.locator("text=This is your component's text");
await expect(column1Text).toBeVisible();

const column2Text = page.locator('text=This is Builder text');
await expect(column2Text).toBeVisible();
});
});
15 changes: 15 additions & 0 deletions packages/sdks-tests/src/snippet-tests/editable-regions.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,19 @@ test.describe('Editable regions in custom components', () => {
const secondText = await columns.nth(1).textContent();
expect(secondText?.trim().toLowerCase()).toBe('column 2 text');
});

test.describe('Remix gen1 editable regions text validation', () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

why does remix gen1= have its own special test here? why aren't we enabling the existing tests in this spec for remix?

test('should display column texts directly on the screen', async ({ page, packageName }) => {
test.skip(!['gen1-remix'].includes(packageName));

await page.goto('/editable-region');
await page.waitForLoadState('networkidle');

const column1Text = page.locator('text=column 1 text');
await expect(column1Text).toBeVisible();

const column2Text = page.locator('text=column 2 text');
await expect(column2Text).toBeVisible();
});
});
});
48 changes: 48 additions & 0 deletions packages/sdks/snippets/gen1-remix/app/routes/advanced-child.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// routes/advanced-child.tsx
import { Builder, BuilderComponent, builder } from '@builder.io/react';
import type { LoaderFunctionArgs } from '@remix-run/node';
import { useLoaderData } from '@remix-run/react';
import CustomTabs from './components/CustomTabs';

builder.init('ee9f13b4981e489a9a1209887695ef2b');

Builder.registerComponent(CustomTabs, {
name: 'TabFields',
inputs: [
{
name: 'tabList',
type: 'array',
defaultValue: [],
subFields: [
{
name: 'tabName',
type: 'string',
},
{
name: 'blocks',
type: 'uiBlocks',
hideFromUI: true,
defaultValue: [],
},
],
},
],
});

export const loader = async ({ request }: LoaderFunctionArgs) => {
const page = await builder
.get('advanced-child', {
userAttributes: {
urlPath: `/${request.url.split('/').pop()}`,
},
})
.toPromise();

return { page };
};

export default function AdvancedChildPage() {
const { page } = useLoaderData<typeof loader>();

return <BuilderComponent model="advanced-child" content={page} />;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { BuilderBlocks, BuilderElement } from '@builder.io/react';

interface CustomColumnsProps {
column1: { blocks: React.ReactNode };
column2: { blocks: React.ReactNode };
builderBlock: BuilderElement;
}

export const CustomColumns = (props: CustomColumnsProps) => {
return (
<>
<BuilderBlocks
parentElementId={props.builderBlock.id}
dataPath={`column1.blocks`}
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
dataPath={`column1.blocks`}
dataPath="column1.blocks"

blocks={props.column1?.blocks}
/>

<BuilderBlocks
parentElementId={props.builderBlock.id}
dataPath={`column2.blocks`}
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
dataPath={`column2.blocks`}
dataPath="column2.blocks"

blocks={props.column2?.blocks}
/>
</>
);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { ReactNode } from 'react';

interface CustomHeroProps {
children: ReactNode;
}

export const CustomHero = (props: CustomHeroProps) => {
Comment on lines +1 to +7
Copy link
Contributor

@samijaber samijaber Nov 21, 2024

Choose a reason for hiding this comment

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

Suggested change
import { ReactNode } from 'react';
interface CustomHeroProps {
children: ReactNode;
}
export const CustomHero = (props: CustomHeroProps) => {
import { type PropsWithChildren } from 'react';
export const CustomHero = (props: PropsWithChildren) => {

return (
<>
<div>This is your component&apos;s text</div>
Copy link
Contributor

Choose a reason for hiding this comment

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

&apos; here is not pretty. can we use a '?

{props.children}
</>
);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { BuilderBlocks, BuilderElement } from '@builder.io/react';

import { useState } from 'react';

type TabProps = {
tabList: { tabName: string; blocks: React.ReactNode[] }[];
builderBlock: BuilderElement;
};

export default function CustomTabs({ tabList, builderBlock }: TabProps) {
const [activeTab, setActiveTab] = useState(0);

if (!tabList?.length) return null;

return (
<>
<div className="tab-buttons">
{tabList.map((tab, index) => (
<button
key={index}
className={`tab-button ${activeTab === index ? 'active' : ''}`}
onClick={() => setActiveTab(index)}
>
{tab.tabName}
</button>
))}
</div>

<BuilderBlocks
parentElementId={builderBlock?.id}
dataPath={`tabList.${activeTab}.blocks`}
blocks={tabList[activeTab].blocks}
/>
</>
);
}
42 changes: 42 additions & 0 deletions packages/sdks/snippets/gen1-remix/app/routes/custom-child.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { Builder, builder, BuilderComponent } from '@builder.io/react';

import { LoaderFunctionArgs } from '@remix-run/node';
import { useLoaderData } from '@remix-run/react';
import { CustomHero } from './components/CustomHero';

builder.init('ee9f13b4981e489a9a1209887695ef2b');

Builder.registerComponent(CustomHero, {
name: 'CustomHero',
inputs: [],
Copy link
Contributor

Choose a reason for hiding this comment

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

We don't need to provide an empty array, we can drop this altogether. If you can confirm that, go ahead and remove all empty inputs: []

canHaveChildren: true,
defaultChildren: [
{
'@type': '@builder.io/sdk:Element',
component: {
name: 'Text',
options: {
text: 'This is Builder text',
},
},
},
],
});

export const loader = async ({ request }: LoaderFunctionArgs) => {
const page = await builder
.get('custom-child', {
userAttributes: {
urlPath: `/${request.url.split('/').pop()}`,
},
})
.toPromise();

return { page };
};

export default function CustomChildPage() {
const { page } = useLoaderData<typeof loader>();

return <BuilderComponent model="custom-child" content={page} />;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// routes/advanced-child.tsx
import { Builder, BuilderComponent, builder } from "@builder.io/react";
import type { LoaderFunctionArgs } from "@remix-run/node";
import { useLoaderData } from "@remix-run/react";
import { CustomColumns } from "./components/CustomColumns";

builder.init("ee9f13b4981e489a9a1209887695ef2b");

Builder.registerComponent(CustomColumns, {
name: "MyColumns",
inputs: [
{
name: "column1",
type: "uiBlocks",
broadcast: true,
hideFromUI: true,
Copy link
Contributor

Choose a reason for hiding this comment

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

i don't think we need hideFromUI for uiBlocks input types. Test that everything works as expected in the editor (that the UI block doesn't show up as an input in the Content Editor sidebase). If that's the case, let's remove it from everywhere in the snippets (this PR and elsewhere)

defaultValue: {
blocks: [],
},
},
{
name: "column2",
type: "uiBlocks",
broadcast: true,
hideFromUI: true,
defaultValue: {
blocks: [],
},
},
],
});

export const loader = async ({ request }: LoaderFunctionArgs) => {
const page = await builder
.get("editable-regions", {
userAttributes: {
urlPath: `/${request.url.split("/").pop()}`,
Copy link
Contributor

Choose a reason for hiding this comment

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

instead of request.url.split("/").pop(), we can rely on remix's file naming conventions to extract the URL path as a param. See

};
export const loader: LoaderFunction = async ({ params }) => {
const { initializeNodeRuntime } = await import(
'@builder.io/sdk-react/node/init'
);
await initializeNodeRuntime();
return await getProps({ pathname: `/${params.slug || ''}` });
};

by naming the file ($slug).tsx, remix will automatically pass the URL path value to params.slug. This is far cleaner as an approach, so let's do things this way for Remix moving forward

Copy link
Contributor

@samijaber samijaber Nov 21, 2024

Choose a reason for hiding this comment

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

We do it in the snippets already too. See:

export const loader = async ({ params, request }: LoaderFunctionArgs) => {
const announcementBar = await builder
.get('announcement-bar', {
userAttributes: {
urlPath: `/announcements/${params.slug ? params.slug : ''}`,
},
})
.toPromise();
const isPreviewing = new URL(request.url).searchParams.has('builder.preview');

Copy link
Contributor

Choose a reason for hiding this comment

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

i just realized this suggestion of mine doesn't work because the routes you added in this PR are hardcoded with exact URLs 🤔

forget what i said for now, and let's stick with your approach

},
})
.toPromise();

return { page };
};

export default function EditableRegionsPage() {
const { page } = useLoaderData<typeof loader>();

return <BuilderComponent model="editable-regions" content={page} />;
}
Loading