Skip to content

Commit

Permalink
fix toggle group
Browse files Browse the repository at this point in the history
  • Loading branch information
FredericHeem committed May 21, 2024
1 parent bc3216b commit d304115
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,7 @@ export default (context: Context) => {
const onsubmit = (event: any) => {
event.preventDefault();
const formEl = event.currentTarget;
const buttonName = formEl.querySelector(
"button[aria-pressed=true]"
)?.name;
const buttonName = formEl.querySelector("button[selected=true]")?.name;
alert(buttonName);
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export default (context: Context) => {
event.preventDefault();
const formEl = event.currentTarget;
const buttonNames = [
...formEl.querySelectorAll("button[aria-pressed=true]"),
...formEl.querySelectorAll("button[selected=true]"),
].map(({ name }: any) => name);
alert(JSON.stringify(buttonNames));
};
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import { Context } from "@grucloud/bau-ui/context";
import toggleGroup from "@grucloud/bau-ui/toggleGroup";
import toggle from "@grucloud/bau-ui/toggle";
import button from "@grucloud/bau-ui/button";

export default (context: Context) => {
const { bau, window } = context;
const { form, footer, article } = bau.tags;

const toggleGroupName = "my-toggle-group";
const groups = [
{ value: "one", label: "ONE" },
{ value: "two", label: "TWO" },
{ value: "three", label: "THREE" },
];

const color = "primary";
const variant = "solid";

const Toggle = toggle(context, { color, variant });
const ToggleGroup = toggleGroup(context, { color, variant });
const Button = button(context, {
variant: "outline",
color: "primary",
});

return () => {
const search = new URLSearchParams(window.location.search);

const selectedState = bau.state([...search.getAll(toggleGroupName)]);

const onChange = ({ values }: any) => {
selectedState.val = values;
const search = new URLSearchParams(window.location.search);
search.delete(toggleGroupName);
values.forEach((value: string) => search.append(toggleGroupName, value));
window.history.replaceState(
"",
"",
`?${search.toString()}${window.location.hash}`
);
};

const onsubmit = (event: any) => {
event.preventDefault();
const search = new URLSearchParams(window.location.search);
alert(search.getAll(toggleGroupName));
};

return form(
{ onsubmit },
article(
ToggleGroup(
{ name: toggleGroupName, exclusive: true, onChange },
groups.map(
({ label, value }) =>
() =>
Toggle(
{
value,
name: label,
selected: selectedState.val.includes(value),
},
label
)
)
)
),
footer(Button({ type: "submit" }, "Submit"))
);
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,18 @@ import pageExample from "../pageExample.ts";

import toggleGroupGridItem from "./toggleGroup-grid-item.ts";

import toggleGroupDefault from "./toggleGroup-example-default.ts";
import toggleGroupExclusive from "./toggleGroup-exclusive.ts";
// @ts-ignore
import codeExampleDefault from "./toggleGroup-example-default.ts?raw";
import codeExampleExclusive from "./toggleGroup-exclusive.ts?raw";

import toggleGroupInclusive from "./toggleGroup-inclusive.ts";
// @ts-ignore
import codeExampleInclusive from "./toggleGroup-inclusive.ts?raw";

import toggleGroupUrl from "./toggleGroup-url.ts";
// @ts-ignore
import codeExampleUrl from "./toggleGroup-url.ts?raw";

export const toggleGroupSpec = {
title: "Toggle Group",
package: "toggleGroup",
Expand All @@ -23,15 +27,21 @@ export const toggleGroupSpec = {
{
title: "Exclusive ToggleGroup",
description: "A simple exclusive toggleGroup.",
code: codeExampleDefault,
createComponent: toggleGroupDefault,
code: codeExampleExclusive,
createComponent: toggleGroupExclusive,
},
{
title: "Inclusive ToggleGroup",
description: "A simple inclusive toggleGroup.",
code: codeExampleInclusive,
createComponent: toggleGroupInclusive,
},
{
title: "State in URL",
description: "A toggleGroup with state in the URL.",
code: codeExampleUrl,
createComponent: toggleGroupUrl,
},
],
gridItem: toggleGroupGridItem,
};
Expand Down
2 changes: 1 addition & 1 deletion bau-ui/input/input.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export default function (context, options = {}) {
color,
variant,
className,
props.class.join(" "),
Array.isArray(props.class) ? props.class?.join(" ") : props.class,
],
});
};
Expand Down
6 changes: 4 additions & 2 deletions bau-ui/toggle/toggle.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export default function (context, options = {}) {
--toggle-background-color: rgba(255, 255, 255, 0.4)
}
`;

const className = css`
color: inherit;
display: inline-flex;
Expand All @@ -31,11 +32,11 @@ export default function (context, options = {}) {
transition: all var(--transition-slow);
box-sizing: border-box;
cursor: pointer;
&[aria-pressed="true"] {
&[selected="true"] {
background-color: var(--toggle-background-color);
box-shadow: var(--shadow-lg);
}
&[aria-pressed="true"].solid {
&[selected="true"].solid {
filter: brightness(80%) !important;
}
&.outline,
Expand Down Expand Up @@ -81,6 +82,7 @@ export default function (context, options = {}) {
...props,
onclick: (event) => {
const { target } = event;
//debugger;
const pressed = target.getAttribute("aria-pressed");
target.setAttribute(
"aria-pressed",
Expand Down

0 comments on commit d304115

Please sign in to comment.