diff --git a/packages/grafana-ui/src/components/Select/SelectBase.test.tsx b/packages/grafana-ui/src/components/Select/SelectBase.test.tsx index 48a358b3e9a..20e560001f4 100644 --- a/packages/grafana-ui/src/components/Select/SelectBase.test.tsx +++ b/packages/grafana-ui/src/components/Select/SelectBase.test.tsx @@ -1,6 +1,7 @@ -import React from 'react'; +import React, { useState } from 'react'; import { mount, ReactWrapper } from 'enzyme'; import { render, screen } from '@testing-library/react'; +import userEvent from '@testing-library/user-event'; import selectEvent from 'react-select-event'; import { SelectBase } from './SelectBase'; import { SelectableValue } from '@grafana/data'; @@ -43,6 +44,25 @@ describe('SelectBase', () => { expect(screen.getByLabelText('My select')).toBeInTheDocument(); }); + it('allows the value to be unset', async () => { + const Test = () => { + const option = { value: 'test-value', label: 'Test label' }; + const [value, setValue] = useState | null>(option); + + return ( + <> + + + + ); + }; + + render(); + expect(screen.queryByText('Test label')).toBeInTheDocument(); + userEvent.click(screen.getByText('clear value')); + expect(screen.queryByText('Test label')).not.toBeInTheDocument(); + }); + describe('when openMenuOnFocus prop', () => { describe('is provided', () => { it('opens on focus', () => { diff --git a/packages/grafana-ui/src/components/Select/types.ts b/packages/grafana-ui/src/components/Select/types.ts index 0dee841f29d..5af8e377873 100644 --- a/packages/grafana-ui/src/components/Select/types.ts +++ b/packages/grafana-ui/src/components/Select/types.ts @@ -60,7 +60,7 @@ export interface SelectCommonProps { /** Use a custom element to control Select. A proper ref to the renderControl is needed if 'portal' isn't set to null*/ renderControl?: ControlComponent; tabSelectsValue?: boolean; - value?: SelectValue; + value?: SelectValue | null; /** Sets the width to a multiple of 8px. Should only be used with inline forms. Setting width of the container is preferred in other cases.*/ width?: number; isOptionDisabled?: () => boolean; diff --git a/packages/grafana-ui/src/components/Select/utils.test.ts b/packages/grafana-ui/src/components/Select/utils.test.ts index 8a48c413ab2..e279b9418f1 100644 --- a/packages/grafana-ui/src/components/Select/utils.test.ts +++ b/packages/grafana-ui/src/components/Select/utils.test.ts @@ -74,10 +74,14 @@ describe('Select utils', () => { expect(cleanValue('test1', optGroup)).toEqual([{ label: 'Group 4 - Option 1', value: 'test1' }]); expect(cleanValue(3, options)).toEqual([{ label: 'Option 3', value: 3 }]); }); - it('should return undefined for null/undefined/empty values', () => { + + it('should return null for null values', () => { + expect(cleanValue(null, options)).toEqual([null]); + }); + + it('should return undefined for undefined/empty values', () => { expect(cleanValue([undefined], options)).toEqual(undefined); expect(cleanValue(undefined, options)).toEqual(undefined); - expect(cleanValue(null, options)).toEqual(undefined); expect(cleanValue('', options)).toEqual(undefined); }); }); diff --git a/packages/grafana-ui/src/components/Select/utils.ts b/packages/grafana-ui/src/components/Select/utils.ts index a1615d2fd21..a564781c398 100644 --- a/packages/grafana-ui/src/components/Select/utils.ts +++ b/packages/grafana-ui/src/components/Select/utils.ts @@ -9,7 +9,8 @@ export const cleanValue = (value: any, options: Array