Skip to content

Commit 8ca36b7

Browse files
authored
Select: Fix issue with placeholder and auto width (grafana#42851)
* Select: Fix issue with placeholder and auto width * Fixed lint issue
1 parent 8828d5e commit 8ca36b7

File tree

6 files changed

+15
-39
lines changed

6 files changed

+15
-39
lines changed

packages/grafana-ui/src/components/Forms/Legacy/Select/Select.tsx

+5-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ import NoOptionsMessage from './NoOptionsMessage';
1515
import resetSelectStyles from '../../../Select/resetSelectStyles';
1616
import { CustomScrollbar } from '../../../CustomScrollbar/CustomScrollbar';
1717
import { PopoverContent, Tooltip } from '../../../Tooltip/Tooltip';
18-
import { SelectableValue } from '@grafana/data';
18+
import { GrafanaTheme2, SelectableValue } from '@grafana/data';
19+
import { ThemeContext } from '../../../../themes';
1920

2021
/**
2122
* Changes in new selects:
@@ -49,6 +50,8 @@ export const MenuList = (props: any) => {
4950
);
5051
};
5152
export class Select<T> extends PureComponent<LegacySelectProps<T>> {
53+
static contextType = ThemeContext;
54+
5255
static defaultProps: Partial<LegacySelectProps<any>> = {
5356
className: '',
5457
isDisabled: false,
@@ -137,7 +140,7 @@ export class Select<T> extends PureComponent<LegacySelectProps<T>> {
137140
onChange={onChange}
138141
options={options}
139142
placeholder={placeholder || 'Choose'}
140-
styles={resetSelectStyles()}
143+
styles={resetSelectStyles(this.context as GrafanaTheme2)}
141144
isDisabled={isDisabled}
142145
isLoading={isLoading}
143146
isClearable={isClearable}

packages/grafana-ui/src/components/Select/SelectBase.tsx

+1-29
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import { default as AsyncCreatable } from 'react-select/async-creatable';
66

77
import { Icon } from '../Icon/Icon';
88
import { Spinner } from '../Spinner/Spinner';
9-
import { css, cx } from '@emotion/css';
109
import resetSelectStyles from './resetSelectStyles';
1110
import { SelectMenu, SelectMenuOptions } from './SelectMenu';
1211
import { IndicatorsContainer } from './IndicatorsContainer';
@@ -251,33 +250,6 @@ export function SelectBase<T>({
251250
MenuList: SelectMenu,
252251
Group: SelectOptionGroup,
253252
ValueContainer,
254-
Placeholder(props: any) {
255-
return (
256-
<div
257-
{...props.innerProps}
258-
className={cx(
259-
css(props.getStyles('placeholder', props)),
260-
css`
261-
display: inline-block;
262-
color: ${theme.colors.text.disabled};
263-
grid-area: 1 / 1 / 2 / 3;
264-
box-sizing: border-box;
265-
line-height: 1;
266-
white-space: nowrap;
267-
`,
268-
// When width: auto, the placeholder must take up space in the Select otherwise the width collapses down
269-
width !== 'auto' &&
270-
css`
271-
position: absolute;
272-
top: 50%;
273-
transform: translateY(-50%);
274-
`
275-
)}
276-
>
277-
{props.children}
278-
</div>
279-
);
280-
},
281253
IndicatorsContainer(props: any) {
282254
const { selectProps } = props;
283255
const { value, showAllSelectedWhenOpen, maxVisibleValues, menuIsOpen } = selectProps;
@@ -346,7 +318,7 @@ export function SelectBase<T>({
346318
...components,
347319
}}
348320
styles={{
349-
...resetSelectStyles(),
321+
...resetSelectStyles(theme),
350322
menuPortal: (base: any) => ({
351323
...base,
352324
zIndex: theme.zIndex.portal,

packages/grafana-ui/src/components/Select/resetSelectStyles.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
import { GrafanaTheme2 } from '@grafana/data';
12
import { CSSObjectWithLabel } from 'react-select';
23

3-
export default function resetSelectStyles() {
4+
export default function resetSelectStyles(theme: GrafanaTheme2) {
45
return {
56
clearIndicator: () => ({}),
67
container: () => ({}),
@@ -31,7 +32,10 @@ export default function resetSelectStyles() {
3132
multiValueRemove: () => ({}),
3233
noOptionsMessage: () => ({}),
3334
option: () => ({}),
34-
placeholder: () => ({}),
35+
placeholder: (originalStyles: CSSObjectWithLabel) => ({
36+
...originalStyles,
37+
color: theme.colors.text.disabled,
38+
}),
3539
singleValue: () => ({}),
3640
valueContainer: () => ({}),
3741
};

public/app/core/components/FolderFilter/FolderFilter.tsx

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React, { useCallback, useMemo, useState } from 'react';
22
import { css } from '@emotion/css';
33
import debounce from 'debounce-promise';
4-
import { AsyncMultiSelect, Icon, resetSelectStyles, useStyles2 } from '@grafana/ui';
4+
import { AsyncMultiSelect, Icon, useStyles2 } from '@grafana/ui';
55
import { GrafanaTheme2, SelectableValue } from '@grafana/data';
66

77
import { FolderInfo, PermissionLevelString } from 'app/types';
@@ -36,7 +36,6 @@ export function FolderFilter({ onChange: propsOnChange, maxMenuHeight }: FolderF
3636
isMulti: true,
3737
noOptionsMessage: 'No folders found',
3838
placeholder: 'Filter by folder',
39-
styles: resetSelectStyles(),
4039
maxMenuHeight,
4140
value,
4241
onChange,

public/app/core/components/PanelTypeFilter/PanelTypeFilter.tsx

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React, { useCallback, useMemo, useState } from 'react';
22
import { GrafanaTheme2, PanelPluginMeta, SelectableValue } from '@grafana/data';
33
import { getAllPanelPluginMeta } from 'app/features/panel/state/util';
4-
import { Icon, resetSelectStyles, MultiSelect, useStyles2 } from '@grafana/ui';
4+
import { Icon, MultiSelect, useStyles2 } from '@grafana/ui';
55
import { css } from '@emotion/css';
66

77
export interface Props {
@@ -42,7 +42,6 @@ export const PanelTypeFilter = ({ onChange: propsOnChange, maxMenuHeight }: Prop
4242
getOptionValue: (i: any) => i.value,
4343
noOptionsMessage: 'No Panel types found',
4444
placeholder: 'Filter by type',
45-
styles: resetSelectStyles(),
4645
maxMenuHeight,
4746
options,
4847
value,

public/app/core/components/TagFilter/TagFilter.tsx

+1-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import React, { FC } from 'react';
33
import { css } from '@emotion/css';
44
import { components } from 'react-select';
55
import debounce from 'debounce-promise';
6-
import { stylesFactory, useTheme, resetSelectStyles, Icon, AsyncMultiSelect } from '@grafana/ui';
6+
import { stylesFactory, useTheme, Icon, AsyncMultiSelect } from '@grafana/ui';
77
import { escapeStringForRegex, GrafanaTheme } from '@grafana/data';
88
// Components
99
import { TagOption } from './TagOption';
@@ -82,7 +82,6 @@ export const TagFilter: FC<Props> = ({
8282
noOptionsMessage: 'No tags found',
8383
onChange: onTagChange,
8484
placeholder,
85-
styles: resetSelectStyles(),
8685
value,
8786
width,
8887
components: {

0 commit comments

Comments
 (0)