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

Update Template DSL to support placement prop #196

Open
wants to merge 1 commit 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
29 changes: 28 additions & 1 deletion language/dsl/src/__tests__/template.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ test("works if already in a template array", async () => {
});
});

test("Template Order is preserved", async () => {
test("Template order is preserved", async () => {
const element = (
<Collection id="view-1">
<Collection.Values>
Expand Down Expand Up @@ -183,6 +183,33 @@ test("Template Order is preserved", async () => {
});
});

test("Template placement is passed in", async () => {
const element = (
<obj>
<property name="foo">
<array>
<value>Foo</value>
<Template dynamic data={b`foo.output`} placement="append">
<value>bar</value>
</Template>
</array>
</property>
</obj>
);
expect((await render(element)).jsonValue).toStrictEqual({
foo: ["Foo"],
template: [
{
data: "foo.output",
placement: "append",
dynamic: true,
value: "bar",
output: "foo",
},
],
});
});

test("template will delete empty arrays related to the template only", async () => {
const element = (
<Collection>
Expand Down
18 changes: 14 additions & 4 deletions language/dsl/src/template.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ export interface TemplateContextType {
depth: number;
}

export const TemplateContext = React.createContext<TemplateContextType>({
depth: 0,
});
export const TemplateContext: React.Context<TemplateContextType> =
React.createContext<TemplateContextType>({
depth: 0,
});

export interface TemplateProps {
/** The source binding */
Expand All @@ -34,6 +35,9 @@ export interface TemplateProps {

/** boolean that specifies whether template should recompute when data changes */
dynamic?: boolean;

/** Placement method */
placement?: "append" | "prepend";
}

/** Add a template instance to the object */
Expand Down Expand Up @@ -134,9 +138,10 @@ const getParentProperty = (node: JsonNode): PropertyNode | undefined => {
};

/** A template allows users to dynamically map over an array of data */
export const Template = (props: TemplateProps) => {
export const Template = (props: TemplateProps): React.JSX.Element => {
const baseContext = React.useContext(TemplateContext);
const dynamicProp = props.dynamic ?? false;
const placementProp = props.placement ?? false;
const [outputProp, setOutputProp] = React.useState<string | undefined>(
props.output
);
Expand Down Expand Up @@ -218,6 +223,11 @@ export const Template = (props: TemplateProps) => {
{toJsonElement(dynamicProp)}
</property>
)}
{placementProp && (
<property name="placement">
{toJsonElement(placementProp)}
</property>
)}
</object>
</TemplateProvider>
</OptionalIDSuffixProvider>,
Expand Down