Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(TextArea): reset rows when rows exceed autoSize #6256

Merged
merged 2 commits into from
Jul 20, 2023
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
12 changes: 12 additions & 0 deletions src/components/text-area/tests/text-area.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -130,4 +130,16 @@ describe('TextArea', () => {
expect(textarea).toHaveAttribute('rows', '1')
expect(hiddenTextarea).toHaveAttribute('rows', '1')
})

test('rows should be the minRows when rows < minRows', () => {
const { getByRole } = render(<TextArea autoSize={{ minRows: 3 }} />)
const textarea = getByRole('textbox')
expect(textarea).toHaveAttribute('rows', '3')
})

test('rows should be the maxRows when rows > maxRows', () => {
const { getByRole } = render(<TextArea autoSize={{ maxRows: 1 }} />)
const textarea = getByRole('textbox')
expect(textarea).toHaveAttribute('rows', '1')
})
})
14 changes: 12 additions & 2 deletions src/components/text-area/text-area.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -135,13 +135,23 @@ export const TextArea = forwardRef<TextAreaRef, TextAreaProps>(
)
}

let rows = props.rows
if (typeof autoSize === 'object') {
if (autoSize.maxRows && rows > autoSize.maxRows) {
rows = autoSize.maxRows
}
if (autoSize.minRows && rows < autoSize.minRows) {
rows = autoSize.minRows
}
}

return withNativeProps(
props,
<div className={classPrefix}>
<textarea
ref={nativeTextAreaRef}
className={`${classPrefix}-element`}
rows={props.rows}
rows={rows}
value={value}
placeholder={props.placeholder}
onChange={e => {
Expand Down Expand Up @@ -180,7 +190,7 @@ export const TextArea = forwardRef<TextAreaRef, TextAreaProps>(
ref={hiddenTextAreaRef}
className={`${classPrefix}-element ${classPrefix}-element-hidden`}
value={value}
rows={props.rows}
rows={rows}
aria-hidden
readOnly
/>
Expand Down