| 1 |
import { useEffect, useRef, useState } from 'react'; |
| 2 |
import { |
| 3 |
LabelGroup, |
| 4 |
Label, |
| 5 |
LabelColor, |
| 6 |
Button, |
| 7 |
Form, |
| 8 |
FormGroup, |
| 9 |
TextInput, |
| 10 |
Menu, |
| 11 |
MenuContent, |
| 12 |
MenuList, |
| 13 |
MenuItem, |
| 14 |
MenuToggle, |
| 15 |
Radio, |
| 16 |
Popper |
| 17 |
} from '@patternfly/react-core'; |
| 18 |
import { Modal as ModalDeprecated, ModalVariant as ModalVariantDeprecated } from '@patternfly/react-core/deprecated'; |
| 19 |
import RhUiInformationFillIcon from '@patternfly/react-icons/dist/esm/icons/rh-ui-information-fill-icon'; |
| 20 |
|
| 21 |
export const LabelGroupEditableAddModal: React.FunctionComponent = () => { |
| 22 |
const [isModalOpen, setModalOpen] = useState<boolean>(false); |
| 23 |
const [idIndex, setIdIndex] = useState<number>(7); |
| 24 |
const [labelText, setLabelText] = useState<string>(''); |
| 25 |
const [color, setColor] = useState<string>(); |
| 26 |
const [icon, setIcon] = useState<any>(); |
| 27 |
const [labelType, setLabelType] = useState<string>('filled'); |
| 28 |
const [isClosable, setIsCloseable] = useState<boolean>(false); |
| 29 |
const [isEditable, setIsEditable] = useState<boolean>(false); |
| 30 |
const labelInputRef = useRef(null); |
| 31 |
|
| 32 |
const [isColorOpen, setIsColorOpen] = useState<boolean>(false); |
| 33 |
const colorMenuRef = useRef<HTMLDivElement>(null); |
| 34 |
const colorContainerRef = useRef<HTMLDivElement>(null); |
| 35 |
const colorToggleRef = useRef<HTMLButtonElement>(null); |
| 36 |
|
| 37 |
const [isIconOpen, setIsIconOpen] = useState<boolean>(false); |
| 38 |
const iconMenuRef = useRef<HTMLDivElement>(null); |
| 39 |
const iconContainerRef = useRef<HTMLDivElement>(null); |
| 40 |
const iconToggleRef = useRef<HTMLButtonElement>(null); |
| 41 |
|
| 42 |
const [labels, setLabels] = useState<any>([ |
| 43 |
{ name: 'Label 1', id: 4 }, |
| 44 |
{ name: 'Label 2', id: 5 }, |
| 45 |
{ |
| 46 |
name: 'Label 3', |
| 47 |
props: { |
| 48 |
isEditable: true, |
| 49 |
editableProps: { |
| 50 |
'aria-label': 'Editable label with text Label 3' |
| 51 |
} |
| 52 |
}, |
| 53 |
id: 6 |
| 54 |
} |
| 55 |
]); |
| 56 |
|
| 57 |
const onClose = (labelId: string) => { |
| 58 |
setLabels(labels.filter((l: any) => l.id !== labelId)); |
| 59 |
}; |
| 60 |
|
| 61 |
const onEdit = (nextText: string, index: number) => { |
| 62 |
const copy = [...labels]; |
| 63 |
const updatedProps = { |
| 64 |
...labels[index].props, |
| 65 |
editableProps: { 'aria-label': `Editable label with text ${nextText}` } |
| 66 |
}; |
| 67 |
|
| 68 |
copy[index] = { name: nextText, props: updatedProps, id: labels[index].id }; |
| 69 |
setLabels(copy); |
| 70 |
}; |
| 71 |
|
| 72 |
const onAdd = () => { |
| 73 |
let labelIcon: any; |
| 74 |
if (icon === 'Info circle icon') { |
| 75 |
labelIcon = <RhUiInformationFillIcon />; |
| 76 |
} |
| 77 |
|
| 78 |
setLabels([ |
| 79 |
{ |
| 80 |
name: labelText || 'New Label', |
| 81 |
props: { |
| 82 |
color: color ? color.toLowerCase() : 'gray', |
| 83 |
icon: labelIcon, |
| 84 |
variant: labelType || 'filled', |
| 85 |
...(!isClosable && isClosable !== undefined && { onClose: null }), |
| 86 |
isEditable: isEditable !== undefined ? isEditable : true, |
| 87 |
...(isEditable && { |
| 88 |
editableProps: { |
| 89 |
'aria-label': `Editable label with text ${labelText || 'New Label'}` |
| 90 |
} |
| 91 |
}) |
| 92 |
}, |
| 93 |
id: idIndex |
| 94 |
}, |
| 95 |
...labels |
| 96 |
]); |
| 97 |
setModalOpen(!isModalOpen); |
| 98 |
setIdIndex(idIndex + 1); |
| 99 |
setLabelText(''); |
| 100 |
setColor(''); |
| 101 |
setIcon(null); |
| 102 |
setLabelType('filled'); |
| 103 |
setIsCloseable(false); |
| 104 |
setIsEditable(false); |
| 105 |
}; |
| 106 |
|
| 107 |
const handleModalToggle = (_event: KeyboardEvent | React.MouseEvent) => { |
| 108 |
setModalOpen(!isModalOpen); |
| 109 |
}; |
| 110 |
|
| 111 |
useEffect(() => { |
| 112 |
if (isModalOpen && labelInputRef && labelInputRef.current) { |
| 113 |
(labelInputRef.current as HTMLInputElement).focus(); |
| 114 |
} |
| 115 |
}, [isModalOpen]); |
| 116 |
|
| 117 |
const handleMenuKeys = (event: KeyboardEvent) => { |
| 118 |
if (isColorOpen && colorMenuRef?.current?.contains(event.target as Node)) { |
| 119 |
if (event.key === 'Escape' || event.key === 'Tab') { |
| 120 |
setIsColorOpen(!isColorOpen); |
| 121 |
colorToggleRef?.current?.focus(); |
| 122 |
} |
| 123 |
} |
| 124 |
if (isIconOpen && iconMenuRef?.current?.contains(event.target as Node)) { |
| 125 |
if (event.key === 'Escape' || event.key === 'Tab') { |
| 126 |
setIsIconOpen(!isIconOpen); |
| 127 |
iconToggleRef?.current?.focus(); |
| 128 |
} |
| 129 |
} |
| 130 |
}; |
| 131 |
|
| 132 |
const handleClickOutside = (event: MouseEvent) => { |
| 133 |
if (isColorOpen && !colorMenuRef?.current?.contains(event.target as Node)) { |
| 134 |
setIsColorOpen(false); |
| 135 |
} |
| 136 |
if (isIconOpen && !iconMenuRef?.current?.contains(event.target as Node)) { |
| 137 |
setIsIconOpen(false); |
| 138 |
} |
| 139 |
}; |
| 140 |
|
| 141 |
useEffect(() => { |
| 142 |
window.addEventListener('keydown', handleMenuKeys); |
| 143 |
window.addEventListener('click', handleClickOutside); |
| 144 |
return () => { |
| 145 |
window.removeEventListener('keydown', handleMenuKeys); |
| 146 |
window.removeEventListener('click', handleClickOutside); |
| 147 |
}; |
| 148 |
}, [isColorOpen, isIconOpen, colorMenuRef, iconMenuRef]); |
| 149 |
|
| 150 |
const onColorToggleClick = (ev: React.MouseEvent) => { |
| 151 |
ev.stopPropagation(); // Stop handleClickOutside from handling |
| 152 |
setTimeout(() => { |
| 153 |
if (colorMenuRef.current) { |
| 154 |
const firstElement = colorMenuRef.current.querySelector('li > button:not(:disabled)'); |
| 155 |
firstElement && (firstElement as HTMLElement).focus(); |
| 156 |
} |
| 157 |
}, 0); |
| 158 |
setIsColorOpen(!isColorOpen); |
| 159 |
}; |
| 160 |
|
| 161 |
const colorToggle = ( |
| 162 |
<MenuToggle ref={colorToggleRef} onClick={onColorToggleClick} isExpanded={isColorOpen}> |
| 163 |
{color || 'Select'} |
| 164 |
</MenuToggle> |
| 165 |
); |
| 166 |
|
| 167 |
const colorMenu = ( |
| 168 |
<Menu |
| 169 |
ref={colorMenuRef} |
| 170 |
activeItemId={color} |
| 171 |
onSelect={(_ev, itemId) => { |
| 172 |
setColor(itemId?.toString()); |
| 173 |
colorToggleRef?.current?.focus(); |
| 174 |
setIsColorOpen(false); |
| 175 |
}} |
| 176 |
selected={color} |
| 177 |
> |
| 178 |
<MenuContent> |
| 179 |
<MenuList> |
| 180 |
<MenuItem itemId="Gray">Gray</MenuItem> |
| 181 |
<MenuItem itemId="Blue">Blue</MenuItem> |
| 182 |
<MenuItem itemId="Green">Green</MenuItem> |
| 183 |
<MenuItem itemId="Orange">Orange</MenuItem> |
| 184 |
<MenuItem itemId="Red">Red</MenuItem> |
| 185 |
<MenuItem itemId="Purple">Purple</MenuItem> |
| 186 |
<MenuItem itemId="Teal">Teal</MenuItem> |
| 187 |
</MenuList> |
| 188 |
</MenuContent> |
| 189 |
</Menu> |
| 190 |
); |
| 191 |
|
| 192 |
const onIconToggleClick = (ev: React.MouseEvent) => { |
| 193 |
ev.stopPropagation(); // Stop handleClickOutside from handling |
| 194 |
setTimeout(() => { |
| 195 |
if (iconMenuRef.current) { |
| 196 |
const firstElement = iconMenuRef.current.querySelector('li > button:not(:disabled)'); |
| 197 |
firstElement && (firstElement as HTMLElement).focus(); |
| 198 |
} |
| 199 |
}, 0); |
| 200 |
setIsIconOpen(!isIconOpen); |
| 201 |
}; |
| 202 |
|
| 203 |
const iconToggle = ( |
| 204 |
<MenuToggle ref={iconToggleRef} onClick={onIconToggleClick} isExpanded={isIconOpen} icon={icon}> |
| 205 |
{icon ? '' : 'Select'} |
| 206 |
</MenuToggle> |
| 207 |
); |
| 208 |
|
| 209 |
const iconMenu = ( |
| 210 |
<Menu |
| 211 |
ref={iconMenuRef} |
| 212 |
activeItemId={icon} |
| 213 |
onSelect={(_ev, itemId) => { |
| 214 |
setIcon(itemId?.toString()); |
| 215 |
iconToggleRef?.current?.focus(); |
| 216 |
setIsIconOpen(false); |
| 217 |
}} |
| 218 |
selected={icon} |
| 219 |
> |
| 220 |
<MenuContent> |
| 221 |
<MenuList> |
| 222 |
<MenuItem itemId="No icon">No icon</MenuItem> |
| 223 |
<MenuItem itemId="Info circle icon"> |
| 224 |
<RhUiInformationFillIcon /> |
| 225 |
Info circle icon |
| 226 |
</MenuItem> |
| 227 |
</MenuList> |
| 228 |
</MenuContent> |
| 229 |
</Menu> |
| 230 |
); |
| 231 |
|
| 232 |
return ( |
| 233 |
<div> |
| 234 |
<LabelGroup |
| 235 |
categoryName="Label group 1" |
| 236 |
numLabels={5} |
| 237 |
isEditable |
| 238 |
addLabelControl={ |
| 239 |
<Label variant="overflow" onClick={handleModalToggle}> |
| 240 |
Add label |
| 241 |
</Label> |
| 242 |
} |
| 243 |
> |
| 244 |
{labels.map((label: { name: string; id: string; props: any }, index: number) => ( |
| 245 |
<Label |
| 246 |
key={label.id} |
| 247 |
id={label.id} |
| 248 |
color={LabelColor.blue} |
| 249 |
onClose={() => onClose(label.id)} |
| 250 |
onEditCancel={(_event, prevText) => onEdit(prevText, index)} |
| 251 |
onEditComplete={(_event, newText) => onEdit(newText, index)} |
| 252 |
{...label.props} |
| 253 |
> |
| 254 |
{label.name} |
| 255 |
</Label> |
| 256 |
))} |
| 257 |
</LabelGroup> |
| 258 |
<ModalDeprecated |
| 259 |
variant={ModalVariantDeprecated.small} |
| 260 |
title="Add Label" |
| 261 |
isOpen={isModalOpen} |
| 262 |
onClose={handleModalToggle} |
| 263 |
actions={[ |
| 264 |
<Button key="create" variant="primary" form="create-label-form" onClick={onAdd}> |
| 265 |
Save |
| 266 |
</Button>, |
| 267 |
<Button key="cancel" variant="link" onClick={handleModalToggle}> |
| 268 |
Cancel |
| 269 |
</Button> |
| 270 |
]} |
| 271 |
> |
| 272 |
<Form id="create-label-form"> |
| 273 |
<FormGroup label="Label text" fieldId="create-label-form-label-text"> |
| 274 |
<TextInput |
| 275 |
type="text" |
| 276 |
id="create-label-form-label-text" |
| 277 |
name="create-label-form-label-text" |
| 278 |
value={labelText} |
| 279 |
onChange={(_event: React.FormEvent<HTMLInputElement>, value: string) => setLabelText(value)} |
| 280 |
ref={labelInputRef} |
| 281 |
/> |
| 282 |
</FormGroup> |
| 283 |
<FormGroup label="Color" fieldId="create-label-form-color"> |
| 284 |
<div ref={colorContainerRef}> |
| 285 |
<Popper |
| 286 |
trigger={colorToggle} |
| 287 |
triggerRef={colorToggleRef} |
| 288 |
popper={colorMenu} |
| 289 |
popperRef={colorMenuRef} |
| 290 |
appendTo={colorContainerRef.current as HTMLElement} |
| 291 |
isVisible={isColorOpen} |
| 292 |
/> |
| 293 |
</div> |
| 294 |
</FormGroup> |
| 295 |
<FormGroup label="Icon" fieldId="create-label-form-icon"> |
| 296 |
<div ref={iconContainerRef}> |
| 297 |
<Popper |
| 298 |
trigger={iconToggle} |
| 299 |
triggerRef={iconToggleRef} |
| 300 |
popper={iconMenu} |
| 301 |
popperRef={iconMenuRef} |
| 302 |
appendTo={iconContainerRef.current as HTMLElement} |
| 303 |
isVisible={isIconOpen} |
| 304 |
/> |
| 305 |
</div> |
| 306 |
</FormGroup> |
| 307 |
<FormGroup label="Label type" fieldId="create-label-form-label-type"> |
| 308 |
<Radio |
| 309 |
isChecked={labelType === 'filled'} |
| 310 |
name="filled-label" |
| 311 |
onChange={() => setLabelType('filled')} |
| 312 |
label="Filled" |
| 313 |
id="radio-filled" |
| 314 |
value="check1" |
| 315 |
/> |
| 316 |
<Radio |
| 317 |
isChecked={labelType === 'outline'} |
| 318 |
name="outline-label" |
| 319 |
onChange={() => setLabelType('outline')} |
| 320 |
label="Outlined" |
| 321 |
id="radio-outline" |
| 322 |
value="check2" |
| 323 |
/> |
| 324 |
</FormGroup> |
| 325 |
<FormGroup label="Dismissable" fieldId="create-label-form-dismissable"> |
| 326 |
<Radio |
| 327 |
isChecked={isClosable === true} |
| 328 |
name="closeable-label" |
| 329 |
onChange={() => setIsCloseable(true)} |
| 330 |
label="Yes" |
| 331 |
id="radio-closable" |
| 332 |
value="check1" |
| 333 |
/> |
| 334 |
<Radio |
| 335 |
isChecked={isClosable === false} |
| 336 |
name="not-closeable-label" |
| 337 |
onChange={() => setIsCloseable(false)} |
| 338 |
label="No" |
| 339 |
id="radio-not-closable" |
| 340 |
value="check2" |
| 341 |
/> |
| 342 |
</FormGroup> |
| 343 |
<FormGroup label="Editable" fieldId="create-label-form-editable"> |
| 344 |
<Radio |
| 345 |
isChecked={isEditable === true} |
| 346 |
name="editable-label" |
| 347 |
onChange={() => setIsEditable(true)} |
| 348 |
label="Yes" |
| 349 |
id="radio-editable" |
| 350 |
value="check3" |
| 351 |
/> |
| 352 |
<Radio |
| 353 |
isChecked={isEditable === false} |
| 354 |
name="not-editable-label" |
| 355 |
onChange={() => setIsEditable(false)} |
| 356 |
label="No" |
| 357 |
id="radio-not-editable" |
| 358 |
value="check4" |
| 359 |
/> |
| 360 |
</FormGroup> |
| 361 |
</Form> |
| 362 |
</ModalDeprecated> |
| 363 |
</div> |
| 364 |
); |
| 365 |
}; |