-
Notifications
You must be signed in to change notification settings - Fork 7
Chip group #598
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Chip group #598
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
70dee51
Reapply "Hide top on scroll (#589)" (#594)
840c68c
Add ChipGroup component
85b1219
Update chip styles
402a037
test: ChipGroup
3d26596
Add Chips to components
b36ca1f
style: wrap chips
ecbb5da
Documentation
d497cd0
chore: remove log
73321ee
docs
46c40d6
docs
5710a60
🤖 update snapshots
github-actions[bot] 8a2faab
Merge branch 'main' into chip-group
mstrasinskis cb49c95
Add aria params
78df1ac
Merge branch 'chip-group' of github.com:dfinity/gix-components into c…
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Binary file modified
BIN
-613 Bytes
(100%)
e2e/busy-screen.e2e.ts-snapshots/Should-hide-spinner-1-Google-Chrome-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
-1.56 KB
(99%)
...kbox.e2e.ts-snapshots/Should-display-default-behavior-1-Google-Chrome-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
-1.56 KB
(99%)
...ault-behavior-even-if-box-sizing-is-set-as-border-box-1-Google-Chrome-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
-2.2 KB
(99%)
....ts-snapshots/External-link-is-displayed-with-an-icon-1-Google-Chrome-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
-1.61 KB
(99%)
...nner.e2e.ts-snapshots/Should-display-default-behavior-1-Google-Chrome-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
-86 Bytes
(100%)
e2e/popover.e2e.ts-snapshots/Should-render-close-button-1-Google-Chrome-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
-395 Bytes
(100%)
...er.e2e.ts-snapshots/Should-render-overflowing-popover-1-Google-Chrome-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
-1.15 KB
(99%)
...ress-bar.e2e.ts-snapshots/Should-render-progress-bars-1-Google-Chrome-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
-783 Bytes
(100%)
...ogress-steps.e2e.ts-snapshots/All-steps-are-completed-1-Google-Chrome-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
-326 Bytes
(100%)
...ess-steps.e2e.ts-snapshots/Second-step-is-in-progress-1-Google-Chrome-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+18 Bytes
(100%)
...e.ts-snapshots/QR-code-renders-https-identity-ic0-app-1-Google-Chrome-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
-64 Bytes
(100%)
...or.e2e.ts-snapshots/QR-code-renders-https-nns-ic0-app-1-Google-Chrome-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
-733 Bytes
(100%)
...eader.e2e.ts-snapshots/Read-QR-code-value-with-camera-1-Google-Chrome-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
-530 Bytes
(100%)
e2e/segment.e2e.ts-snapshots/First-segment-is-selected-1-Google-Chrome-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
-435 Bytes
(100%)
e2e/segment.e2e.ts-snapshots/Second-segment-is-selected-1-Google-Chrome-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
-566 Bytes
(100%)
e2e/segment.e2e.ts-snapshots/Third-segment-is-selected-1-Google-Chrome-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+719 Bytes
(100%)
e2e/tag.e2e.ts-snapshots/Should-render-tags-1-Google-Chrome-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+14 Bytes
(100%)
e2e/toasts.e2e.ts-snapshots/Should-close-toast-1-Google-Chrome-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+295 Bytes
(100%)
...display-multiple-toasts-and-user-is-able-to-close-one-1-Google-Chrome-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+281 Bytes
(100%)
...s/Should-display-multiple-toasts-for-the-top-position-1-Google-Chrome-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+753 Bytes
(100%)
e2e/toasts.e2e.ts-snapshots/Toast-error-1-Google-Chrome-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+746 Bytes
(100%)
e2e/toasts.e2e.ts-snapshots/Toast-info-1-Google-Chrome-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+336 Bytes
(100%)
e2e/toasts.e2e.ts-snapshots/Toast-message-1-Google-Chrome-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+63 Bytes
(100%)
e2e/toasts.e2e.ts-snapshots/Toast-multiline-message-1-Google-Chrome-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+724 Bytes
(100%)
e2e/toasts.e2e.ts-snapshots/Toast-success-1-Google-Chrome-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+760 Bytes
(100%)
e2e/toasts.e2e.ts-snapshots/Toast-warn-1-Google-Chrome-linux.png
Oops, something went wrong.
This file contains hidden or 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 hidden or 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 hidden or 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,55 @@ | ||
| <script lang="ts"> | ||
| import { createEventDispatcher } from "svelte"; | ||
|
|
||
| export let label: string; | ||
| export let id: string; | ||
| export let selected: boolean; | ||
|
|
||
| const dispatch = createEventDispatcher(); | ||
| </script> | ||
|
|
||
| <button | ||
| class="chip" | ||
| data-tid="chip-component" | ||
| class:selected | ||
| role="radio" | ||
| aria-checked={selected} | ||
| on:click={() => dispatch("nnsClick", id)}>{label}</button | ||
| > | ||
|
|
||
| <style lang="scss"> | ||
| @use "../styles/mixins/effect"; | ||
| @use "../styles/mixins/fonts"; | ||
| @use "../styles/mixins/button"; | ||
|
|
||
| .chip { | ||
| @include button.base; | ||
| @include fonts.small(true); | ||
|
|
||
| // Override default button styles | ||
| padding: var(--padding) var(--padding-1_5x); | ||
| min-height: 0; | ||
| border: solid var(--button-border-size) var(--primary); | ||
| background: transparent; | ||
| color: var(--button-secondary-color); | ||
| border-radius: var(--border-radius-5x); | ||
|
|
||
| transition: all var(--animation-time-short) ease-in; | ||
|
|
||
| &:hover, | ||
| &:focus { | ||
| background: var(--button-card-focus-background); | ||
| } | ||
|
|
||
| &.selected { | ||
| background: var(--primary); | ||
| color: var(--primary-contrast); | ||
| pointer-events: none; | ||
|
|
||
| &:hover, | ||
| &:focus { | ||
| background: var(--primary); | ||
| } | ||
| } | ||
| } | ||
| </style> | ||
This file contains hidden or 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,43 @@ | ||
| <script lang="ts" context="module"> | ||
| export interface ChipData { | ||
| label: string; | ||
| id: string; | ||
| selected: boolean; | ||
| } | ||
| </script> | ||
|
|
||
| <script lang="ts"> | ||
| import { createEventDispatcher } from "svelte"; | ||
| import Chip from "./Chip.svelte"; | ||
|
|
||
| export let chips: ChipData[] = []; | ||
| export let testId: string = "chip-group-component"; | ||
|
|
||
| const dispatch = createEventDispatcher(); | ||
| const onChipClick = ({ detail: selectedId }: CustomEvent<string>) => { | ||
| chips = chips.map((chip) => ({ | ||
| ...chip, | ||
| selected: chip.id === selectedId, | ||
| })); | ||
| dispatch("nnsSelect", selectedId); | ||
| }; | ||
| </script> | ||
|
|
||
| <div | ||
| data-tid={testId} | ||
| class="chip-group" | ||
| role="radiogroup" | ||
| aria-label="Options" | ||
| > | ||
| {#each chips as { id, label, selected } (id)} | ||
| <Chip {id} {label} {selected} on:nnsClick={onChipClick} /> | ||
| {/each} | ||
| </div> | ||
|
|
||
| <style lang="scss"> | ||
| .chip-group { | ||
| display: flex; | ||
| flex-wrap: wrap; | ||
| gap: var(--padding-0_5x); | ||
| } | ||
| </style> |
This file contains hidden or 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,84 @@ | ||
| <script lang="ts"> | ||
| import BottomSheet from "$lib/components/BottomSheet.svelte"; | ||
| import ChipGroup from "$lib/components/ChipGroup.svelte"; | ||
| import Card from "$lib/components/Card.svelte"; | ||
| import DocsLoremIpsum from "$docs/components/DocsLoremIpsum.svelte"; | ||
|
|
||
| const currentMonth = new Date().toLocaleString("en-US", { month: "short" }).toLowerCase(); | ||
| let chips = [ | ||
| { id: "jan", label: "January", selected: currentMonth === "jan" }, | ||
| { id: "feb", label: "February", selected: currentMonth === "feb" }, | ||
| { id: "mar", label: "March", selected: currentMonth === "mar" }, | ||
| { id: "apr", label: "April", selected: currentMonth === "apr" }, | ||
| { id: "may", label: "May", selected: currentMonth === "may" }, | ||
| { id: "jun", label: "June", selected: currentMonth === "jun" }, | ||
| { id: "jul", label: "July", selected: currentMonth === "jul" }, | ||
| { id: "aug", label: "August", selected: currentMonth === "aug" }, | ||
| { id: "sep", label: "September", selected: currentMonth === "sep" }, | ||
| { id: "oct", label: "October", selected: currentMonth === "oct" }, | ||
| { id: "nov", label: "November", selected: currentMonth === "nov" }, | ||
| { id: "dec", label: "December", selected: currentMonth === "dec" } | ||
| ]; | ||
|
|
||
| let nnsSelectDetail = undefined; | ||
| const onNnsSelect = ({detail}) => nnsSelectDetail= detail; | ||
| </script> | ||
|
|
||
| # ChipGroup | ||
|
|
||
| ChipGroup allows users to choose from multiple options. It’s possible to listen for the nnsSelect event (where the selected ID is provided in the event detail) or, alternatively, bind the chip property. The current implementation does not support multiple selections. | ||
|
|
||
| ### ChipData interface | ||
|
|
||
| ```typescript | ||
| export interface ChipData { | ||
| label: string; | ||
| id: string; | ||
| selected: boolean; | ||
| } | ||
| ``` | ||
|
|
||
| ### Sample | ||
|
|
||
| ```javascript | ||
| <script> | ||
| // Items of ChipData | ||
| let chips = [ | ||
| { | ||
| id: "jan", | ||
| label: "January", | ||
| selected: false | ||
| }, | ||
| // ... | ||
| ]; | ||
| let nnsSelectDetail; | ||
| const onNnsSelect = ({detail}) => nnsSelectDetail= detail; | ||
| </script> | ||
|
|
||
| <ChipGroup bind:chips on:nnsSelect={onNnsSelect} /> | ||
| ``` | ||
|
|
||
| ## Properties | ||
|
|
||
| | Property | Description | Type | Default | | ||
| | -------- | -------------------------- | ----------------- | ---------------------- | | ||
| | `chips` | ChipData items to display. | `Array<ChipData>` | | | ||
| | `testId` | Optional data-tid value. | `string` | `chip-group-component` | | ||
|
|
||
| ## Events | ||
|
|
||
| | Event | Description | Detail | | ||
| | ----------- | --------------------------------------- | ----------- | | ||
| | `nnsSelect` | Triggered when a user clicks on a Chip. | ChipData.id | | ||
|
|
||
| ## Showcase | ||
|
|
||
| <Card> | ||
| <ChipGroup on:nnsSelect={onNnsSelect} bind:chips /> | ||
| </Card> | ||
|
|
||
| <h4>nnsSelect event detail:</h4> | ||
| <pre>{nnsSelectDetail}</pre> | ||
|
|
||
| <h4>bind:chips</h4> | ||
| <pre>{JSON.stringify(chips, null, 2)}</pre> |
This file contains hidden or 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,84 @@ | ||
| import ChipGroup from "$lib/components/ChipGroup.svelte"; | ||
| import { fireEvent } from "@testing-library/dom"; | ||
| import { tick } from "svelte"; | ||
| import { render } from "../../utils/render.test-utils"; | ||
|
|
||
| describe("ChipGroup", () => { | ||
| const testChips = [ | ||
| { id: "stake", label: "Stake", selected: false }, | ||
| { id: "maturity", label: "Maturity", selected: false }, | ||
| { id: "state", label: "State", selected: false }, | ||
| { id: "dissolve delay", label: "Dissolve Delay", selected: false }, | ||
| ]; | ||
|
|
||
| it("should render Chips", () => { | ||
| const { getAllByTestId, getByTestId } = render(ChipGroup, { | ||
| props: { chips: testChips }, | ||
| }); | ||
|
|
||
| expect(getByTestId("chip-group-component")).not.toBeNull(); | ||
| const chipElements = getAllByTestId("chip-component"); | ||
| expect(chipElements).toHaveLength(testChips.length); | ||
| chipElements.forEach((chip, index) => { | ||
| expect(chip.textContent).toEqual(testChips[index].label); | ||
| }); | ||
| }); | ||
|
|
||
| it("should support initial selection", () => { | ||
| const chips = [...testChips]; | ||
| const selectedIndex = 2; | ||
| chips[selectedIndex] = { ...chips[selectedIndex], selected: true }; | ||
| const { getAllByTestId } = render(ChipGroup, { | ||
| props: { chips }, | ||
| }); | ||
| const chipElements = getAllByTestId("chip-component"); | ||
| chipElements.forEach((chip, index) => { | ||
| if (index === selectedIndex) { | ||
| expect(chip).toHaveClass("selected"); | ||
| } else { | ||
| expect(chip).not.toHaveClass("selected"); | ||
| } | ||
| }); | ||
| }); | ||
|
|
||
| it("should update selection on click", async () => { | ||
| const selectedIndex = 2; | ||
| const { getAllByTestId } = render(ChipGroup, { | ||
| props: { chips: testChips }, | ||
| }); | ||
|
|
||
| const chipElements = getAllByTestId("chip-component"); | ||
| chipElements.forEach((chip) => expect(chip).not.toHaveClass("selected")); | ||
|
|
||
| fireEvent.click(chipElements[selectedIndex]); | ||
| await tick(); | ||
| chipElements.forEach((chip, index) => { | ||
| if (index === selectedIndex) { | ||
| expect(chip).toHaveClass("selected"); | ||
| } else { | ||
| expect(chip).not.toHaveClass("selected"); | ||
| } | ||
| }); | ||
| }); | ||
|
|
||
| it("should emit nnsSelect", () => { | ||
| const spyNnsSelect = vi.fn(); | ||
| const { getAllByTestId } = render(ChipGroup, { | ||
| props: { chips: testChips }, | ||
| events: { | ||
| nnsSelect: spyNnsSelect, | ||
| }, | ||
| }); | ||
|
|
||
| expect(spyNnsSelect).toHaveBeenCalledTimes(0); | ||
| const chipElements = getAllByTestId("chip-component"); | ||
|
|
||
| fireEvent.click(chipElements[1]); | ||
| expect(spyNnsSelect).toHaveBeenCalledTimes(1); | ||
| expect(spyNnsSelect).toHaveBeenCalledWith( | ||
| expect.objectContaining({ | ||
| detail: testChips[1].id, | ||
| }), | ||
| ); | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.