@@ -4,6 +4,7 @@ import { CommonEvent } from '@tarojs/components/types/common'
4
4
import Taro from '@tarojs/taro'
5
5
import { AtTextareaProps } from 'types/textarea'
6
6
import { pxTransform } from '../../utils/common'
7
+ import { useModelValue } from '../../composables/model'
7
8
8
9
type ExtendEvent = {
9
10
target : {
@@ -50,19 +51,15 @@ const AtTextarea = defineComponent({
50
51
height : { type : [ String , Number ] , default : 100 } ,
51
52
cursorSpacing : { type : Number , default : 100 } ,
52
53
// event handlers
53
- onChange : {
54
- type : Function as PropType < AtTextareaProps [ 'onChange' ] > ,
55
- default : ( ) => ( value : string , event ?: CommonEvent ) => { } ,
56
- required : true
57
- } ,
54
+ onChange : Function as PropType < AtTextareaProps [ 'onChange' ] > ,
58
55
onFocus : Function as PropType < AtTextareaProps [ 'onFocus' ] > ,
59
56
onBlur : Function as PropType < AtTextareaProps [ 'onBlur' ] > ,
60
57
onConfirm : Function as PropType < AtTextareaProps [ 'onConfirm' ] > ,
61
58
onLinechange : Function as PropType < AtTextareaProps [ 'onLinechange' ] > ,
62
59
} ,
63
60
64
- setup ( props : AtTextareaProps , { attrs, slots } ) {
65
- const isAlipay = Taro . getEnv ( ) === Taro . ENV_TYPE . ALIPAY
61
+ setup ( props : AtTextareaProps , { attrs, emit } ) {
62
+ const inputValue = useModelValue ( props , emit , 'value' )
66
63
67
64
const _maxLength = computed ( ( ) => parseInt ( props . maxLength ! . toString ( ) ) )
68
65
@@ -79,68 +76,72 @@ const AtTextarea = defineComponent({
79
76
const rootClasses = computed ( ( ) => ( {
80
77
'at-textarea' : true ,
81
78
[ `at-textarea--${ ENV } ` ] : true ,
82
- 'at-textarea--error' : _maxLength . value < props . value . length
79
+ 'at-textarea--error' : _maxLength . value < inputValue . value . length
83
80
} ) )
84
81
85
- const placeholderClasses = computed ( ( ) => `placeholder ${ props . placeholderClass } ` )
86
-
87
- const alipayShowCount = computed ( ( ) => isAlipay
88
- ? { showCount : props . count }
89
- : { }
90
- )
82
+ const placeholderClasses = computed ( ( ) => ( {
83
+ 'placeholder' : true ,
84
+ [ `${ props . placeholderClass } ` ] : Boolean ( props . placeholderClass )
85
+ } ) )
91
86
92
87
function handleInput ( event : CommonEvent & ExtendEvent ) : void {
93
- props . onChange ( event . target . value , event )
88
+ if ( attrs [ 'onUpdate:value' ] ) {
89
+ inputValue . value = event . target . value
90
+ } else {
91
+ props . onChange ?.( event . target . value , event )
92
+ }
94
93
}
95
94
96
95
function handleFocus ( event : CommonEvent ) : void {
97
- props . onFocus && props . onFocus ( event )
96
+ props . onFocus ?. ( event )
98
97
}
99
98
100
99
function handleBlur ( event : CommonEvent ) : void {
101
- props . onBlur && props . onBlur ( event )
100
+ props . onBlur ?. ( event )
102
101
}
103
102
104
103
function handleConfirm ( event : CommonEvent ) : void {
105
- props . onConfirm && props . onConfirm ( event )
104
+ props . onConfirm ?. ( event )
106
105
}
107
106
108
107
function handleLinechange ( event : CommonEvent ) : void {
109
- props . onLinechange && props . onLinechange ( event )
108
+ props . onLinechange ?. ( event )
110
109
}
111
110
112
111
return ( ) => (
113
112
h ( View , mergeProps ( attrs , {
114
113
class : rootClasses . value
115
114
} ) , {
116
115
default : ( ) => [
117
- h ( Textarea , mergeProps ( alipayShowCount . value , {
118
- class : 'at-textarea__textarea' ,
119
- style : textareaStyle . value ,
120
- placeholderstyle : props . placeholderStyle ,
121
- placeholderClass : placeholderClasses . value ,
122
- cursorSpacing : props . cursorSpacing ,
123
- value : props . value ,
124
- maxlength : actualMaxLength . value ,
125
- placeholder : props . placeholder ,
126
- disabled : props . disabled ,
127
- autoFocus : props . autoFocus ,
128
- focus : props . focus ,
129
- fixed : props . fixed ,
130
- showConfirmBar : props . showConfirmBar ,
131
- selectionStart : props . selectionStart ,
132
- selectionEnd : props . selectionEnd ,
133
- onInput : handleInput ,
134
- onFocus : handleFocus ,
135
- onBlur : handleBlur ,
136
- onConfirm : handleConfirm ,
137
- onLineChange : handleLinechange ,
138
- } ) ) ,
139
-
140
- props . count && ! isAlipay && (
116
+ h ( Textarea , mergeProps (
117
+ process . env . TARO_ENV === 'alipay' ? { showCount : props . count } : { } ,
118
+ {
119
+ class : 'at-textarea__textarea' ,
120
+ style : textareaStyle . value ,
121
+ placeholderstyle : props . placeholderStyle ,
122
+ placeholderClass : placeholderClasses . value ,
123
+ cursorSpacing : props . cursorSpacing ,
124
+ value : inputValue . value ,
125
+ maxlength : actualMaxLength . value ,
126
+ placeholder : props . placeholder ,
127
+ disabled : props . disabled ,
128
+ autoFocus : props . autoFocus ,
129
+ focus : props . focus ,
130
+ fixed : props . fixed ,
131
+ showConfirmBar : props . showConfirmBar ,
132
+ selectionStart : props . selectionStart ,
133
+ selectionEnd : props . selectionEnd ,
134
+ onInput : handleInput ,
135
+ onFocus : handleFocus ,
136
+ onBlur : handleBlur ,
137
+ onConfirm : handleConfirm ,
138
+ onLineChange : handleLinechange ,
139
+ } ) ) ,
140
+
141
+ props . count && process . env . TARO_ENV !== 'alipay' && (
141
142
h ( View , {
142
143
class : 'at-textarea__counter'
143
- } , { default : ( ) => `${ props . value . length } / ${ _maxLength . value } ` } )
144
+ } , { default : ( ) => `${ inputValue . value . length } / ${ _maxLength . value } ` } )
144
145
)
145
146
]
146
147
} )
0 commit comments