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

test(Select): add story for selected value #1507

Merged
merged 1 commit into from
Feb 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
28 changes: 24 additions & 4 deletions src/components/Select/Select.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,15 @@ type Props = {
optionsClassName?: string;
variant?: VariantType;
disabled?: boolean;
value?: SelectOption;
};

const exampleOptions = [
type SelectOption = {
key: string;
label: string;
};

const exampleOptions: SelectOption[] = [
{
key: '1',
label: 'Dogs',
Expand All @@ -48,11 +54,12 @@ const exampleOptions = [
];

function InteractiveExampleUsingChildren(props: Props) {
const { variant, optionsAlign, optionsClassName, disabled } = props;
const { variant, optionsAlign, optionsClassName, disabled, value } = props;
const compact = variant === 'compact';

const [selectedOption, setSelectedOption] =
React.useState<(typeof exampleOptions)[0]>();
const [selectedOption, setSelectedOption] = React.useState<
SelectOption | undefined
>(value);

return (
<div className={styles['interactive-example']}>
Expand Down Expand Up @@ -224,3 +231,16 @@ export const OpenByDefault: StoryObj = {
await userEvent.click(dropdownButton);
},
};

export const WithSelectedOption: StoryObj = {
args: {
value: exampleOptions[0],
},
render: ({ value }) => (
<InteractiveExampleUsingChildren
aria-label="Favorite Animal"
labelComponent={<Select.Label>Favorite Animal</Select.Label>}
value={value}
/>
),
};
141 changes: 133 additions & 8 deletions src/components/Select/__snapshots__/Select.test.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -893,19 +893,144 @@ exports[`<Select /> UsingFunctionChildrenProp story renders snapshot 1`] = `
</div>
`;

exports[`<Select /> WithSelectedOption story renders snapshot 1`] = `
<div
class="select select--non-compact"
data-headlessui-state="open"
data-testid="dropdown"
>
<div
class="select__overline"
>
<label
class="select__label"
data-headlessui-state="open"
id="headlessui-listbox-label-:r1f:"
>
Favorite Animal
</label>
</div>
<button
aria-controls="headlessui-listbox-options-:r1h:"
aria-expanded="true"
aria-haspopup="true"
aria-labelledby="headlessui-listbox-label-:r1f: headlessui-listbox-button-:r1g:"
class="select-button"
data-headlessui-state="open"
id="headlessui-listbox-button-:r1g:"
type="button"
>
<span>
Dogs
</span>
<svg
aria-hidden="true"
class="icon select-button__icon select-button__icon--reversed"
fill="currentColor"
height="1.25rem"
style="--icon-size: 1.25rem;"
viewBox="0 0 24 24"
width="1.25rem"
xmlns="http://www.w3.org/2000/svg"
>
<path
d="M12 15.3746L6 9.37461L7.4 7.97461L12 12.5746L16.6 7.97461L18 9.37461L12 15.3746Z"
/>
</svg>
</button>
<div
aria-activedescendant="headlessui-listbox-option-:r1i:"
aria-labelledby="headlessui-listbox-label-:r1f:"
aria-orientation="vertical"
class="popover-container select__options select__options--full-width"
data-headlessui-state="open"
id="headlessui-listbox-options-:r1h:"
role="listbox"
tabindex="0"
>
<div
aria-selected="true"
class="popover-list-item popover-list-item--active select__option-item"
data-headlessui-state="active selected"
id="headlessui-listbox-option-:r1i:"
role="option"
tabindex="-1"
>
<div
class="popover-list-item__icon"
>
<svg
aria-hidden="true"
class="icon"
fill="currentColor"
height="1.5rem"
style="--icon-size: 1.5rem;"
viewBox="0 0 24 24"
width="1.5rem"
xmlns="http://www.w3.org/2000/svg"
>
<path
d="M9.55001 17.9996L3.85001 12.2996L5.27501 10.8746L9.55001 15.1496L18.725 5.97461L20.15 7.39961L9.55001 17.9996Z"
/>
</svg>
</div>
<span
class="select__option-text"
>
Dogs
</span>
</div>
<div
aria-selected="false"
class="popover-list-item select__option-item"
data-headlessui-state=""
id="headlessui-listbox-option-:r1j:"
role="option"
tabindex="-1"
>
<div
class="popover-list-item__no-icon"
/>
<span
class="select__option-text"
>
Cats
</span>
</div>
<div
aria-selected="false"
class="popover-list-item select__option-item"
data-headlessui-state=""
id="headlessui-listbox-option-:r1k:"
role="option"
tabindex="-1"
>
<div
class="popover-list-item__no-icon"
/>
<span
class="select__option-text"
>
Birds
</span>
</div>
</div>
</div>
`;

exports[`<Select /> renders the OpenByDefault story 1`] = `
<div
class="select select--non-compact"
data-headlessui-state="open"
data-testid="dropdown"
>
<button
aria-controls="headlessui-listbox-options-:r1i:"
aria-controls="headlessui-listbox-options-:r1o:"
aria-expanded="true"
aria-haspopup="true"
class="select-button"
data-headlessui-state="open"
id="headlessui-listbox-button-:r1h:"
id="headlessui-listbox-button-:r1n:"
type="button"
>
<span>
Expand All @@ -927,20 +1052,20 @@ exports[`<Select /> renders the OpenByDefault story 1`] = `
</svg>
</button>
<div
aria-activedescendant="headlessui-listbox-option-:r1n:"
aria-labelledby="headlessui-listbox-button-:r1h:"
aria-activedescendant="headlessui-listbox-option-:r1t:"
aria-labelledby="headlessui-listbox-button-:r1n:"
aria-orientation="vertical"
class="popover-container select__options select__options--full-width"
data-headlessui-state="open"
id="headlessui-listbox-options-:r1i:"
id="headlessui-listbox-options-:r1o:"
role="listbox"
tabindex="0"
>
<div
aria-selected="false"
class="popover-list-item select__option-item"
data-headlessui-state=""
id="headlessui-listbox-option-:r1m:"
id="headlessui-listbox-option-:r1s:"
role="option"
tabindex="-1"
>
Expand All @@ -957,7 +1082,7 @@ exports[`<Select /> renders the OpenByDefault story 1`] = `
aria-selected="true"
class="popover-list-item popover-list-item--active select__option-item"
data-headlessui-state="active selected"
id="headlessui-listbox-option-:r1n:"
id="headlessui-listbox-option-:r1t:"
role="option"
tabindex="-1"
>
Expand Down Expand Up @@ -989,7 +1114,7 @@ exports[`<Select /> renders the OpenByDefault story 1`] = `
aria-selected="false"
class="popover-list-item select__option-item"
data-headlessui-state=""
id="headlessui-listbox-option-:r1o:"
id="headlessui-listbox-option-:r1u:"
role="option"
tabindex="-1"
>
Expand Down