Skip to content

Commit

Permalink
Merge branch 'carousel' into alpha
Browse files Browse the repository at this point in the history
  • Loading branch information
ecarreras committed Dec 11, 2024
2 parents e95e61a + 33eb726 commit e6675a3
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src/Carousel.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import ContainerWidget from "./ContainerWidget";
import Group from "./Group";
import { parseBoolAttribute } from "./helpers/nodeParser";

class Carousel extends ContainerWidget {
_autoPlay = true;

get autoPlay(): boolean {
return this._autoPlay;
}

set autoPlay(value: boolean) {
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 ("auto_play" in props) {
this._autoPlay = parseBoolAttribute(props.auto_play);
}
}
}
}

export default Carousel;
4 changes: 4 additions & 0 deletions src/WidgetFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import Comments from "./Comments";
import JSONField from "./JSONField";
import Email from "./Email";
import Spinner from "./Spinner";
import Carousel from "./Carousel";

class WidgetFactory {
/**
Expand Down Expand Up @@ -184,6 +185,9 @@ class WidgetFactory {
case "spinner":
this._widgetClass = Spinner;
break;
case "carousel":
this._widgetClass = Carousel;
break;
default:
break;
}
Expand Down
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ import JSONField from "./JSONField";
import Comments from "./Comments";
import Email from "./Email";
import Spinner from "./Spinner";
import Carousel from "./Carousel";

import {
Graph,
Expand Down Expand Up @@ -142,4 +143,5 @@ export {
JSONField,
Email,
Spinner,
Carousel,
};
75 changes: 75 additions & 0 deletions src/spec/Carousel.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import WidgetFactory from "../WidgetFactory";
import Form from "../Form";
import Carousel from "../Carousel";
import { it, expect, describe } from "vitest";

describe("A Carousel", () => {
it("should have an id corresponding to field name", () => {
const widgetFactory = new WidgetFactory();
const props = {
name: "carousel",
};

const widget = widgetFactory.createWidget("carousel", props);
expect(widget).toBeInstanceOf(Carousel);
});
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(true);
});
it("should allow autoPlay to be set", () => {
const widgetFactory = new WidgetFactory();
const props = {
name: "carousel",
auto_play: false,
};
const widget = widgetFactory.createWidget("carousel", props);
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 e6675a3

Please sign in to comment.