|
| 1 | +import React from 'react'; |
| 2 | +import { Color, FontVariation } from '@harnessio/design-system'; |
| 3 | +import { Button, ButtonSize, ButtonVariation, Text, TextInput, useToaster } from '@harnessio/uicore'; |
| 4 | +import css from './EditableStepName.module.scss'; |
| 5 | + |
| 6 | +export interface EditableStepNameProps { |
| 7 | + stepName?: string; |
| 8 | + faultName: string; |
| 9 | + onSave: (newStepName: string) => Promise<void>; |
| 10 | + fontSize?: FontVariation; |
| 11 | + showSubtitle?: boolean; |
| 12 | + disabled?: boolean; |
| 13 | +} |
| 14 | + |
| 15 | +export function EditableStepName({ |
| 16 | + stepName, |
| 17 | + faultName, |
| 18 | + onSave, |
| 19 | + fontSize = FontVariation.H5, |
| 20 | + showSubtitle = true, |
| 21 | + disabled = false |
| 22 | +}: EditableStepNameProps): React.ReactElement { |
| 23 | + const { showError } = useToaster(); |
| 24 | + const [isEditing, setIsEditing] = React.useState<boolean>(false); |
| 25 | + const [editedValue, setEditedValue] = React.useState<string>(''); |
| 26 | + const [isSaving, setIsSaving] = React.useState<boolean>(false); |
| 27 | + |
| 28 | + const displayName = stepName || faultName; |
| 29 | + const shouldShowSubtitle = showSubtitle && stepName && stepName !== faultName; |
| 30 | + |
| 31 | + const handleEditStart = (): void => { |
| 32 | + setEditedValue(displayName); |
| 33 | + setIsEditing(true); |
| 34 | + }; |
| 35 | + |
| 36 | + const handleSave = async (): Promise<void> => { |
| 37 | + if (editedValue.trim() && editedValue !== stepName) { |
| 38 | + setIsSaving(true); |
| 39 | + try { |
| 40 | + await onSave(editedValue.trim()); |
| 41 | + setIsEditing(false); |
| 42 | + setEditedValue(''); |
| 43 | + } catch (error) { |
| 44 | + showError('Failed to update step name'); |
| 45 | + } finally { |
| 46 | + setIsSaving(false); |
| 47 | + } |
| 48 | + } else { |
| 49 | + setIsEditing(false); |
| 50 | + setEditedValue(''); |
| 51 | + } |
| 52 | + }; |
| 53 | + |
| 54 | + const handleCancel = (): void => { |
| 55 | + setIsEditing(false); |
| 56 | + setEditedValue(''); |
| 57 | + }; |
| 58 | + |
| 59 | + const handleKeyDown = (e: React.KeyboardEvent<HTMLInputElement>): void => { |
| 60 | + if (e.key === 'Enter') { |
| 61 | + e.stopPropagation(); |
| 62 | + e.preventDefault(); |
| 63 | + handleSave(); |
| 64 | + } else if (e.key === 'Escape') { |
| 65 | + e.stopPropagation(); |
| 66 | + e.preventDefault(); |
| 67 | + handleCancel(); |
| 68 | + } |
| 69 | + }; |
| 70 | + |
| 71 | + if (isEditing) { |
| 72 | + return ( |
| 73 | + <div className={css.editMode}> |
| 74 | + <TextInput |
| 75 | + wrapperClassName={css.stepNameTextInput} |
| 76 | + value={editedValue} |
| 77 | + onChange={(e: React.ChangeEvent<HTMLInputElement>) => setEditedValue(e.target.value)} |
| 78 | + onKeyDown={handleKeyDown} |
| 79 | + autoFocus |
| 80 | + disabled={isSaving} |
| 81 | + /> |
| 82 | + <Button |
| 83 | + variation={ButtonVariation.ICON} |
| 84 | + icon="tick" |
| 85 | + size={ButtonSize.SMALL} |
| 86 | + onClick={handleSave} |
| 87 | + disabled={isSaving} |
| 88 | + loading={isSaving} |
| 89 | + /> |
| 90 | + <Button |
| 91 | + variation={ButtonVariation.ICON} |
| 92 | + icon="cross" |
| 93 | + size={ButtonSize.SMALL} |
| 94 | + onClick={handleCancel} |
| 95 | + disabled={isSaving} |
| 96 | + /> |
| 97 | + </div> |
| 98 | + ); |
| 99 | + } |
| 100 | + |
| 101 | + return ( |
| 102 | + <div className={css.displayMode}> |
| 103 | + <div className={css.stepNameTextContainer}> |
| 104 | + <Text font={{ variation: fontSize }}>{displayName}</Text> |
| 105 | + {shouldShowSubtitle && ( |
| 106 | + <Text color={Color.GREY_600} font={{ variation: FontVariation.SMALL, italic: true }}> |
| 107 | + {faultName} |
| 108 | + </Text> |
| 109 | + )} |
| 110 | + </div> |
| 111 | + {!disabled && ( |
| 112 | + <Button |
| 113 | + variation={ButtonVariation.ICON} |
| 114 | + icon="Edit" |
| 115 | + size={ButtonSize.SMALL} |
| 116 | + onClick={handleEditStart} |
| 117 | + minimal |
| 118 | + /> |
| 119 | + )} |
| 120 | + </div> |
| 121 | + ); |
| 122 | +} |
| 123 | + |
| 124 | +export default EditableStepName; |
0 commit comments