mirror of
https://github.com/ansible/awx.git
synced 2026-01-23 23:41:23 -03:30
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:
parent
4f5596eb0c
commit
697193d3d6
@ -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>
|
||||
|
||||
@ -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)) {
|
||||
|
||||
@ -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);
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user