Page Menu
Home
Sealhub
Search
Configure Global Search
Log In
Files
F1262289
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
4 KB
Referenced Files
None
Subscribers
None
View Options
diff --git a/src/forms/controls/single-reference.test.ts b/src/forms/controls/single-reference.test.ts
index 77c8005..294148c 100644
--- a/src/forms/controls/single-reference.test.ts
+++ b/src/forms/controls/single-reference.test.ts
@@ -1,62 +1,63 @@
import { Page } from "playwright";
import { collectionTemplate } from "../../templates/collection.js";
import { formTemplate } from "../../templates/form.js";
import { RealAppTest } from "../../test_utils/test-on-real-app.js";
import { getBrowser } from "../../utils/browser-creator.js";
import { expect as PlaywritghtExpect } from "@playwright/test";
describe("single-reference.test", () => {
let page: Page;
before(async () => {
const browser = await getBrowser();
const context = await browser.newContext();
page = await context.newPage();
});
after(async () => {
await page.close();
});
it("creates minimal sealious app and adds a simple route that works", async function () {
const test_app = await RealAppTest.init();
// add possiblity to modify templates (form and collection) and create test case
await test_app.addFile(
"src/back/collections/articles.ts",
collectionTemplate("articles", `title: new FieldTypes.Text(),`)
);
await test_app.addFile(
"src/back/collections/comments.ts",
collectionTemplate(
"comments",
`text: new FieldTypes.Text(),
article: new FieldTypes.SingleReference("articles"),`
)
);
const form_file_path = "src/back/routes/some-form.form.ts";
await test_app.addFile(
form_file_path,
await formTemplate("SomeForm", form_file_path, {
postimport: `import {Comments} from "../collections/collections.js"; import {Fields} from "@sealcode/sealgen";`,
fields: `article: new Fields.CollectionField(true, Comments.fields.article),`,
controls: `new Controls.SingleReferenceDropdown(fields.article, {getLabel: item=>item.get("title")}),`,
validate_method: "",
})
);
await test_app.start();
await test_app.httpPOST(`/api/v1/collections/articles`, { title: "article 1" });
await test_app.httpPOST(`/api/v1/collections/articles`, { title: "article 2" });
await page.goto(`http://localhost:${test_app.app_port}/some-form/`);
await page.locator("html").click();
await PlaywritghtExpect(page.getByLabel("article")).toBeVisible();
await page.getByLabel("article").selectOption({ label: "article 2" });
await page.getByRole("button", { name: "Wyślij" }).click();
+ await page.getByLabel("article").selectOption({ label: "--" }); // "--" means empty value - it should be available, as the field is not required
await test_app.close();
}).timeout(100 * 1000);
});
diff --git a/src/forms/controls/single-reference.ts b/src/forms/controls/single-reference.ts
index fdb1238..179321d 100644
--- a/src/forms/controls/single-reference.ts
+++ b/src/forms/controls/single-reference.ts
@@ -1,36 +1,41 @@
import SealiousSingleReference from "sealious/@types/src/app/base-chips/field-types/single-reference.js";
import { CollectionField } from "../fields/collection-field.js";
import { DropdownOptions, FreeformDropdown } from "./dropdown.js";
import { Collection, CollectionItem } from "sealious";
export class SingleReferenceDropdown<
F extends SealiousSingleReference,
Required extends boolean
> extends FreeformDropdown {
constructor(
public field: CollectionField<F, Required>,
public options: DropdownOptions & {
getLabel?: (item: CollectionItem<Collection>) => string;
} = {
label: field.name,
autosubmit: false,
autocomplete: true,
}
) {
super(
field,
async (ctx) => {
const { items } = await ctx.$app.collections[
field.sealiousField.target_collection
]
.list(ctx.$context)
.fetch();
- return items.map((item) => ({
- value: item.id,
- label: options.getLabel ? options.getLabel(item) : item.id,
- }));
+ return [
+ { value: "", label: "--" },
+ ...items.map((item) => ({
+ value: item.id,
+ label: options.getLabel
+ ? options.getLabel(item)
+ : item.id,
+ })),
+ ];
},
options
);
}
}
File Metadata
Details
Attached
Mime Type
text/x-diff
Expires
Thu, Jan 23, 19:19 (20 h, 16 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
601505
Default Alt Text
(4 KB)
Attached To
Mode
rSGEN sealgen
Attached
Detach File
Event Timeline
Log In to Comment