Skip to content
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

[WNMGDS-2871] Adds ds-autocomplete component #3257

Merged
merged 33 commits into from
Oct 4, 2024
Merged
Show file tree
Hide file tree
Changes from 32 commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
b2f216c
initial autocomplete scaffolding
kim-cmsds Aug 30, 2024
200b722
speculatively check for a wc text field
kim-cmsds Aug 30, 2024
e47bd2b
flatten autocomplete wc
kim-cmsds Aug 31, 2024
ea264d2
Merge branch 'main' into kniedermaier/WNMGDS-2871/autocomplete-wc
kim-cmsds Sep 5, 2024
0a00fb2
remove changes
kim-cmsds Sep 17, 2024
454cb3c
Merge branch 'main' into kniedermaier/WNMGDS-2871/autocomplete-wc
kim-cmsds Sep 23, 2024
c5d3fa9
sketching
kim-cmsds Sep 26, 2024
5fbf0a1
a more complete autocomplete example
kim-cmsds Sep 26, 2024
8f728f1
[WNMGDS-2892] Adds `ds-tabs` web component. (#3234)
tamara-corbalt Sep 24, 2024
accf79f
[WNMDGS-2975] Change focus color from orchid to copper on CMSgov them…
malloryiden Sep 26, 2024
416e875
initial autocomplete unit test, snapshot
kim-cmsds Sep 27, 2024
1e92186
adds to test coverage
kim-cmsds Sep 27, 2024
8aff619
Merge branch 'main' into kniedermaier/WNMGDS-2871/autocomplete-wc
kim-cmsds Oct 1, 2024
36045da
Clarifies text field hint for "no results" story
kim-cmsds Oct 1, 2024
fa7f665
{s}
kim-cmsds Oct 1, 2024
c66afa3
formats autocomplete and text field astro examples
kim-cmsds Oct 1, 2024
1ea4c0d
comments out custom markup example
kim-cmsds Oct 1, 2024
607ce70
adds additional test coverage to ds-autocomplete example
kim-cmsds Oct 1, 2024
c9da98f
updates storybook docs snapshots
kim-cmsds Oct 1, 2024
ff1ddb9
updates astro web component script srcs
kim-cmsds Oct 1, 2024
d3ff4c8
remove unnecessary comment
kim-cmsds Oct 1, 2024
3c0e887
updates snapshots
kim-cmsds Oct 1, 2024
7735a39
updates examples snapshots
kim-cmsds Oct 2, 2024
8d801ef
Update packages/design-system/src/components/web-components/ds-autoco…
kim-cmsds Oct 2, 2024
02c2817
Merge branch 'main' into kniedermaier/WNMGDS-2871/autocomplete-wc
kim-cmsds Oct 2, 2024
365c40a
updates storybook docs table per review
kim-cmsds Oct 2, 2024
4fd0851
more safely check for undefined in various web components; adds test …
kim-cmsds Oct 2, 2024
744f0c8
Merge branch 'main' into kniedermaier/WNMGDS-2871/autocomplete-wc
kim-cmsds Oct 3, 2024
0a53014
updates snapshots
kim-cmsds Oct 3, 2024
c6ea35b
Removes a proof-of-concept change
kim-cmsds Oct 3, 2024
2fbc37e
updates snaps
kim-cmsds Oct 3, 2024
77aed7f
Merge branch 'main' into kniedermaier/WNMGDS-2871/autocomplete-wc
kim-cmsds Oct 4, 2024
9e61181
updates snaps
kim-cmsds Oct 4, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions examples/astro/src/pages/web-components.astro
Original file line number Diff line number Diff line change
Expand Up @@ -124,11 +124,71 @@ import Layout from '../layouts/Layout.astro';
<hr class="ds-u-margin-y--4">

<ds-pagination id="pagination" total-pages="10" class="ds-u-margin-y--4"></ds-pagination>

<h2 class="ds-text-heading--2xl">Controlled text field example</h2>
<ds-text-field value="" id="the-text-field" label="Text field example"></ds-text-field>

<hr class="ds-u-margin-y--4">

<h2 class="ds-text-heading--2xl">Autocomplete example</h2>
<ds-autocomplete
id="the-autocomplete"
items='[{"id":"71","name":"Acetaminophen"},{"id":"72","name":"Advil"},{"id":"73","name":"Benadryl"},{"id":"74","name":"Claritin"},{"id":"75","name":"Detrol"},{"id":"76","name":"Excedrin"}]'
label="Enter and select a drug to see its cost under each plan."
hint="Type a letter to see results, then use ARROW keys to change options, ENTER key to make a selection, ESC to dismiss."
clear-search-button="true"
class-name="ds-u-padding-bottom--7"
>
</ds-autocomplete>
</div>
</main>
</Layout>

<script>
document.getElementById('the-text-field')?.addEventListener('ds-change', ((
e: CustomEvent<{ target: { value: string } }>
) => {
const newValue = e.detail.target.value;
document.getElementById('the-text-field')?.setAttribute('value', newValue);
}) as EventListener);

let items: { id: string; name: string }[] | null = null;
window.addEventListener("load", () => {
const initialItems = document?.getElementById('the-autocomplete')?.getAttribute('items');
if (typeof initialItems === 'string') {
items = JSON.parse(initialItems)
}
})

const autocompleteElement = document.getElementById('the-autocomplete');
autocompleteElement?.addEventListener('ds-input-value-change', ((
e: CustomEvent<{ value: string }>
) => {
const input = e?.detail?.value ?? '';

// reset items to original value
if (input.length === 0) {
autocompleteElement.setAttribute('items', JSON.stringify(items));
return;
}

if (input.length > 0 && items) {
const filteredItems = items.filter(
(item) => !item.name || item.name.toLowerCase().includes(input.toLowerCase())
);
autocompleteElement.setAttribute('items', JSON.stringify(filteredItems));
}
}) as EventListener);


document
.getElementById('the-autocomplete')
?.addEventListener('ds-change', function (
e: CustomEvent<{ selectedItem: { id: string; name: string } }>
) {
console.log(`Autocomplete selection: ${JSON.stringify(e.detail.selectedItem)}`);
} as EventListener);

document.getElementById('the-button').addEventListener('click', function () {
const alert = document.getElementById('alert');
alert.setAttribute('variation', 'error');
Expand Down Expand Up @@ -162,10 +222,12 @@ import Layout from '../layouts/Layout.astro';
<script src="@cmsgov/design-system/dist/web-components/bundle/base.js"></script>
<script src="@cmsgov/design-system/dist/web-components/bundle/ds-accordion.js"></script>
<script src="@cmsgov/design-system/dist/web-components/bundle/ds-alert.js"></script>
<script src="@cmsgov/design-system/dist/web-components/bundle/ds-autocomplete.js"></script>
<script src="@cmsgov/design-system/dist/web-components/bundle/ds-button.js"></script>
<script src="@cmsgov/design-system/dist/web-components/bundle/ds-choice.js"></script>
<script src="@cmsgov/design-system/dist/web-components/bundle/ds-dropdown.js"></script>
<script src="@cmsgov/design-system/dist/web-components/bundle/ds-month-picker.js"></script>
<script src="@cmsgov/design-system/dist/web-components/bundle/ds-pagination.js"></script>
<script src="@cmsgov/design-system/dist/web-components/bundle/ds-spinner.js"></script>
<script src="@cmsgov/design-system/dist/web-components/bundle/ds-text-field.js"></script>
<script src="@cmsgov/design-system/dist/web-components/bundle/ds-usa-banner.js"></script>
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,6 @@ export const NoResults: Story = {
items: [],
clearSearchButton: false,
textFieldLabel: 'This will show a "no results" message.',
textFieldHint: "Start typing, but you'll only get a loading message.",
textFieldHint: 'Start typing, but youll only get a "no results" message.',
} as any,
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Autocomplete renders correctly 1`] = `
<DocumentFragment>
<ds-autocomplete
aria-clear-label="Clear search to try again"
clear-input-text="Clear search"
clear-search-button="true"
items="[{\\"id\\":\\"kRf6c2fY\\",\\"name\\":\\"Cook County, IL\\"}]"
label="autocomplete label"
loading-message="Loading..."
no-results-message="No results"
>
<div
class="ds-c-autocomplete"
>
<div
class="ds-u-clearfix"
>
<label
class="ds-c-label"
for="autocomplete--1"
id="autocomplete--1__label"
>
autocomplete label
</label>
<p
aria-atomic="true"
aria-live="assertive"
class="ds-c-inline-error"
id="autocomplete--1__error"
/>
<input
aria-autocomplete="list"
aria-controls="autocomplete--1__menu"
aria-expanded="false"
aria-invalid="false"
aria-labelledby="autocomplete--1__label"
autocomplete="off"
autocorrect="off"
class="ds-c-field"
id="autocomplete--1"
name="autocomplete"
role="combobox"
spellcheck="false"
type="text"
/>
</div>
<button
aria-label="Clear search to try again"
class="ds-c-button ds-c-button--small ds-c-button--ghost ds-u-padding-right--0 ds-c-autocomplete__clear-btn"
type="button"
>
Clear search
</button>
</div>
<template />
</ds-autocomplete>
</DocumentFragment>
`;

exports[`Autocomplete renders item with custom className 1`] = `
Array [
<li
aria-selected="false"
class="ds-c-autocomplete__menu-item"
data-key="1a"
id="autocomplete--13__item--0"
role="option"
>
<span>
Normal item
</span>
</li>,
<li
aria-selected="false"
class="custom-class ds-c-autocomplete__menu-item"
data-key="5b"
id="autocomplete--13__item--1"
role="option"
>
<span>
Special item
</span>
</li>,
]
`;
Loading