Skip to content

Commit

Permalink
Added support for multi-line text input for Doctor Notes (#6977)
Browse files Browse the repository at this point in the history
* 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
kshitijv256 authored Feb 14, 2024
1 parent 2cc66f0 commit 5e0727d
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 27 deletions.
8 changes: 5 additions & 3 deletions src/Components/Facility/ConsultationDoctorNotes/index.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { useState } from "react";
import * as Notification from "../../../Utils/Notifications.js";
import Page from "../../Common/components/Page";
import TextFormField from "../../Form/FormFields/TextFormField";
import ButtonV2 from "../../Common/components/ButtonV2";
import CareIcon from "../../../CAREUI/icons/CareIcon";
import { NonReadOnlyUsers } from "../../../Utils/AuthorizeFor";
Expand All @@ -13,6 +12,7 @@ import request from "../../../Utils/request/request.js";
import useQuery from "../../../Utils/request/useQuery.js";
import useKeyboardShortcut from "use-keyboard-shortcut";
import { isAppleDevice } from "../../../Utils/utils.js";
import AutoExpandingTextInputFormField from "../../Form/FormFields/AutoExpandingTextInputFormField.js";

interface ConsultationDoctorNotesProps {
patientId: string;
Expand Down Expand Up @@ -120,12 +120,14 @@ const ConsultationDoctorNotes = (props: ConsultationDoctorNotesProps) => {
/>

<div className="relative mx-4 flex items-center">
<TextFormField
<AutoExpandingTextInputFormField
id="doctor_consultation_notes"
maxHeight={160}
rows={1}
name="note"
value={noteField}
onChange={(e) => setNoteField(e.value)}
className="grow"
type="text"
errorClassName="hidden"
placeholder="Type your Note"
disabled={!patientActive}
Expand Down
7 changes: 4 additions & 3 deletions src/Components/Facility/PatientNotesSlideover.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import * as Notification from "../../Utils/Notifications.js";
import { NonReadOnlyUsers } from "../../Utils/AuthorizeFor";
import CareIcon from "../../CAREUI/icons/CareIcon";
import { classNames, isAppleDevice } from "../../Utils/utils";
import TextFormField from "../Form/FormFields/TextFormField";
import ButtonV2 from "../Common/components/ButtonV2";
import { make as Link } from "../Common/components/Link.bs";
import { useMessageListener } from "../../Common/hooks/useMessageListener";
Expand All @@ -12,6 +11,7 @@ import request from "../../Utils/request/request";
import routes from "../../Redux/api";
import { PatientNoteStateType } from "./models";
import useKeyboardShortcut from "use-keyboard-shortcut";
import AutoExpandingTextInputFormField from "../Form/FormFields/AutoExpandingTextInputFormField.js";

interface PatientNotesProps {
patientId: string;
Expand Down Expand Up @@ -169,13 +169,14 @@ export default function PatientNotesSlideover(props: PatientNotesProps) {
setReload={setReload}
/>
<div className="relative mx-4 flex items-center">
<TextFormField
<AutoExpandingTextInputFormField
id="doctor_notes_textarea"
maxHeight={160}
rows={1}
name="note"
value={noteField}
onChange={(e) => setNoteField(e.value)}
className="grow"
type="text"
errorClassName="hidden"
placeholder="Type your Note"
disabled={!patientActive}
Expand Down
30 changes: 30 additions & 0 deletions src/Components/Form/FormFields/AutoExpandingTextInputFormField.tsx
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;
53 changes: 32 additions & 21 deletions src/Components/Form/FormFields/TextAreaFormField.tsx
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;

0 comments on commit 5e0727d

Please sign in to comment.