Form Text Area Example
src / Demo.tsx
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102import React, { ReactElement } from "react";
import {
Checkbox,
Fieldset,
NativeSelect,
TextArea,
TextAreaResize,
useChecked,
useChoice,
} from "@react-md/form";
import TextFieldThemeConfig from "./TextFieldThemeConfig";
import styles from "./TextAreaExample.module.scss";
const MAX_ROWS = [-1, 4, 5, 6, 7, 8, 9, 10];
export default function Demo(): ReactElement {
const [resize, handleResizeChange] = useChoice<TextAreaResize>("auto");
const [animate, handleAnimateChange] = useChecked(true);
const [rows, handleRowChange] = useChoice("2");
const [maxRows, handleMaxRowChange, setMaxRows] = useChoice<string>("-1");
const rowsInt = parseInt(rows, 10);
const maxRowsInt = parseInt(maxRows, 10);
if (maxRowsInt !== -1 && maxRowsInt < rowsInt) {
const i = MAX_ROWS.find((value) => value >= rowsInt) || -1;
setMaxRows(`${i}`);
}
return (
<TextFieldThemeConfig
idPrefix="textarea"
renderField={(props) => (
<TextArea
id="configurable-textarea"
{...props}
key={`${rows}-${resize}`}
rows={rowsInt}
maxRows={maxRowsInt}
resize={resize}
animate={animate}
/>
)}
>
<Fieldset legend="Textarea options">
<Checkbox
id="textarea-animate"
checked={animate}
onChange={handleAnimateChange}
name="animate"
label="Animate height"
disabled={resize !== "auto"}
/>
<NativeSelect
id="textarea-resize"
label="Resize"
name="resize"
value={resize}
onChange={handleResizeChange}
className={styles.select}
>
<option value="none">None</option>
<option value="auto">Auto</option>
<option value="horizontal">Horizontal</option>
<option value="vertical">Vertical</option>
<option value="both">Both</option>
</NativeSelect>
<NativeSelect
id="textarea-rows"
label="Starting Rows"
name="rows"
value={rows}
onChange={handleRowChange}
className={styles.select}
>
{Array.from({ length: 6 }, (_, i) => (
<option key={i} value={i + 2}>
{i + 2}
</option>
))}
</NativeSelect>
<NativeSelect
id="textarea-max-rows"
label="Max rows"
name="maxRows"
value={maxRows}
onChange={handleMaxRowChange}
className={styles.select}
>
{MAX_ROWS.map((amount) => (
<option
key={amount}
value={amount}
disabled={amount !== -1 && amount < rowsInt}
>
{amount}
</option>
))}
</NativeSelect>
</Fieldset>
</TextFieldThemeConfig>
);
}