Skip to content

Commit

Permalink
Merge pull request #58 from gisce/button_grup
Browse files Browse the repository at this point in the history
Button grup
  • Loading branch information
mguellsegarra authored Aug 5, 2022
2 parents 8d7627d + 2119287 commit df6660d
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 1 deletion.
31 changes: 31 additions & 0 deletions src/ButtonGroup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import Button from "./Button";
import ContainerWidget from "./ContainerWidget";

class ButtonGroup extends ContainerWidget {

_defaultName: string = "";
get defaultName(): string {
return this._defaultName;
}

get defaultButton(): Button | undefined {
return this.buttons.find(button => button.id === this.defaultName);
}

get secondaryButtons(): Button[] {
return this.buttons.filter(button => button.id !== this.defaultName);
}

get buttons(): Button[] {
return this._container.rows[0] as Button[];
}

constructor(props: any) {
super(props);
if (props.default) {
this._defaultName = props.default;
}
}
}

export default ButtonGroup;
4 changes: 4 additions & 0 deletions src/WidgetFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import Notebook from "./Notebook";
import Page from "./Page";
import Group from "./Group";
import Button from "./Button";
import ButtonGroup from "./ButtonGroup";
import Label from "./Label";
import Char from "./Char";
import Text from "./Text";
Expand Down Expand Up @@ -61,6 +62,9 @@ class WidgetFactory {
case "button":
this._widgetClass = Button;
break;
case "buttonGroup":
this._widgetClass = ButtonGroup;
break;
case "selection":
this._widgetClass = Selection;
break;
Expand Down
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import Group from "./Group";
import Page from "./Page";
import Separator from "./Separator";
import Button from "./Button";
import ButtonGroup from "./ButtonGroup";
import Reference from "./Reference";
import Binary from "./Binary";
import Image from "./Image";
Expand Down Expand Up @@ -85,6 +86,7 @@ export {
Label,
Separator,
Button,
ButtonGroup,
Reference,
Binary,
Image,
Expand Down
43 changes: 43 additions & 0 deletions src/spec/ButtonGroup.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import ButtonGroup from "../ButtonGroup";
import WidgetFactory from "../WidgetFactory";
import Button from "../Button";

describe("A ButtonsGroup widget", () => {
it("should have an id corresponding to button name", () => {
const widgetFactory = new WidgetFactory();
const props = {
name: "defaultButtonGroup",
};

const widget = widgetFactory.createWidget("buttonGroup", props);
expect(widget.id).toBe("defaultButtonGroup");
});

describe("Getting child buttons", () => {

let buttonGroup: ButtonGroup;

beforeAll(() => {
const btn1 = new Button({name: "btn1", type: "object", icon: "gtk-execute", string: "Button 1"});
const btn2 = new Button({name: "btn2", type: "object", icon: "gtk-execute", string: "Button 2"});
const btn3 = new Button({name: "btn3", type: "object", icon: "gtk-execute", string: "Button 3"});
buttonGroup = new ButtonGroup({name: "btnGroup", default: "btn1"});
buttonGroup.container.rows[0].push(...[btn1, btn2, btn3]);
})

it("should have child buttons", () => {
expect(buttonGroup.buttons).toHaveLength(3);
});

it("should have this default button", () => {
expect(buttonGroup.defaultButton?.id).toBe("btn1");
});

it("should have this secondary buttons", () => {
expect(buttonGroup.secondaryButtons).toHaveLength(2);
expect(buttonGroup.secondaryButtons[0].id).toBe("btn2");
expect(buttonGroup.secondaryButtons[1].id).toBe("btn3");
});

});
});
23 changes: 23 additions & 0 deletions src/spec/Form.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import Label from "../Label";
import Field from "../Field";
import Reference from "../Reference";
import Button from "../Button";
import ButtonGroup from "../ButtonGroup";

const XML_VIEW_FORM = `<?xml version="1.0"?>
<form string="Partner Address">
Expand Down Expand Up @@ -756,6 +757,28 @@ describe("A Form", () => {
expect(field.buttonType).toBe("object");
});

describe("A ButtonGroup", () => {
it("should be able to parse a ButtonGroup", () => {
const fields = {};
const xmlViewForm = `<?xml version="1.0"?>
<form string="Form1">
<buttonGroup name="aButtonGroup" default="main">
<button name="main" type="object" string="Main action" />
<button name="secondary" type="object" string="Secondary action" />
</buttonGroup>
</form>`;
const form = new Form(fields);
form.parse(xmlViewForm);

const buttonGroup = form.container.rows[0][0] as ButtonGroup;
expect(buttonGroup).toBeInstanceOf(ButtonGroup);
expect(buttonGroup.buttons).toHaveLength(2);
buttonGroup.buttons.forEach((button) => {
expect(button).toBeInstanceOf(Button);
})
});
});

it("should be able to parse a Button by default to type workflow", () => {
const fields = {
button: {
Expand Down
13 changes: 12 additions & 1 deletion src/spec/WidgetFactory.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import WidgetFactory from "../WidgetFactory";
import { Char, Button, NewLine, Tags, MultiCheckbox, Radio, Switch, Steps, CodeEditor } from "..";
import {
Char, Button, NewLine, Tags, MultiCheckbox,
Radio, Switch, Steps, CodeEditor, ButtonGroup
} from "..";


describe('A WidgetFactory', () => {
Expand Down Expand Up @@ -83,4 +86,12 @@ describe('A WidgetFactory', () => {
expect(widget).toBeInstanceOf(CodeEditor);
expect(widget.type).toBe("codeeditor");
});

it('should be able to retrieve ButtonGroup type', () => {
const widgetFactory = new WidgetFactory();
const props = {};
const widget = widgetFactory.createWidget("buttonGroup", props);
expect(widget).toBeInstanceOf(ButtonGroup);
expect(widget.type).toBe("buttonGroup");
});
});

0 comments on commit df6660d

Please sign in to comment.