Skip to content

Commit

Permalink
feat(carousel): add items
Browse files Browse the repository at this point in the history
  • Loading branch information
ecarreras committed Dec 11, 2024
1 parent ef6733b commit 33eb726
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 7 deletions.
12 changes: 9 additions & 3 deletions src/Carousel.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import ContainerWidget from "./ContainerWidget";
import Group from "./Group";
import { parseBoolAttribute } from "./helpers/nodeParser";

class Carousel extends ContainerWidget {
_autoPlay = false;
_autoPlay = true;

get autoPlay(): boolean {
return this._autoPlay;
Expand All @@ -11,11 +13,15 @@ class Carousel extends ContainerWidget {
this._autoPlay = value;
}

get items(): Group[] {
return this._container.rows.flat().filter((g) => !g.invisible) as Group[];
}

constructor(props?: any) {
super(props);
if (props) {
if (props.auto_play) {
this._autoPlay = props.auto_play;
if ("auto_play" in props) {
this._autoPlay = parseBoolAttribute(props.auto_play);
}
}
}
Expand Down
51 changes: 47 additions & 4 deletions src/spec/Carousel.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import WidgetFactory from "../WidgetFactory";
import Form from "../Form";
import Carousel from "../Carousel";
import { it, expect, describe } from "vitest";

Expand All @@ -12,21 +13,63 @@ describe("A Carousel", () => {
const widget = widgetFactory.createWidget("carousel", props);
expect(widget).toBeInstanceOf(Carousel);
});
it("should have autoPlay as false by default", () => {
it("should have autoPlay as true by default", () => {
const widgetFactory = new WidgetFactory();
const props = {
name: "carousel",
};
const widget = widgetFactory.createWidget("carousel", props);
expect(widget.autoPlay).toBe(false);
expect(widget.autoPlay).toBe(true);
});
it("should allow autoPlay to be set", () => {
const widgetFactory = new WidgetFactory();
const props = {
name: "carousel",
auto_play: true,
auto_play: false,
};
const widget = widgetFactory.createWidget("carousel", props);
expect(widget.autoPlay).toBe(true);
expect(widget.autoPlay).toBe(false);
});
it("should have items with the first childs group items", () => {
const xml = `
<form>
<carousel name="carousel">
<group string="Group 1">
<field name="field1" string="Field 1" />
</group>
<group string="Group 2">
<field name="field2" string="Field 2" />
<group string="Group 3">
<field name="field3" string="Field 3" />
</group>
</group>
</carousel>
</form>
`;
const fields = {
field1: {
string: "Field 1",
type: "char",
size: 10,
},
field2: {
string: "Field 2",
type: "char",
size: 10,
},
field3: {
string: "Field 3",
type: "char",
size: 10,
},
};

const form = new Form(fields);
form.parse(xml);
const carousel = form.findById("carousel") as Carousel;
expect(carousel).toBeInstanceOf(Carousel);
expect(carousel.items.length).toBe(2);
expect(carousel.items[0].label).toBe("Group 1");
expect(carousel.items[1].label).toBe("Group 2");
});
});

0 comments on commit 33eb726

Please sign in to comment.