Extends LabelSelect to have a custom chip render. This allows us to disable labels that cannot be removed on job launch

This commit is contained in:
mabashian
2022-09-07 16:17:11 -04:00
committed by Alan Rominger
parent 4f5596eb0c
commit 697193d3d6
3 changed files with 50 additions and 5 deletions

View File

@@ -1,6 +1,12 @@
import React, { useState, useEffect } from 'react'; import React, { useState, useEffect } from 'react';
import { func, arrayOf, number, shape, string, oneOfType } from 'prop-types'; import { func, arrayOf, number, shape, string, oneOfType } from 'prop-types';
import { Select, SelectOption, SelectVariant } from '@patternfly/react-core'; import {
Chip,
ChipGroup,
Select,
SelectOption,
SelectVariant,
} from '@patternfly/react-core';
import { t } from '@lingui/macro'; import { t } from '@lingui/macro';
import { LabelsAPI } from 'api'; import { LabelsAPI } from 'api';
import useIsMounted from 'hooks/useIsMounted'; import useIsMounted from 'hooks/useIsMounted';
@@ -60,7 +66,12 @@ function LabelSelect({ value, placeholder, onChange, onError, createText }) {
const renderOptions = (opts) => const renderOptions = (opts) =>
opts.map((option) => ( opts.map((option) => (
<SelectOption key={option.id} aria-label={option.name} value={option}> <SelectOption
key={option.id}
aria-label={option.name}
value={option}
isDisabled={option.isReadOnly}
>
{option.name} {option.name}
</SelectOption> </SelectOption>
)); ));
@@ -73,6 +84,26 @@ function LabelSelect({ value, placeholder, onChange, onError, createText }) {
} }
return null; return null;
}; };
const chipGroupComponent = () => (
<ChipGroup>
{(selections || []).map((currentChip) => (
<Chip
isReadOnly={currentChip.isReadOnly}
key={currentChip.name}
onClick={(e, item) => {
if (typeof item === 'string') {
item = { id: item, name: item };
}
onSelect(e, item);
}}
>
{currentChip.name}
</Chip>
))}
</ChipGroup>
);
return ( return (
<Select <Select
variant={SelectVariant.typeaheadMulti} variant={SelectVariant.typeaheadMulti}
@@ -83,7 +114,7 @@ function LabelSelect({ value, placeholder, onChange, onError, createText }) {
} }
onSelect(e, item); onSelect(e, item);
}} }}
onClear={() => onChange([])} onClear={() => onChange(selections.filter((label) => label.isReadOnly))}
onFilter={onFilter} onFilter={onFilter}
isCreatable isCreatable
onCreateOption={(label) => { onCreateOption={(label) => {
@@ -101,6 +132,7 @@ function LabelSelect({ value, placeholder, onChange, onError, createText }) {
createText={createText} createText={createText}
noResultsFoundText={t`No results found`} noResultsFoundText={t`No results found`}
ouiaId="template-label-select" ouiaId="template-label-select"
chipGroupComponent={chipGroupComponent()}
> >
{renderOptions(options)} {renderOptions(options)}
</Select> </Select>

View File

@@ -75,7 +75,12 @@ function LaunchButton({ resource, children }) {
data: { results }, data: { results },
} = await readLabels; } = await readLabels;
setLabels(results); const allLabels = results.map((label) => ({
...label,
isReadOnly: true,
}));
setLabels(allLabels);
} }
if (canLaunchWithoutPrompt(launch)) { if (canLaunchWithoutPrompt(launch)) {

View File

@@ -23,7 +23,15 @@ export default function useSyncedSelectValue(value, onChange) {
if (!match) { if (!match) {
newOptions.push(item); newOptions.push(item);
} }
return match || item;
if (match) {
if (item.isReadOnly) {
match.isReadOnly = true;
}
return match;
}
return item;
}); });
setSelections(syncedValue); setSelections(syncedValue);
} }