-
Notifications
You must be signed in to change notification settings - Fork 178
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Model Registry: Initial content of model details tab with stub editable fields and tab navigation handling #2760
Merged
openshift-merge-bot
merged 1 commit into
opendatahub-io:main
from
mturley:RHOAIENG-2235-model-details-section
May 1, 2024
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
.odh-custom-description-list-term-with-action > span { | ||
/* Workaround for missing functionality in PF DescriptionList, see https://github.com/patternfly/patternfly/issues/6583 */ | ||
width: 100%; | ||
} |
108 changes: 108 additions & 0 deletions
108
frontend/src/components/DashboardDescriptionListGroup.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
import * as React from 'react'; | ||
import { | ||
ActionList, | ||
ActionListItem, | ||
Button, | ||
DescriptionListDescription, | ||
DescriptionListGroup, | ||
DescriptionListTerm, | ||
Split, | ||
SplitItem, | ||
} from '@patternfly/react-core'; | ||
import text from '@patternfly/react-styles/css/utilities/Text/text'; | ||
import { CheckIcon, PencilAltIcon, TimesIcon } from '@patternfly/react-icons'; | ||
|
||
import './DashboardDescriptionListGroup.scss'; | ||
|
||
type EditableProps = { | ||
isEditing: boolean; | ||
contentWhenEditing: React.ReactNode; | ||
isSavingEdits?: boolean; | ||
onEditClick: () => void; | ||
onSaveEditsClick: () => void; | ||
onDiscardEditsClick: () => void; | ||
}; | ||
|
||
export type DashboardDescriptionListGroupProps = { | ||
title: React.ReactNode; | ||
action?: React.ReactNode; | ||
isEmpty?: boolean; | ||
contentWhenEmpty?: React.ReactNode; | ||
children: React.ReactNode; | ||
} & (({ isEditable: true } & EditableProps) | ({ isEditable?: false } & Partial<EditableProps>)); | ||
|
||
const DashboardDescriptionListGroup: React.FC<DashboardDescriptionListGroupProps> = (props) => { | ||
const { | ||
title, | ||
action, | ||
isEmpty, | ||
contentWhenEmpty, | ||
isEditable = false, | ||
isEditing, | ||
contentWhenEditing, | ||
isSavingEdits = false, | ||
onEditClick, | ||
onSaveEditsClick, | ||
onDiscardEditsClick, | ||
children, | ||
} = props; | ||
return ( | ||
<DescriptionListGroup> | ||
{action || isEditable ? ( | ||
<DescriptionListTerm className="odh-custom-description-list-term-with-action"> | ||
<Split> | ||
<SplitItem isFilled>{title}</SplitItem> | ||
<SplitItem> | ||
{action || | ||
(isEditing ? ( | ||
<ActionList isIconList> | ||
<ActionListItem> | ||
<Button | ||
data-testid={`save-edit-button-${title}`} | ||
aria-label={`Save edits to ${title}`} | ||
variant="link" | ||
onClick={onSaveEditsClick} | ||
isDisabled={isSavingEdits} | ||
> | ||
<CheckIcon /> | ||
</Button> | ||
</ActionListItem> | ||
<ActionListItem> | ||
<Button | ||
data-testid={`discard-edit-button-${title} `} | ||
aria-label={`Discard edits to ${title} `} | ||
variant="plain" | ||
onClick={onDiscardEditsClick} | ||
isDisabled={isSavingEdits} | ||
> | ||
<TimesIcon /> | ||
</Button> | ||
</ActionListItem> | ||
</ActionList> | ||
) : ( | ||
<Button | ||
data-testid={`edit-button-${title}`} | ||
aria-label={`Edit ${title}`} | ||
isInline | ||
variant="link" | ||
icon={<PencilAltIcon />} | ||
iconPosition="end" | ||
onClick={onEditClick} | ||
> | ||
Edit | ||
</Button> | ||
))} | ||
</SplitItem> | ||
</Split> | ||
</DescriptionListTerm> | ||
) : ( | ||
<DescriptionListTerm>{title}</DescriptionListTerm> | ||
)} | ||
<DescriptionListDescription className={isEmpty && !isEditing ? text.disabledColor_100 : ''}> | ||
{isEditing ? contentWhenEditing : isEmpty ? contentWhenEmpty : children} | ||
</DescriptionListDescription> | ||
</DescriptionListGroup> | ||
); | ||
}; | ||
|
||
export default DashboardDescriptionListGroup; |
193 changes: 193 additions & 0 deletions
193
frontend/src/components/EditableLabelsDescriptionListGroup.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,193 @@ | ||
import * as React from 'react'; | ||
import { | ||
Button, | ||
Form, | ||
FormGroup, | ||
FormHelperText, | ||
HelperText, | ||
HelperTextItem, | ||
Label, | ||
LabelGroup, | ||
Modal, | ||
TextInput, | ||
} from '@patternfly/react-core'; | ||
import { ExclamationCircleIcon } from '@patternfly/react-icons'; | ||
import DashboardDescriptionListGroup, { | ||
DashboardDescriptionListGroupProps, | ||
} from './DashboardDescriptionListGroup'; | ||
|
||
type EditableTextDescriptionListGroupProps = Partial< | ||
Pick<DashboardDescriptionListGroupProps, 'title' | 'contentWhenEmpty'> | ||
> & { | ||
labels: string[]; | ||
saveEditedLabels: (labels: string[]) => Promise<void>; | ||
}; | ||
|
||
const EditableLabelsDescriptionListGroup: React.FC<EditableTextDescriptionListGroupProps> = ({ | ||
title = 'Labels', | ||
contentWhenEmpty = 'No labels', | ||
labels, | ||
saveEditedLabels, | ||
}) => { | ||
const [isEditing, setIsEditing] = React.useState(false); | ||
const [unsavedLabels, setUnsavedLabels] = React.useState(labels); | ||
const [isSavingEdits, setIsSavingEdits] = React.useState(false); | ||
|
||
const editUnsavedLabel = (newText: string, index: number) => { | ||
if (isSavingEdits) { | ||
return; | ||
} | ||
const copy = [...unsavedLabels]; | ||
copy[index] = newText; | ||
setUnsavedLabels(copy); | ||
}; | ||
const removeUnsavedLabel = (text: string) => { | ||
if (isSavingEdits) { | ||
return; | ||
} | ||
setUnsavedLabels(unsavedLabels.filter((label) => label !== text)); | ||
}; | ||
const addUnsavedLabel = (text: string) => { | ||
if (isSavingEdits) { | ||
return; | ||
} | ||
setUnsavedLabels([...unsavedLabels, text]); | ||
}; | ||
|
||
const [isAddLabelModalOpen, setIsAddLabelModalOpen] = React.useState(false); | ||
const [addLabelInputValue, setAddLabelInputValue] = React.useState(''); | ||
const addLabelInputRef = React.useRef<HTMLInputElement>(null); | ||
const addLabelInputTooLong = addLabelInputValue.length > 63; | ||
|
||
const toggleAddLabelModal = () => { | ||
setAddLabelInputValue(''); | ||
setIsAddLabelModalOpen(!isAddLabelModalOpen); | ||
}; | ||
React.useEffect(() => { | ||
if (isAddLabelModalOpen && addLabelInputRef.current) { | ||
addLabelInputRef.current.focus(); | ||
} | ||
}, [isAddLabelModalOpen]); | ||
const addLabelModalSubmitDisabled = !addLabelInputValue || addLabelInputTooLong; | ||
const submitAddLabelModal = (event?: React.FormEvent) => { | ||
event?.preventDefault(); | ||
if (!addLabelModalSubmitDisabled) { | ||
addUnsavedLabel(addLabelInputValue); | ||
toggleAddLabelModal(); | ||
} | ||
}; | ||
|
||
return ( | ||
<> | ||
<DashboardDescriptionListGroup | ||
title={title} | ||
isEmpty={labels.length === 0} | ||
contentWhenEmpty={contentWhenEmpty} | ||
isEditable | ||
isEditing={isEditing} | ||
isSavingEdits={isSavingEdits} | ||
contentWhenEditing={ | ||
<LabelGroup | ||
isEditable={!isSavingEdits} | ||
numLabels={unsavedLabels.length} | ||
addLabelControl={ | ||
!isSavingEdits && ( | ||
<Label color="blue" variant="outline" isOverflowLabel onClick={toggleAddLabelModal}> | ||
Add label | ||
</Label> | ||
) | ||
} | ||
> | ||
{unsavedLabels.map((label, index) => ( | ||
<Label | ||
key={label} | ||
color="blue" | ||
data-testid="label" | ||
isEditable={!isSavingEdits} | ||
editableProps={{ 'aria-label': `Editable label with text ${label}` }} | ||
onClose={() => removeUnsavedLabel(label)} | ||
closeBtnProps={{ isDisabled: isSavingEdits }} | ||
onEditComplete={(_event, newText) => editUnsavedLabel(newText, index)} | ||
> | ||
{label} | ||
</Label> | ||
))} | ||
</LabelGroup> | ||
} | ||
onEditClick={() => { | ||
setUnsavedLabels(labels); | ||
setIsEditing(true); | ||
}} | ||
onSaveEditsClick={async () => { | ||
setIsSavingEdits(true); | ||
try { | ||
await saveEditedLabels(unsavedLabels); | ||
} finally { | ||
setIsSavingEdits(false); | ||
} | ||
setIsEditing(false); | ||
}} | ||
onDiscardEditsClick={() => { | ||
setUnsavedLabels(labels); | ||
setIsEditing(false); | ||
}} | ||
> | ||
<LabelGroup> | ||
{labels.map((label) => ( | ||
<Label key={label} color="blue" data-testid="label"> | ||
{label} | ||
</Label> | ||
))} | ||
</LabelGroup> | ||
</DashboardDescriptionListGroup> | ||
<Modal | ||
variant="small" | ||
title="Add label" | ||
isOpen={isAddLabelModalOpen} | ||
onClose={toggleAddLabelModal} | ||
actions={[ | ||
<Button | ||
key="save" | ||
variant="primary" | ||
form="add-label-form" | ||
onClick={submitAddLabelModal} | ||
isDisabled={addLabelModalSubmitDisabled} | ||
> | ||
Save | ||
</Button>, | ||
<Button key="cancel" variant="link" onClick={toggleAddLabelModal}> | ||
Cancel | ||
</Button>, | ||
]} | ||
> | ||
<Form id="add-label-form" onSubmit={submitAddLabelModal}> | ||
<FormGroup label="Label text" fieldId="add-label-form-label-text" isRequired> | ||
<TextInput | ||
type="text" | ||
id="add-label-form-label-text" | ||
name="add-label-form-label-text" | ||
value={addLabelInputValue} | ||
onChange={(_event: React.FormEvent<HTMLInputElement>, value: string) => | ||
setAddLabelInputValue(value) | ||
} | ||
ref={addLabelInputRef} | ||
isRequired | ||
validated={addLabelInputTooLong ? 'error' : 'default'} | ||
/> | ||
{addLabelInputTooLong && ( | ||
<FormHelperText> | ||
<HelperText> | ||
<HelperTextItem icon={<ExclamationCircleIcon />} variant="error"> | ||
Label text can't exceed 63 characters | ||
</HelperTextItem> | ||
</HelperText> | ||
</FormHelperText> | ||
)} | ||
</FormGroup> | ||
</Form> | ||
</Modal> | ||
</> | ||
); | ||
}; | ||
|
||
export default EditableLabelsDescriptionListGroup; |
73 changes: 73 additions & 0 deletions
73
frontend/src/components/EditableTextDescriptionListGroup.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import * as React from 'react'; | ||
import { ExpandableSection, TextArea } from '@patternfly/react-core'; | ||
import DashboardDescriptionListGroup, { | ||
DashboardDescriptionListGroupProps, | ||
} from './DashboardDescriptionListGroup'; | ||
|
||
type EditableTextDescriptionListGroupProps = Pick< | ||
DashboardDescriptionListGroupProps, | ||
'title' | 'contentWhenEmpty' | ||
> & { | ||
value: string; | ||
saveEditedValue: (value: string) => Promise<void>; | ||
}; | ||
|
||
const EditableTextDescriptionListGroup: React.FC<EditableTextDescriptionListGroupProps> = ({ | ||
title, | ||
contentWhenEmpty, | ||
value, | ||
saveEditedValue, | ||
}) => { | ||
const [isEditing, setIsEditing] = React.useState(false); | ||
const [unsavedValue, setUnsavedValue] = React.useState(value); | ||
const [isSavingEdits, setIsSavingEdits] = React.useState(false); | ||
const [isTextExpanded, setIsTextExpanded] = React.useState(false); | ||
return ( | ||
<DashboardDescriptionListGroup | ||
title={title} | ||
isEmpty={!value} | ||
contentWhenEmpty={contentWhenEmpty} | ||
isEditable | ||
isEditing={isEditing} | ||
isSavingEdits={isSavingEdits} | ||
contentWhenEditing={ | ||
<TextArea | ||
data-testid={`edit-text-area-${title}`} | ||
aria-label={`Text box for editing ${title}`} | ||
value={unsavedValue} | ||
onChange={(_event, v) => setUnsavedValue(v)} | ||
isDisabled={isSavingEdits} | ||
/> | ||
} | ||
onEditClick={() => { | ||
setUnsavedValue(value); | ||
setIsEditing(true); | ||
}} | ||
onSaveEditsClick={async () => { | ||
setIsSavingEdits(true); | ||
try { | ||
await saveEditedValue(unsavedValue); | ||
} finally { | ||
setIsSavingEdits(false); | ||
} | ||
setIsEditing(false); | ||
}} | ||
onDiscardEditsClick={() => { | ||
setUnsavedValue(value); | ||
setIsEditing(false); | ||
}} | ||
> | ||
<ExpandableSection | ||
variant="truncate" | ||
truncateMaxLines={12} | ||
toggleText={isTextExpanded ? 'Show less' : 'Show more'} | ||
onToggle={(_event, isExpanded) => setIsTextExpanded(isExpanded)} | ||
isExpanded={isTextExpanded} | ||
> | ||
{value} | ||
</ExpandableSection> | ||
</DashboardDescriptionListGroup> | ||
); | ||
}; | ||
|
||
export default EditableTextDescriptionListGroup; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I feel that if this is gonna be reused in the details view we can move it into a utils right?
Not needed right now, can be refactor on @dpanshug pr
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Resolving conversation it can be done in another PR
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This whole
EditableLabelsDescriptionListGroup
is already ready to be reused anywhere in the UI, will just drop it in for version details