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
No known key found for this signature in database
GPG Key ID: C2D7EAAA12B63559
3 changed files with 50 additions and 5 deletions

View File

@ -1,6 +1,12 @@
import React, { useState, useEffect } from 'react';
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 { LabelsAPI } from 'api';
import useIsMounted from 'hooks/useIsMounted';
@ -60,7 +66,12 @@ function LabelSelect({ value, placeholder, onChange, onError, createText }) {
const renderOptions = (opts) =>
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}
</SelectOption>
));
@ -73,6 +84,26 @@ function LabelSelect({ value, placeholder, onChange, onError, createText }) {
}
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 (
<Select
variant={SelectVariant.typeaheadMulti}
@ -83,7 +114,7 @@ function LabelSelect({ value, placeholder, onChange, onError, createText }) {
}
onSelect(e, item);
}}
onClear={() => onChange([])}
onClear={() => onChange(selections.filter((label) => label.isReadOnly))}
onFilter={onFilter}
isCreatable
onCreateOption={(label) => {
@ -101,6 +132,7 @@ function LabelSelect({ value, placeholder, onChange, onError, createText }) {
createText={createText}
noResultsFoundText={t`No results found`}
ouiaId="template-label-select"
chipGroupComponent={chipGroupComponent()}
>
{renderOptions(options)}
</Select>

View File

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

View File

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