-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Organization working hours notice to End User with hyperlink (#1083)
* Added RichTextField * Added rich text area * remove rich text field
- Loading branch information
1 parent
4d6988a
commit d448337
Showing
3 changed files
with
128 additions
and
66 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
181 changes: 121 additions & 60 deletions
181
GUI/src/components/FormElements/FormTextarea/index.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 |
---|---|---|
@@ -1,72 +1,133 @@ | ||
import { ChangeEvent, forwardRef, useId, useState } from 'react'; | ||
import TextareaAutosize, { TextareaAutosizeProps } from 'react-textarea-autosize'; | ||
import TextareaAutosize, { | ||
TextareaAutosizeProps, | ||
} from 'react-textarea-autosize'; | ||
import { | ||
createRegexRenderer, | ||
RichTextarea, | ||
RichTextareaProps, | ||
} from 'rich-textarea'; | ||
import clsx from 'clsx'; | ||
|
||
import './FormTextarea.scss'; | ||
|
||
type TextareaProps = TextareaAutosizeProps & { | ||
label: string; | ||
name: string; | ||
hideLabel?: boolean; | ||
showMaxLength?: boolean; | ||
maxLengthBottom?: boolean; | ||
}; | ||
type TextareaProps = TextareaAutosizeProps & | ||
RichTextareaProps & { | ||
label: string; | ||
name: string; | ||
hideLabel?: boolean; | ||
showMaxLength?: boolean; | ||
maxLengthBottom?: boolean; | ||
useRichText?: boolean; | ||
}; | ||
|
||
const FormTextarea = forwardRef<HTMLTextAreaElement, TextareaProps>(( | ||
{ | ||
label, | ||
name, | ||
maxLength = 2000, | ||
minRows = 3, | ||
maxRows = 3, | ||
disabled, | ||
hideLabel, | ||
showMaxLength, | ||
maxLengthBottom, | ||
defaultValue, | ||
onChange, | ||
...rest | ||
}, | ||
ref, | ||
) => { | ||
const id = useId(); | ||
const [currentLength, setCurrentLength] = useState((typeof defaultValue === 'string' && defaultValue.length) || 0); | ||
const textareaClasses = clsx( | ||
'textarea', | ||
disabled && 'textarea--disabled', | ||
showMaxLength && 'textarea--maxlength-shown', | ||
); | ||
const FormTextarea = forwardRef<HTMLTextAreaElement, TextareaProps>( | ||
( | ||
{ | ||
label, | ||
name, | ||
maxLength = 2000, | ||
minRows = 3, | ||
maxRows = 3, | ||
disabled, | ||
hideLabel, | ||
showMaxLength, | ||
maxLengthBottom, | ||
defaultValue, | ||
useRichText, | ||
onChange, | ||
...rest | ||
}, | ||
ref | ||
) => { | ||
const id = useId(); | ||
const [currentLength, setCurrentLength] = useState( | ||
(typeof defaultValue === 'string' && defaultValue.length) || 0 | ||
); | ||
const textareaClasses = clsx( | ||
'textarea', | ||
disabled && 'textarea--disabled', | ||
showMaxLength && 'textarea--maxlength-shown' | ||
); | ||
|
||
const handleOnChange = (e: ChangeEvent<HTMLTextAreaElement>) => { | ||
if (showMaxLength) { | ||
setCurrentLength(e.target.value.length); | ||
} | ||
}; | ||
const handleOnChange = (e: ChangeEvent<HTMLTextAreaElement>) => { | ||
if (showMaxLength) { | ||
setCurrentLength(e.target.value.length); | ||
} | ||
}; | ||
|
||
return ( | ||
<div className={textareaClasses}> | ||
{label && !hideLabel && <label htmlFor={id} className='textarea__label'>{label}</label>} | ||
<div className='textarea__wrapper'> | ||
<TextareaAutosize | ||
id={id} | ||
maxLength={maxLength} | ||
minRows={minRows} | ||
maxRows={maxRows} | ||
ref={ref} | ||
defaultValue={defaultValue} | ||
aria-label={hideLabel ? label : undefined} | ||
onChange={(e) => { | ||
if (onChange) onChange(e); | ||
handleOnChange(e); | ||
}} | ||
{...rest} | ||
/> | ||
{showMaxLength && ( | ||
<div className={maxLengthBottom ? 'textarea__max-length-bottom' : 'textarea__max-length-top'}>{currentLength}/{maxLength}</div> | ||
return ( | ||
<div className={textareaClasses}> | ||
{label && !hideLabel && ( | ||
<label htmlFor={id} className="textarea__label"> | ||
{label} | ||
</label> | ||
)} | ||
<div className="textarea__wrapper"> | ||
{useRichText ? ( | ||
<RichTextarea | ||
id={id} | ||
style={{ width: '100%', height: '95px' }} | ||
maxLength={maxLength} | ||
ref={ref} | ||
defaultValue={defaultValue ?? ''} | ||
aria-label={hideLabel ? label : undefined} | ||
onChange={(e) => { | ||
if (onChange) onChange(e); | ||
handleOnChange(e); | ||
}} | ||
> | ||
{createRegexRenderer([ | ||
[ | ||
/https?:\/\/[-_.!~*\'()a-zA-Z0-9;\/?:\@&=+\$,%#]+/g, | ||
({ children, key, value }) => ( | ||
<a | ||
style={{ | ||
color: 'blue', | ||
textDecoration: 'underline', | ||
cursor: 'pointer', | ||
}} | ||
key={key} | ||
href={value} | ||
target="_blank" | ||
> | ||
{children} | ||
</a> | ||
), | ||
], | ||
])} | ||
</RichTextarea> | ||
) : ( | ||
<TextareaAutosize | ||
id={id} | ||
maxLength={maxLength} | ||
minRows={minRows} | ||
maxRows={maxRows} | ||
ref={ref} | ||
defaultValue={defaultValue} | ||
aria-label={hideLabel ? label : undefined} | ||
onChange={(e) => { | ||
if (onChange) onChange(e); | ||
handleOnChange(e); | ||
}} | ||
{...rest} | ||
/> | ||
)} | ||
{showMaxLength && ( | ||
<div | ||
className={ | ||
maxLengthBottom | ||
? 'textarea__max-length-bottom' | ||
: 'textarea__max-length-top' | ||
} | ||
> | ||
{currentLength}/{maxLength} | ||
</div> | ||
)} | ||
</div> | ||
</div> | ||
</div> | ||
); | ||
}); | ||
); | ||
} | ||
); | ||
|
||
export default FormTextarea; |
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