mirror of
https://github.com/ansible/awx.git
synced 2026-03-07 19:51:08 -03:30
start usePFSelect hook
This commit is contained in:
@@ -12,6 +12,31 @@ function setToString(labels) {
|
|||||||
return labels;
|
return labels;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO: extract to shared file
|
||||||
|
function usePFSelect(value, options, onChange) {
|
||||||
|
const [currentValue, setCurrentValue] = useState([]);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (value !== currentValue && options.length) {
|
||||||
|
const syncedValue = value.map(item =>
|
||||||
|
options.find(i => i.id === item.id)
|
||||||
|
);
|
||||||
|
setCurrentValue(syncedValue);
|
||||||
|
}
|
||||||
|
/* eslint-disable-next-line react-hooks/exhaustive-deps */
|
||||||
|
}, [value, options]);
|
||||||
|
|
||||||
|
const handleSelect = (event, item) => {
|
||||||
|
if (currentValue.includes(item)) {
|
||||||
|
onChange(currentValue.filter(i => i.id !== item.id));
|
||||||
|
} else {
|
||||||
|
onChange(currentValue.concat(item));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
return [currentValue, handleSelect];
|
||||||
|
}
|
||||||
|
|
||||||
async function loadLabelOptions(setLabels, onError) {
|
async function loadLabelOptions(setLabels, onError) {
|
||||||
let labels;
|
let labels;
|
||||||
try {
|
try {
|
||||||
@@ -39,35 +64,17 @@ async function loadLabelOptions(setLabels, onError) {
|
|||||||
|
|
||||||
function LabelSelect({ value, placeholder, onChange, onError }) {
|
function LabelSelect({ value, placeholder, onChange, onError }) {
|
||||||
const [options, setOptions] = useState([]);
|
const [options, setOptions] = useState([]);
|
||||||
const [currentValue, setCurrentValue] = useState([]);
|
const [currentValue, handleSelect] = usePFSelect(value, options, onChange);
|
||||||
const [isExpanded, setIsExpanded] = useState(false);
|
const [isExpanded, setIsExpanded] = useState(false);
|
||||||
|
|
||||||
const toggleExpanded = () => {
|
const toggleExpanded = () => {
|
||||||
setIsExpanded(!isExpanded);
|
setIsExpanded(!isExpanded);
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleSelect = (event, item) => {
|
|
||||||
if (currentValue.includes(item)) {
|
|
||||||
onChange(currentValue.filter(i => i.id !== item.id));
|
|
||||||
} else {
|
|
||||||
onChange(currentValue.concat(item));
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
loadLabelOptions(setOptions, onError);
|
loadLabelOptions(setOptions, onError);
|
||||||
}, [onError]);
|
}, [onError]);
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
if (value !== currentValue && options.length) {
|
|
||||||
const syncedValue = value.map(item =>
|
|
||||||
options.find(i => i.id === item.id)
|
|
||||||
);
|
|
||||||
setCurrentValue(syncedValue);
|
|
||||||
}
|
|
||||||
/* eslint-disable-next-line react-hooks/exhaustive-deps */
|
|
||||||
}, [value, options]);
|
|
||||||
|
|
||||||
const renderOptions = opts => {
|
const renderOptions = opts => {
|
||||||
return opts.map(option => (
|
return opts.map(option => (
|
||||||
<SelectOption key={option.id} value={option}>
|
<SelectOption key={option.id} value={option}>
|
||||||
|
|||||||
Reference in New Issue
Block a user