Skip to content

Commit

Permalink
[accel-web] fix namespace bug in formFor()
Browse files Browse the repository at this point in the history
  • Loading branch information
koyopro committed Dec 3, 2024
1 parent be940e3 commit 50d30b5
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/cyan-apricots-cry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"accel-web": patch
---

Fixed the issue where the namespace passed to formFor() was not being reflected correctly.
5 changes: 2 additions & 3 deletions packages/accel-web/src/form/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,9 @@ export const formFor = (resource: any, options?: FormForOptions) => {
};

const getPrefix = (resource: any, options?: FormForOptions) => {
if (options?.namespace) return options.namespace;
const name = resource.class?.()?.name;
if (name) return `${toCamelCase(name)}.`;
return "";
const namespace = options?.namespace || (name ? toCamelCase(name) : undefined);
return namespace ? `${namespace}.` : "";
};

export const extendCommponent = <L extends keyof astroHTML.JSX.DefinedIntrinsicElements, P>(
Expand Down
13 changes: 13 additions & 0 deletions packages/accel-web/tests/MyFormWithNamespace.astro
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
import { FormModel } from "accel-record";
import { attributes } from "accel-record/attributes";
import { formFor } from "accel-web";
class SampleForm extends FormModel {
count = attributes.integer(0);
}
const form = SampleForm.build({});
const { Label } = formFor(form, { namespace: "q" });
---

<Label for="count" />
8 changes: 8 additions & 0 deletions packages/accel-web/tests/formFor.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
import { experimental_AstroContainer as AstroContainer } from "astro/container";
import MyForm from "./MyForm.astro";
import MyFormWithNamespace from "./MyFormWithNamespace.astro";

test("formFor() with FormModel", async () => {
const container = await AstroContainer.create();
const result = await container.renderToString(MyForm);

expect(result).toContain('htmlFor="sampleForm.count"');
});

test("formFor() with namespace", async () => {
const container = await AstroContainer.create();
const result = await container.renderToString(MyFormWithNamespace);

expect(result).toContain('htmlFor="q.count"');
});

0 comments on commit 50d30b5

Please sign in to comment.