Skip to content

Commit

Permalink
fix(comp:textarea): lineheight error when not rendered initially (#1372)
Browse files Browse the repository at this point in the history
  • Loading branch information
sallerli1 committed Dec 23, 2022
1 parent bda0db2 commit 627e018
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 10 deletions.
Expand Up @@ -2,10 +2,10 @@

exports[`Textarea > render work 1`] = `"<span class=\\"ix-textarea ix-textarea-md\\" data-count=\\"\\"><textarea class=\\"ix-textarea-inner\\" style=\\"resize: vertical;\\"></textarea><!----></span>"`;
exports[`Textarea > resize and autoRows work 1`] = `"<span class=\\"ix-textarea ix-textarea-md\\" data-count=\\"\\"><textarea class=\\"ix-textarea-inner\\" style=\\"resize: none; line-height: -4px; height: 0px; overflow: hidden;\\"></textarea><!----></span>"`;
exports[`Textarea > resize and autoRows work 1`] = `"<span class=\\"ix-textarea ix-textarea-md\\" data-count=\\"\\"><textarea class=\\"ix-textarea-inner\\" style=\\"resize: none;\\"></textarea><!----></span>"`;
exports[`Textarea > resize and autoRows work 2`] = `"<span class=\\"ix-textarea ix-textarea-md\\" data-count=\\"\\"><textarea class=\\"ix-textarea-inner\\" style=\\"resize: none; line-height: -4px; height: 0px; overflow: hidden;\\"></textarea><!----></span>"`;
exports[`Textarea > resize and autoRows work 2`] = `"<span class=\\"ix-textarea ix-textarea-md\\" data-count=\\"\\"><textarea class=\\"ix-textarea-inner\\" style=\\"resize: none;\\"></textarea><!----></span>"`;
exports[`Textarea > resize and autoRows work 3`] = `"<span class=\\"ix-textarea ix-textarea-md\\" data-count=\\"\\"><textarea class=\\"ix-textarea-inner\\" style=\\"resize: none; line-height: -4px; height: 0px; overflow: hidden; max-height: -4px;\\"></textarea><!----></span>"`;
exports[`Textarea > resize and autoRows work 3`] = `"<span class=\\"ix-textarea ix-textarea-md\\" data-count=\\"\\"><textarea class=\\"ix-textarea-inner\\" style=\\"resize: none;\\"></textarea><!----></span>"`;
exports[`Textarea > resize and autoRows work 4`] = `"<span class=\\"ix-textarea ix-textarea-md\\" data-count=\\"\\"><textarea class=\\"ix-textarea-inner\\" style=\\"resize: none; line-height: -4px; height: 0px; overflow: hidden; max-height: -4px;\\"></textarea><!----></span>"`;
exports[`Textarea > resize and autoRows work 4`] = `"<span class=\\"ix-textarea ix-textarea-md\\" data-count=\\"\\"><textarea class=\\"ix-textarea-inner\\" style=\\"resize: none;\\"></textarea><!----></span>"`;
Expand Up @@ -20,7 +20,7 @@ import { useLineHeight } from './useLineHeight'

export interface AutoRowsContext {
resizeToFitContent: (force?: boolean) => void
lineHeight: ComputedRef<number>
lineHeight: Ref<number>
boxSizingData: ComputedRef<BoxSizingData>
}

Expand Down
30 changes: 26 additions & 4 deletions packages/components/textarea/src/composables/useLineHeight.ts
Expand Up @@ -5,7 +5,7 @@
* found in the LICENSE file at https://github.com/IDuxFE/idux/blob/main/LICENSE
*/

import { type ComputedRef, type Ref, computed } from 'vue'
import { type Ref, customRef, watch } from 'vue'

import { getBoxSizingData } from '../utils/getBoxSizingData'
import { measureTextarea } from '../utils/measureTextarea'
Expand All @@ -16,8 +16,30 @@ import { measureTextarea } from '../utils/measureTextarea'
* @param textareaRef reference of a textarea element
* @return ComputedRef of line height
*/
export function useLineHeight(textareaRef: Ref<HTMLTextAreaElement | undefined>): ComputedRef<number> {
return computed(() => calcTextareaLineHeight(textareaRef.value))
export function useLineHeight(textareaRef: Ref<HTMLTextAreaElement | undefined>): Ref<number> {
let lineHeight = 0

return customRef((track, trigger) => {
watch(textareaRef, () => {
lineHeight = calcTextareaLineHeight(textareaRef.value)
trigger()
})

return {
get() {
if (!textareaRef.value) {
return 0
}

const value = lineHeight || (lineHeight = calcTextareaLineHeight(textareaRef.value))
track()
return value
},
set() {
// this is readonly, no setter provided
},
}
})
}

function calcTextareaLineHeight(textarea: HTMLTextAreaElement | undefined): number {
Expand All @@ -40,7 +62,7 @@ function calcTextareaLineHeight(textarea: HTMLTextAreaElement | undefined): numb
const lineHeight = el.scrollHeight - paddingSize
el.rows = rows

return lineHeight
return lineHeight > 0 ? lineHeight : 0
},
false,
)
Expand Down
2 changes: 1 addition & 1 deletion packages/pro/textarea/src/token.ts
Expand Up @@ -15,7 +15,7 @@ export interface ProTextareaContext extends ErrorLinesContext {
props: ProTextareaProps
accessor: FormAccessor
boxSizingData: ComputedRef<ɵBoxSizingData>
lineHeight: ComputedRef<number>
lineHeight: Ref<number>
mergedPrefixCls: ComputedRef<string>
rowCounts: ComputedRef<number[]>
textareaRef: Ref<HTMLTextAreaElement | undefined>
Expand Down

0 comments on commit 627e018

Please sign in to comment.