-
Notifications
You must be signed in to change notification settings - Fork 483
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added support for multi-line text input for Doctor Notes (#6977)
* Changed TextFormField to TextAreaFormField * made doctor notes input size dynamic * fixed max height of textareafield * made max height variable * replaced ids with ref * removed id prop * added required comments * added required comments * lineheight computed dynamically
- Loading branch information
1 parent
2cc66f0
commit 5e0727d
Showing
4 changed files
with
71 additions
and
27 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
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
30 changes: 30 additions & 0 deletions
30
src/Components/Form/FormFields/AutoExpandingTextInputFormField.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,30 @@ | ||
import React, { useEffect, useRef } from "react"; | ||
import TextAreaFormField, { TextAreaFormFieldProps } from "./TextAreaFormField"; | ||
|
||
type AutoExpandingTextInputFormFieldProps = TextAreaFormFieldProps & { | ||
maxHeight?: number; | ||
}; | ||
|
||
const AutoExpandingTextInputFormField = ( | ||
props: AutoExpandingTextInputFormFieldProps | ||
) => { | ||
const myref = useRef<HTMLTextAreaElement>(null); | ||
useEffect(() => { | ||
if (myref.current == null) return; | ||
const text = myref.current.textContent?.split("\n"); | ||
const len = text?.length || 1; | ||
// 46 is height of the textarea when there is only 1 line | ||
// getting line height from window | ||
const lineHeight = | ||
window.getComputedStyle(myref.current).lineHeight.slice(0, -2) || "20"; | ||
// added 26 for padding (20+26 = 46) | ||
const height = | ||
Math.min(len * parseInt(lineHeight), (props.maxHeight || 160) - 26) + 26; | ||
// 160 is the max height of the textarea if not specified | ||
myref.current.style.cssText = "height:" + height + "px"; | ||
}); | ||
|
||
return <TextAreaFormField ref={myref} {...props} className="w-full" />; | ||
}; | ||
|
||
export default AutoExpandingTextInputFormField; |
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,33 +1,44 @@ | ||
import { forwardRef } from "react"; | ||
import FormField from "./FormField"; | ||
import { FormFieldBaseProps, useFormFieldPropsResolver } from "./Utils"; | ||
|
||
type TextAreaFormFieldProps = FormFieldBaseProps<string> & { | ||
export type TextAreaFormFieldProps = FormFieldBaseProps<string> & { | ||
placeholder?: string; | ||
value?: string | number; | ||
rows?: number; | ||
// prefixIcon?: React.ReactNode; | ||
// suffixIcon?: React.ReactNode; | ||
onFocus?: (event: React.FocusEvent<HTMLTextAreaElement>) => void; | ||
onBlur?: (event: React.FocusEvent<HTMLTextAreaElement>) => void; | ||
}; | ||
|
||
const TextAreaFormField = ({ rows = 3, ...props }: TextAreaFormFieldProps) => { | ||
const field = useFormFieldPropsResolver(props as any); | ||
return ( | ||
<FormField field={field}> | ||
<textarea | ||
id={field.id} | ||
disabled={field.disabled} | ||
name={field.name} | ||
value={field.value} | ||
required={field.required} | ||
onChange={(e) => field.handleChange(e.target.value)} | ||
placeholder={props.placeholder} | ||
rows={rows} | ||
className={`cui-input-base resize-none ${ | ||
field.error && "border-danger-500" | ||
}`} | ||
/> | ||
</FormField> | ||
); | ||
}; | ||
const TextAreaFormField = forwardRef( | ||
( | ||
{ rows = 3, ...props }: TextAreaFormFieldProps, | ||
ref?: React.Ref<HTMLTextAreaElement> | ||
) => { | ||
const field = useFormFieldPropsResolver(props as any); | ||
return ( | ||
<FormField field={field}> | ||
<textarea | ||
id={field.id} | ||
ref={ref} | ||
disabled={field.disabled} | ||
name={field.name} | ||
value={field.value} | ||
required={field.required} | ||
onChange={(e) => field.handleChange(e.target.value)} | ||
placeholder={props.placeholder} | ||
rows={rows} | ||
className={`cui-input-base resize-none ${ | ||
field.error && "border-danger-500" | ||
}`} | ||
onFocus={props.onFocus} | ||
onBlur={props.onBlur} | ||
/> | ||
</FormField> | ||
); | ||
} | ||
); | ||
|
||
export default TextAreaFormField; |