| 1 |
import { Fragment, useState } from 'react'; |
| 2 |
import { Label, LabelColor } from '@patternfly/react-core'; |
| 3 |
|
| 4 |
export const LabelEditable: React.FunctionComponent = () => { |
| 5 |
const [labelText, setLabelText] = useState('Editable label'); |
| 6 |
const [compactLabelText, setCompactLabelText] = useState('Compact editable label'); |
| 7 |
|
| 8 |
const onEditCancel = (_event: KeyboardEvent, prevText: string) => { |
| 9 |
setLabelText(prevText); |
| 10 |
}; |
| 11 |
|
| 12 |
const onEditComplete = (_event: MouseEvent | KeyboardEvent, text: string) => { |
| 13 |
setLabelText(text); |
| 14 |
}; |
| 15 |
|
| 16 |
const onCompactEditCancel = (_event: KeyboardEvent, prevText: string) => { |
| 17 |
setCompactLabelText(prevText); |
| 18 |
}; |
| 19 |
|
| 20 |
const onCompactEditComplete = (_event: MouseEvent | KeyboardEvent, text: string) => { |
| 21 |
setCompactLabelText(text); |
| 22 |
}; |
| 23 |
|
| 24 |
return ( |
| 25 |
<Fragment> |
| 26 |
<Label |
| 27 |
color={LabelColor.blue} |
| 28 |
onClose={() => {}} |
| 29 |
closeBtnAriaLabel="Custom close button for editable label" |
| 30 |
onEditCancel={onEditCancel} |
| 31 |
onEditComplete={onEditComplete} |
| 32 |
isEditable |
| 33 |
editableProps={{ |
| 34 |
'aria-label': `Editable label with text ${labelText}`, |
| 35 |
id: 'editable-label' |
| 36 |
}} |
| 37 |
> |
| 38 |
{labelText} |
| 39 |
</Label> |
| 40 |
<Label |
| 41 |
color={LabelColor.grey} |
| 42 |
isCompact |
| 43 |
onClose={() => {}} |
| 44 |
closeBtnAriaLabel="Custom close button for compact editable label" |
| 45 |
onEditCancel={onCompactEditCancel} |
| 46 |
onEditComplete={onCompactEditComplete} |
| 47 |
isEditable |
| 48 |
editableProps={{ |
| 49 |
'aria-label': `Editable compact label with text ${compactLabelText}`, |
| 50 |
id: 'compact-editable-label' |
| 51 |
}} |
| 52 |
> |
| 53 |
{compactLabelText} |
| 54 |
</Label> |
| 55 |
</Fragment> |
| 56 |
); |
| 57 |
}; |