generated from CDCgov/template
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Click-To-Edit functionality (#250)
* Make Extracted Data be sortable Updates SortableTable to pass in index and entire row data into formatters Use SortableTable on Review page Create custom formatter for adding exclamation icon and coloring rows red * Add click to edit functionality Adds the click to edit functions for the review and edit page Return will commit changes Escape will revert changes since last commit Clicking outside of the text box will commit changes If there are errors Submit button on review page will be disabled Hovering over content will display icon (transition of icon will respect reduce motion preferences) Minor tsconfig tweak to remove error when importing syntheticDefaultImports Fix bug with Sortable Table passing in wrong index to formatter functions * Update Tests
- Loading branch information
Showing
7 changed files
with
182 additions
and
48 deletions.
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
19 changes: 19 additions & 0 deletions
19
OCR/frontend/src/components/EditableText/EditableText.scss
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,19 @@ | ||
.hover-display-icon { | ||
svg { | ||
opacity: 0; | ||
@media (prefers-reduced-motion: no-preference) { | ||
transition: opacity 0.2s ease-in-out; | ||
} | ||
} | ||
} | ||
|
||
.hover-display-icon:hover { | ||
|
||
svg { | ||
opacity: 1; | ||
@media (prefers-reduced-motion: no-preference) { | ||
transition: opacity 0.2s ease-in-out; | ||
} | ||
} | ||
} | ||
|
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,83 @@ | ||
import React, {FC, ReactNode, useEffect, useRef, useState} from 'react'; | ||
import {Icon, TextInput} from "@trussworks/react-uswds"; | ||
|
||
import './EditableText.scss' | ||
|
||
interface EditableTextProps { | ||
text: string, | ||
onChange: (text: string) => void, | ||
onSave: (value: string) => void, | ||
onCancel: () => void, | ||
onEdit: () => void, | ||
isEditing: boolean, | ||
isError: boolean, | ||
errorMessage: string, | ||
onValidate: (text: string) => boolean | string[] | ||
textFormatter: (text: string) => ReactNode | string | ||
dataTestId?: string | ||
} | ||
|
||
export const EditableText: FC<EditableTextProps> = ({ | ||
text, dataTestId, | ||
onChange = () => { | ||
}, | ||
onSave = () => { | ||
}, | ||
textFormatter = (text) => text | ||
}: EditableTextProps) => { | ||
const [isEditing, setIsEditing] = useState(false) | ||
const [value, setValue] = useState(text) | ||
const inputRef = useRef<HTMLInputElement | null>(null) | ||
useEffect(() => { | ||
if (isEditing) { | ||
inputRef.current?.focus() | ||
} | ||
}, [isEditing]) | ||
|
||
useEffect(() => { | ||
setValue(text) | ||
}, [text]) | ||
|
||
const _onChange = (e: React.ChangeEvent<HTMLInputElement>) => { | ||
setValue(e.target.value) | ||
onChange(e.target.value) | ||
} | ||
|
||
const _onEdit = () => { | ||
setIsEditing(true) | ||
// onEdit() | ||
} | ||
|
||
const _onSave = () => { | ||
setIsEditing(false) | ||
onSave(value) | ||
} | ||
|
||
const _onCancel = () => { | ||
setValue(text) | ||
setIsEditing(false) | ||
} | ||
const _onKeyDown = (e: React.KeyboardEvent<HTMLInputElement>) => { | ||
if (e.key === 'Enter') { | ||
_onSave() | ||
} | ||
if (e.key === 'Escape') { | ||
_onCancel() | ||
} | ||
} | ||
|
||
return (<> | ||
{isEditing ? | ||
<TextInput inputRef={inputRef} className="margin-0 padding-0" type="text" value={value} onChange={_onChange} | ||
onBlur={_onSave} onKeyDown={_onKeyDown} id={value}/> : | ||
<div data-testid={dataTestId} className="display-flex flex-align-center hover-display-icon" | ||
onClick={_onEdit}> | ||
<div className="font-sans-md font-weight-semibold">{textFormatter(value)}</div> | ||
<div className="flex-1"></div> | ||
<div className="padding-left-1 padding-right-1 custom-tooltip-container display-flex flex-align-end"> | ||
<Icon.Edit aria-hidden={true}/> | ||
</div> | ||
</div>} | ||
|
||
</>) | ||
} |
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
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
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
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