Skip to content

Commit

Permalink
feat(components): add clear button to the textarea component
Browse files Browse the repository at this point in the history
  • Loading branch information
IsDyh01 committed Jul 15, 2024
1 parent b762141 commit 045d63f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
15 changes: 13 additions & 2 deletions packages/components/input/src/textarea.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {forwardRef} from "@nextui-org/system";
import {mergeProps} from "@react-aria/utils";
import {useMemo, useState} from "react";
import TextareaAutosize from "react-textarea-autosize";
import {CloseFilledIcon} from "@nextui-org/shared-icons";

import {UseInputProps, useInput} from "./use-input";

Expand Down Expand Up @@ -88,6 +89,8 @@ const Textarea = forwardRef<"textarea", TextAreaProps>(
getHelperWrapperProps,
getDescriptionProps,
getErrorMessageProps,
isClearable,
getClearButtonProps,
} = useInput<HTMLTextAreaElement>({...otherProps, ref, isMultiline: true});

const [hasMultipleRows, setIsHasMultipleRows] = useState(minRows > 1);
Expand Down Expand Up @@ -122,13 +125,21 @@ const Textarea = forwardRef<"textarea", TextAreaProps>(
/>
);

const end = useMemo(() => {
if (isClearable) {
return <span {...getClearButtonProps()}>{endContent || <CloseFilledIcon />}</span>;
}

return endContent;
}, [isClearable, getClearButtonProps]);

const innerWrapper = useMemo(() => {
if (startContent || endContent) {
if (startContent || end) {
return (
<div {...getInnerWrapperProps()}>
{startContent}
{content}
{endContent}
{end}
</div>
);
}
Expand Down
12 changes: 12 additions & 0 deletions packages/components/input/stories/textarea.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -302,3 +302,15 @@ export const IsInvalid = {
errorMessage: "Please enter a valid description",
},
};

export const Clearable = {
render: Template,

args: {
...defaultProps,
placeholder: "Enter your description",
defaultValue: "junior@nextui.org",
// eslint-disable-next-line no-console
onClear: () => console.log("textarea cleared"),
},
};

0 comments on commit 045d63f

Please sign in to comment.