Skip to content

Commit

Permalink
fix: text-area default rows
Browse files Browse the repository at this point in the history
  • Loading branch information
1587315093 committed Jul 17, 2023
1 parent 5892da7 commit 9e80d57
Showing 1 changed file with 16 additions and 13 deletions.
29 changes: 16 additions & 13 deletions src/components/text-area/text-area.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,20 @@ export type TextAreaProps = Pick<
maxLength?: number
showCount?: boolean | ((length: number, maxLength?: number) => ReactNode)
autoSize?:
| boolean
| {
minRows?: number
maxRows?: number
}
| boolean
| {
minRows?: number
maxRows?: number
}
id?: string
} & NativeProps<
| '--font-size'
| '--color'
| '--placeholder-color'
| '--disabled-color'
| '--text-align'
| '--count-text-align'
>
| '--font-size'
| '--color'
| '--placeholder-color'
| '--disabled-color'
| '--text-align'
| '--count-text-align'
>

export type TextAreaRef = {
clear: () => void
Expand All @@ -56,7 +56,7 @@ export type TextAreaRef = {
}

const defaultProps = {
rows: 2,
rows: 1,
showCount: false as NonNullable<TextAreaProps['showCount']>,
autoSize: false as NonNullable<TextAreaProps['autoSize']>,
defaultValue: '',
Expand Down Expand Up @@ -105,6 +105,7 @@ export const TextArea = forwardRef<TextAreaRef, TextAreaProps>(
textArea.style.height = heightRef.current
if (!hiddenTextArea) return
let height = hiddenTextArea.scrollHeight
let rows = props.rows
if (typeof autoSize === 'object') {
const computedStyle = window.getComputedStyle(textArea)
const lineHeight = parseFloat(computedStyle.lineHeight)
Expand All @@ -114,9 +115,11 @@ export const TextArea = forwardRef<TextAreaRef, TextAreaProps>(
if (autoSize.maxRows) {
height = Math.min(height, autoSize.maxRows * lineHeight)
}
rows = Math.floor(height / lineHeight)
}
heightRef.current = `${height}px`
textArea.style.height = `${height}px`
textArea.rows = rows
}, [value, autoSize])

const compositingRef = useRef(false)
Expand Down

0 comments on commit 9e80d57

Please sign in to comment.