-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bc3216b
commit d304115
Showing
6 changed files
with
93 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
bau-ui/examples/bau-storybook/src/pages/toggleGroup/toggleGroup-url.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")) | ||
); | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters