Skip to content

Commit

Permalink
fix: Select component empty value selection
Browse files Browse the repository at this point in the history
  • Loading branch information
daniele-mng committed Jan 21, 2025
1 parent 6015126 commit 72cb97d
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
32 changes: 32 additions & 0 deletions src/web/components/form/__tests__/select.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,38 @@ describe('Select component tests', () => {

expect(getSelectItemElements().length).toEqual(1);
});

test('should not call onChange handler when newValue is null or undefined', async () => {
const items = [
{
value: 'bar',
label: 'Bar',
},
{
value: 'foo',
label: 'Foo',
},
];

const onChange = testing.fn();

render(<Select items={items} value="bar" onChange={onChange} />);

const input = getSelectElement();

await openSelectElement(input);

const domItems = getSelectItemElements();

await clickElement(domItems[0]);

expect(onChange).not.toHaveBeenCalled();

await clickElement(domItems[1]);

expect(onChange).toHaveBeenCalledWith('foo', undefined);
});

describe('SelectItemRaw', () => {
test.each([
{
Expand Down
4 changes: 2 additions & 2 deletions src/web/components/form/select.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {Select as OpenSightSelect} from '@greenbone/opensight-ui-components-mant
import {Loader} from '@mantine/core';
import {_} from 'gmp/locale/lang';
import {isDefined, isArray} from 'gmp/utils/identity';
import {useCallback, forwardRef} from 'react';
import {useState, useCallback, forwardRef} from 'react';
import useTranslation from 'web/hooks/useTranslation';
import PropTypes, {mayRequire} from 'web/utils/proptypes';

Expand Down Expand Up @@ -93,7 +93,7 @@ const Select = ({

const handleChange = useCallback(
newValue => {
if (isDefined(onChange)) {
if (isDefined(onChange) && Boolean(newValue)) {
onChange(newValue, name);
}
},
Expand Down

0 comments on commit 72cb97d

Please sign in to comment.