Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/characterCount/CharacterCount.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,8 @@ export const CharacterCount = forwardRef(
const overThreshold = isOverThreshold(value);

setShowMessage(overThreshold || !threshold);
if (remaining < 0) setOverLimitBy(-remaining);
if (remaining <= 0) setOverLimitBy(-remaining);
if (remaining > 0) setOverLimitBy(0);
setRemainingCount(remaining);
};

Expand Down
15 changes: 15 additions & 0 deletions src/characterCount/__tests__/CharacterCount.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const DummyResetCharacterCountComponent = () => {
rows={5}
cols={50}
ref={textRef}
messageErrorClassName="error-class-name"
/>
<button onClick={() => textRef.current.reset()}>Cancel</button>
</form>
Expand Down Expand Up @@ -131,6 +132,20 @@ describe('CharacterCount with character limit', () => {
expect(error).not.toBeInTheDocument();
});

it('should assign error class for error message and remove class if user backspaces to correct input', async () => {
const user = userEvent.setup();
const { container } = render(<DummyResetCharacterCountComponent />);
const textarea = screen.getByRole('textbox');

await user.type(textarea, 'more than 15 chars');
expect(container.querySelector('.error-class-name')).toBeInTheDocument();

await user.type(textarea, '{backspace}{backspace}{backspace}');
expect(
container.querySelector('.error-class-name')
).not.toBeInTheDocument();
});

it('should display error message when displayError is true', () => {
render(
<CharacterCount
Expand Down