aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-web/design-system/src/components/input/SearchSelect.tsx
blob: 1f8d4c346500b1e43a1c7c45afda619dc821260a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/*
 * SonarQube
 * Copyright (C) 2009-2024 SonarSource SA
 * mailto:info AT sonarsource DOT com
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
import classNames from 'classnames';
import { omit } from 'lodash';
import React from 'react';
import { GroupBase, InputProps } from 'react-select';
import AsyncSelect, { AsyncProps } from 'react-select/async';
import { INPUT_SIZES } from '../../helpers';
import { Key } from '../../helpers/keyboard';
import { SelectProps, selectStyle } from '../../sonar-aligned/components/input';
import { InputSearch } from './InputSearch';

type SearchSelectProps<
  Option,
  IsMulti extends boolean = false,
  Group extends GroupBase<Option> = GroupBase<Option>,
> = SelectProps<Option, IsMulti, Group> & AsyncProps<Option, IsMulti, Group>;

/**
 * @deprecated Use Select or SelectAsync from Echoes instead.
 */
export function SearchSelect<
  Option,
  IsMulti extends boolean = false,
  Group extends GroupBase<Option> = GroupBase<Option>,
>({ size = 'full', selectRef, ...props }: SearchSelectProps<Option, IsMulti, Group>) {
  const styles = selectStyle<Option, IsMulti, Group>({ size });
  return (
    <AsyncSelect<Option, IsMulti, Group>
      {...omit(props, 'className', 'large')}
      className={classNames('react-select', props.className)}
      classNamePrefix="react-select"
      classNames={{
        control: ({ isDisabled }) =>
          classNames(
            'sw-border-0 sw-rounded-2 sw-outline-none sw-shadow-none',
            isDisabled && 'sw-pointer-events-none sw-cursor-not-allowed',
          ),
        indicatorsContainer: () => 'sw-hidden',
        input: () => `sw-flex sw-w-full sw-p-0 sw-m-0`,
        valueContainer: () => `sw-px-3 sw-pb-1 sw-mb-1 sw-pt-4`,
        placeholder: () => 'sw-hidden',
        ...props.classNames,
      }}
      components={{
        Input: SearchSelectInput,
        ...props.components,
      }}
      ref={selectRef}
      styles={{
        ...styles,
        menu: (base, props) => ({
          ...styles.menu?.(base, props),
          width: `calc(${INPUT_SIZES[size]} - 2px)`,
        }),
      }}
    />
  );
}

/**
 * @deprecated Use Select or SelectAsync from Echoes instead.
 */
export function SearchSelectInput<
  Option,
  IsMulti extends boolean = false,
  Group extends GroupBase<Option> = GroupBase<Option>,
>(props: InputProps<Option, IsMulti, Group>) {
  const {
    selectProps: { placeholder, isLoading, inputValue, minLength },
  } = props;

  const onChange = (v: string, prevValue = '') => {
    props.selectProps.onInputChange(v, { action: 'input-change', prevInputValue: prevValue });
  };

  const handleKeyDown = (event: React.KeyboardEvent<HTMLInputElement>) => {
    const target = event.target as HTMLInputElement;

    if (event.key === Key.Escape.toString() && target.value !== '') {
      event.stopPropagation();
      onChange('');
    }
  };

  return (
    <InputSearch
      {...omit(props, 'value', 'aria-label', 'id')}
      autoFocus
      inputId={props.id}
      loading={isLoading && inputValue.length >= (minLength ?? 0)}
      minLength={minLength}
      onChange={onChange}
      onKeyDown={handleKeyDown}
      placeholder={placeholder as string}
      searchInputAriaLabel={props['aria-label']}
      size="full"
    />
  );
}