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

Ensure options with value undefined are selected #1672

Merged
merged 1 commit into from
Aug 21, 2024
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
31 changes: 25 additions & 6 deletions src/components/input/Select.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
useContext,
useId,
useLayoutEffect,
useMemo,
useRef,
useState,
} from 'preact/hooks';
Expand Down Expand Up @@ -77,9 +78,23 @@ function SelectOption<T>({
}

const { selectValue, value: currentValue, multiple } = selectContext;
const selected =
!disabled &&
((multiple && currentValue.includes(value)) || currentValue === value);
const selected = useMemo(() => {
if (disabled) {
return false;
}

if (!multiple) {
return currentValue === value;
}

// In multi-select, the option should be marked as selected for values
// which are explicitly part of the array, or for `undefined` values if the
// array is empty
return (
currentValue.includes(value) ||
(currentValue.length === 0 && value === undefined)
);
}, [currentValue, disabled, multiple, value]);

const selectOneValue = useCallback(() => {
const options: SelectValueOptions = { closeListbox: true };
Expand All @@ -96,10 +111,14 @@ function SelectOption<T>({
return;
}

const options: SelectValueOptions = { closeListbox: false };
const options: SelectValueOptions = {
// Close listbox only if selected value is a "clear" option. Clear options
// are those with `undefined` value
closeListbox: value === undefined,
};

// In multi-select, clear selection for nullish values
if (!value) {
// In multi-select, clear selection for `undefined` values
if (value === undefined) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Being more explicit on what is the "special" value.

selectValue([], options);
return;
}
Expand Down
36 changes: 26 additions & 10 deletions src/components/input/test/Select-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,12 @@ describe('Select', () => {
const wrapper = mount(
<Component value={undefined} onChange={sinon.stub()} {...props}>
<Component.Option value={undefined}>
<span data-testid="option-reset">Reset</span>
{({ selected }) => (
<span data-testid="option-reset">
Reset
{selected && <span data-testid="selected-option" />}
</span>
)}
</Component.Option>
{items.map(item => (
<Component.Option
Expand Down Expand Up @@ -136,6 +141,11 @@ describe('Select', () => {
checkbox.getDOMNode().dispatchEvent(new KeyboardEvent('keydown', { key }));
}

const isOptionSelected = (wrapper, id) =>
wrapper
.find(`[data-testid="option-${id}"]`)
.exists('[data-testid="selected-option"]');

it('changes selected value when an option is clicked', () => {
const onChange = sinon.stub();
const wrapper = createComponent({ onChange });
Expand Down Expand Up @@ -186,16 +196,12 @@ describe('Select', () => {

it('marks the right item as selected', () => {
const wrapper = createComponent({ value: items[2] });
const isOptionSelected = id =>
wrapper
.find(`[data-testid="option-${id}"]`)
.exists('[data-testid="selected-option"]');

assert.isFalse(isOptionSelected(1));
assert.isFalse(isOptionSelected(2));
assert.isTrue(isOptionSelected(3));
assert.isFalse(isOptionSelected(4));
assert.isFalse(isOptionSelected(5));
assert.isFalse(isOptionSelected(wrapper, 1));
assert.isFalse(isOptionSelected(wrapper, 2));
assert.isTrue(isOptionSelected(wrapper, 3));
assert.isFalse(isOptionSelected(wrapper, 4));
assert.isFalse(isOptionSelected(wrapper, 5));
});

it('marks the right item as disabled', () => {
Expand Down Expand Up @@ -478,6 +484,16 @@ describe('Select', () => {
});
});

[
{ value: [], isResetSelected: true },
{ value: [items[0], items[2]], isResetSelected: false },
].forEach(({ value, isResetSelected }) => {
it('marks reset option as selected when value is empty', () => {
const wrapper = createComponent({ value }, { Component: MultiSelect });
assert.equal(isOptionSelected(wrapper, 'reset'), isResetSelected);
});
});

['Enter', ' '].forEach(key => {
it(`does not change selected value when ${key} is pressed in an option's checkbox`, () => {
const onChange = sinon.stub();
Expand Down