Skip to content

Commit

Permalink
chore: separate showCharCount and maxLength display from eachother (s…
Browse files Browse the repository at this point in the history
…ame as TextFiedl)
  • Loading branch information
feclist committed Nov 6, 2024
1 parent bf50cc7 commit f82c621
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
18 changes: 13 additions & 5 deletions packages/core/src/components/TextArea/TextArea.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,13 @@ const TextArea = forwardRef(

const [characterCount, setCharacterCount] = useState(rest.value?.length || 0);

const handleOnChange = useCallback((event: React.ChangeEvent<HTMLTextAreaElement>) => {
setCharacterCount(event.target.value.length);
onChange?.(event);
}, [onChange]);
const handleOnChange = useCallback(
(event: React.ChangeEvent<HTMLTextAreaElement>) => {
setCharacterCount(event.target.value.length);
onChange?.(event);
},
[onChange]
);

return (
<div
Expand Down Expand Up @@ -84,7 +87,12 @@ const TextArea = forwardRef(
{helpText}
</Text>
)}
{maxLength && showCharCount && <div className={cx(styles.limitText)}>{characterCount}/{maxLength}</div>}
{showCharCount && (
<div className={cx(styles.limitText)}>
{characterCount}
{maxLength && `/${maxLength}`}
</div>
)}
</div>
</div>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,11 @@ describe("TextArea", () => {
expect(screen.queryByText("0/")).not.toBeInTheDocument();
});

it("it only shows character count with showCharCount and no MaxLength", () => {
render(<TextArea showCharCount />);
expect(screen.queryByText("0")).toBeInTheDocument();
});

it("should not show character count when only maxLength is provided", () => {
render(<TextArea maxLength={5} />);
expect(screen.queryByText("0/5")).not.toBeInTheDocument();
Expand Down

0 comments on commit f82c621

Please sign in to comment.