From 58bb357d33d2813fd00b7f1345db18df3503a6be Mon Sep 17 00:00:00 2001 From: Guillaume Peoc'h Date: Fri, 1 Apr 2022 14:10:42 +0200 Subject: SONAR-16225 Update SelectLegacy in /quality-gates --- .../apps/quality-gates/components/MetricSelect.tsx | 6 +- .../QualityGatePermissionsAddModalRenderer.tsx | 66 +++++++++++---- .../quality-gates/components/__tests__/App-it.tsx | 11 +-- ...QualityGatePermissionsAddModalRenderer-test.tsx | 12 ++- ...tyGatePermissionsAddModalRenderer-test.tsx.snap | 95 +++++++++++++--------- 5 files changed, 117 insertions(+), 73 deletions(-) (limited to 'server/sonar-web/src') diff --git a/server/sonar-web/src/main/js/apps/quality-gates/components/MetricSelect.tsx b/server/sonar-web/src/main/js/apps/quality-gates/components/MetricSelect.tsx index ce6879efa16..7ff3701a4ba 100644 --- a/server/sonar-web/src/main/js/apps/quality-gates/components/MetricSelect.tsx +++ b/server/sonar-web/src/main/js/apps/quality-gates/components/MetricSelect.tsx @@ -20,7 +20,7 @@ import { sortBy } from 'lodash'; import * as React from 'react'; import withMetricsContext from '../../../app/components/metrics/withMetricsContext'; -import SelectLegacy from '../../../components/controls/SelectLegacy'; +import Select from '../../../components/controls/Select'; import { getLocalizedMetricDomain, translate } from '../../../helpers/l10n'; import { Dict, Metric } from '../../../types/types'; import { getLocalizedMetricNameNoDiffMetric } from '../utils'; @@ -76,13 +76,13 @@ export class MetricSelectComponent extends React.PureComponent { }); return ( - o.value === metric?.key)} /> ); } diff --git a/server/sonar-web/src/main/js/apps/quality-gates/components/QualityGatePermissionsAddModalRenderer.tsx b/server/sonar-web/src/main/js/apps/quality-gates/components/QualityGatePermissionsAddModalRenderer.tsx index ea2f2fbd70c..47c6190c801 100644 --- a/server/sonar-web/src/main/js/apps/quality-gates/components/QualityGatePermissionsAddModalRenderer.tsx +++ b/server/sonar-web/src/main/js/apps/quality-gates/components/QualityGatePermissionsAddModalRenderer.tsx @@ -17,10 +17,12 @@ * along with this program; if not, write to the Free Software Foundation, * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ +import { identity, omit } from 'lodash'; import * as React from 'react'; +import { components, ControlProps, OptionProps, SingleValueProps } from 'react-select'; import { ResetButtonLink, SubmitButton } from '../../../components/controls/buttons'; import Modal from '../../../components/controls/Modal'; -import SelectLegacy from '../../../components/controls/SelectLegacy'; +import Select from '../../../components/controls/Select'; import GroupIcon from '../../../components/icons/GroupIcon'; import Avatar from '../../../components/ui/Avatar'; import { translate } from '../../../helpers/l10n'; @@ -31,7 +33,7 @@ export interface QualityGatePermissionsAddModalRendererProps { onClose: () => void; onInputChange: (query: string) => void; onSubmit: (event: React.SyntheticEvent) => void; - onSelection: (selection: UserBase | Group) => void; + onSelection: (selection: Option) => void; submitting: boolean; loading: boolean; query: string; @@ -39,7 +41,7 @@ export interface QualityGatePermissionsAddModalRendererProps { selection?: UserBase | Group; } -type Option = (UserBase | Group) & { value: string }; +export type Option = (UserBase | Group) & { value: string }; export default function QualityGatePermissionsAddModalRenderer( props: QualityGatePermissionsAddModalRendererProps @@ -50,6 +52,8 @@ export default function QualityGatePermissionsAddModalRenderer( const noResultsText = translate('no_results'); + const options = searchResults.map(r => ({ ...r, value: getValue(r) })); + return (
@@ -59,22 +63,24 @@ export default function QualityGatePermissionsAddModalRenderer(
- i} + autoFocus={true} + isClearable={false} + isSearchable={true} + placeholder="" isLoading={loading} - noResultsText={noResultsText} + filterOptions={identity} + noOptionsMessage={() => noResultsText} onChange={props.onSelection} onInputChange={props.onInputChange} - optionRenderer={optionRenderer} - options={searchResults.map(r => ({ ...r, value: isUser(r) ? r.login : r.name }))} - placeholder="" - searchable={true} - value={selection} - valueRenderer={optionRenderer} + components={{ + Option: optionRenderer, + SingleValue: singleValueRenderer, + Control: controlRenderer + }} + options={options} + value={options.find(o => o.value === (selection && getValue(selection)))} />
@@ -88,7 +94,11 @@ export default function QualityGatePermissionsAddModalRenderer( ); } -function optionRenderer(option: Option) { +function getValue(option: UserBase | Group) { + return isUser(option) ? option.login : option.name; +} + +export function customOptions(option: Option) { return ( <> {isUser(option) ? ( @@ -101,3 +111,27 @@ function optionRenderer(option: Option) { ); } + +function optionRenderer(props: OptionProps) { + return ( + + {customOptions(props.data)} + + ); +} + +function singleValueRenderer(props: SingleValueProps