-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
462 additions
and
130 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import React, { useCallback, useEffect, useRef } from 'react'; | ||
import ContentEditable, { ContentEditableEvent } from 'react-contenteditable'; | ||
import * as sanitize from 'sanitize-html'; | ||
|
||
interface EditableTextProps extends Omit<React.HTMLAttributes<HTMLElement>, 'onChange' | 'children'> { | ||
as: string; | ||
disabled: boolean; | ||
value: string; | ||
onChange(nextValue: string): void; | ||
maxChars?: number; | ||
} | ||
|
||
export const EditableText: React.FC<EditableTextProps> = ({ | ||
as, | ||
disabled, | ||
maxChars = 300, | ||
value, | ||
onChange, | ||
...restProps | ||
}) => { | ||
const elementRef = useRef<HTMLElement | null>(null); | ||
const handleTextChange = useCallback( | ||
(evt: ContentEditableEvent) => { | ||
const nextValue = sanitize(evt.target.value, { | ||
allowedTags: [], | ||
}).substring(0, maxChars); | ||
onChange(nextValue); | ||
}, | ||
[onChange], | ||
); | ||
|
||
const handleKeyPress = useCallback( | ||
(event: React.KeyboardEvent<HTMLDivElement>) => { | ||
const { key } = event; | ||
const enterPressed = key === 'Enter'; | ||
const deleteButtonPressed = key === 'Backspace' || key === 'Delete'; | ||
const replacing = window.getSelection()?.toString() !== ''; | ||
const limitReached = elementRef.current?.innerText.length ?? 0 >= maxChars; | ||
if (enterPressed || (limitReached && !replacing && !deleteButtonPressed)) { | ||
event.stopPropagation(); | ||
event.preventDefault(); | ||
return false; | ||
} | ||
|
||
return undefined; | ||
}, | ||
[maxChars], | ||
); | ||
|
||
useEffect(() => { | ||
if (!disabled) { | ||
elementRef.current?.focus(); | ||
} | ||
}, [disabled]); | ||
|
||
return ( | ||
<ContentEditable | ||
tagName={as} | ||
innerRef={elementRef} | ||
disabled={disabled} | ||
html={value} | ||
onKeyDown={handleKeyPress} | ||
onKeyUp={handleKeyPress} | ||
onChange={handleTextChange} | ||
{...restProps} | ||
/> | ||
); | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import React from 'react'; | ||
|
||
interface StickyLabelProps { | ||
visible: boolean; | ||
className?: string; | ||
children: React.ReactNode; | ||
} | ||
export const StickyLabel: React.FC<StickyLabelProps> = ({ visible, className, children }) => { | ||
if (!visible) return null; | ||
|
||
return ( | ||
<div | ||
className={`absolute -top-[22px] -left-[2px] px-[10px] text-sm text-white rounded-t-lg normal-case ${className}`} | ||
> | ||
{children} | ||
</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
Oops, something went wrong.