Skip to content

Commit 2997c80

Browse files
committed
fix(textarea): placeholder 可以设置为空字符串 (jdf2e#2979)
1 parent aedc5c1 commit 2997c80

File tree

3 files changed

+71
-49
lines changed

3 files changed

+71
-49
lines changed

Diff for: src/packages/textarea/__test__/__snapshots__/textarea.spec.tsx.snap

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@ exports[`textarea props test 1`] = `
66
class="nut-textarea nut-textarea-container nut-textarea-default"
77
>
88
<textarea
9-
class="nut-textarea-textarea "
9+
class="nut-textarea-textarea"
1010
maxlength="20"
11-
placeholder="请输入内容"
11+
placeholder=""
1212
rows="2"
1313
>
1414
基础用法
1515
</textarea>
1616
<div
17-
class="nut-textarea-limit "
17+
class="nut-textarea-limit"
1818
>
1919
4
2020
/

Diff for: src/packages/textarea/textarea.taro.tsx

+39-24
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
import React, { FunctionComponent, useRef } from 'react'
22
import classNames from 'classnames'
33
import Taro from '@tarojs/taro'
4-
import { Textarea, TextareaProps, View, Text } from '@tarojs/components'
4+
import {
5+
BaseEventOrig,
6+
Text,
7+
Textarea,
8+
TextareaProps,
9+
View,
10+
} from '@tarojs/components'
511
import { useConfig, useRtl } from '@/packages/configprovider/index.taro'
612
import { BasicComponent, ComponentDefaults } from '@/utils/typings'
713
import { usePropsValue } from '@/utils/use-props-value'
@@ -21,8 +27,8 @@ export interface TextAreaProps
2127
plain: boolean
2228
status: 'error' | 'default'
2329
onChange: (value: string) => void
24-
onBlur: (event: Event) => void
25-
onFocus: (event: Event) => void
30+
onBlur: (event: BaseEventOrig) => void
31+
onFocus: (event: BaseEventOrig) => void
2632
}
2733

2834
const defaultProps = {
@@ -77,7 +83,7 @@ export const TextArea: FunctionComponent<Partial<TextAreaProps>> = (props) => {
7783
onChange,
7884
})
7985

80-
const handleChange = (event: any) => {
86+
const handleChange = (event: BaseEventOrig) => {
8187
const text = event?.detail?.value
8288
if (text) {
8389
const value = compositionRef.current ? text : format(text)
@@ -87,32 +93,36 @@ export const TextArea: FunctionComponent<Partial<TextAreaProps>> = (props) => {
8793
}
8894
}
8995

90-
const handleFocus = (event: Event) => {
91-
if (disabled) return
92-
if (readOnly) return
93-
onFocus && onFocus(event)
96+
const isDisabled = () => disabled || readOnly
97+
98+
const handleFocus = (event: BaseEventOrig) => {
99+
if (isDisabled()) return
100+
onFocus?.(event)
94101
}
95102

96-
const handleBlur = (event: Event) => {
97-
if (disabled) return
98-
if (readOnly) return
99-
onBlur && onBlur(event)
103+
const handleBlur = (event: BaseEventOrig) => {
104+
if (isDisabled()) return
105+
onBlur?.(event)
100106
}
101107

102108
return (
103109
<>
104110
<View
105111
className={classNames(
106112
classPrefix,
107-
disabled ? `${classPrefix}-disabled` : '',
108-
readOnly ? `${classPrefix}-readonly` : '',
109-
rtl ? `${classPrefix}-rtl` : '',
110-
plain ? `${classPrefix}-plain` : `${classPrefix}-container`,
111-
status ? `${classPrefix}-${status}` : '',
113+
{
114+
[`${classPrefix}-disabled`]: disabled,
115+
[`${classPrefix}-readonly`]: readOnly,
116+
[`${classPrefix}-rtl`]: rtl,
117+
[`${classPrefix}-plain`]: plain,
118+
[`${classPrefix}-container`]: !plain,
119+
[`${classPrefix}-${status}`]: status,
120+
},
112121
className
113122
)}
114123
>
115124
<Textarea
125+
{...rest}
116126
nativeProps={{
117127
style,
118128
readOnly,
@@ -124,22 +134,27 @@ export const TextArea: FunctionComponent<Partial<TextAreaProps>> = (props) => {
124134
compositionRef.current = false
125135
},
126136
}}
127-
className={`${classPrefix}-textarea ${disabled ? `${classPrefix}-textarea-disabled` : ''}`}
137+
className={classNames(`${classPrefix}-textarea`, {
138+
[`${classPrefix}-textarea-disabled`]: disabled,
139+
})}
128140
style={Taro.getEnv() === 'WEB' ? undefined : style}
129141
disabled={Taro.getEnv() === 'WEB' ? disabled : disabled || readOnly}
130142
// @ts-ignore
131143
value={inputValue}
132-
onInput={(e: any) => handleChange(e)}
133-
onBlur={(e: any) => handleBlur(e)}
134-
onFocus={(e: any) => handleFocus(e)}
144+
onInput={handleChange}
145+
onBlur={handleBlur}
146+
onFocus={handleFocus}
135147
autoHeight={autoSize}
136148
maxlength={maxLength}
137-
placeholder={placeholder || locale.placeholder}
138-
{...rest}
149+
placeholder={
150+
placeholder !== undefined ? placeholder : locale.placeholder
151+
}
139152
/>
140153
{showCount && (
141154
<Text
142-
className={`${classPrefix}-limit ${disabled ? `${classPrefix}-limit-disabled` : ''}`}
155+
className={classNames(`${classPrefix}-limit`, {
156+
[`${classPrefix}-limit-disabled`]: disabled,
157+
})}
143158
>
144159
{inputValue.length}/{maxLength < 0 ? 0 : maxLength}
145160
</Text>

Diff for: src/packages/textarea/textarea.tsx

+29-22
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import React, { FunctionComponent, useEffect, useRef } from 'react'
21
import type { ChangeEvent, FocusEvent } from 'react'
2+
import React, { FunctionComponent, useEffect, useRef } from 'react'
33
import classNames from 'classnames'
44
import { useConfig, useRtl } from '@/packages/configprovider'
55
import { BasicComponent, ComponentDefaults } from '@/utils/typings'
@@ -83,9 +83,7 @@ export const TextArea: FunctionComponent<
8383
})
8484

8585
useEffect(() => {
86-
if (autoSize) {
87-
setContentHeight()
88-
}
86+
if (autoSize) setContentHeight()
8987
}, [autoSize, defaultValue, inputValue])
9088

9189
const setContentHeight = () => {
@@ -105,41 +103,47 @@ export const TextArea: FunctionComponent<
105103
setInputValue(value)
106104
}
107105

106+
const isDisabled = () => disabled || readOnly
107+
108108
const handleFocus = (event: FocusEvent<HTMLTextAreaElement>) => {
109-
if (disabled) return
110-
if (readOnly) return
111-
onFocus && onFocus(event)
109+
if (isDisabled()) return
110+
onFocus?.(event)
112111
}
113112

114113
const handleBlur = (event: FocusEvent<HTMLTextAreaElement>) => {
115-
if (disabled) return
116-
if (readOnly) return
117-
onBlur && onBlur(event)
114+
if (isDisabled()) return
115+
onBlur?.(event)
118116
}
119117

120118
return (
121119
<>
122120
<div
123121
className={classNames(
124122
classPrefix,
125-
disabled ? `${classPrefix}-disabled` : '',
126-
readOnly ? `${classPrefix}-readonly` : '',
127-
rtl ? `${classPrefix}-rtl` : '',
128-
plain ? `${classPrefix}-plain` : `${classPrefix}-container`,
129-
status ? `${classPrefix}-${status}` : '',
123+
{
124+
[`${classPrefix}-disabled`]: disabled,
125+
[`${classPrefix}-readonly`]: readOnly,
126+
[`${classPrefix}-rtl`]: rtl,
127+
[`${classPrefix}-plain`]: plain,
128+
[`${classPrefix}-container`]: !plain,
129+
[`${classPrefix}-${status}`]: status,
130+
},
130131
className
131132
)}
132133
>
133134
<textarea
135+
{...rest}
134136
ref={textareaRef}
135-
className={`${classPrefix}-textarea ${disabled ? `${classPrefix}-textarea-disabled` : ''}`}
137+
className={classNames(`${classPrefix}-textarea`, {
138+
[`${classPrefix}-textarea-disabled`]: disabled,
139+
})}
136140
style={style}
137141
disabled={disabled}
138142
readOnly={readOnly}
139143
value={inputValue}
140-
onChange={(e) => handleChange(e)}
141-
onBlur={(e) => handleBlur(e)}
142-
onFocus={(e) => handleFocus(e)}
144+
onChange={handleChange}
145+
onBlur={handleBlur}
146+
onFocus={handleFocus}
143147
onCompositionEnd={() => {
144148
compositionRef.current = false
145149
}}
@@ -148,12 +152,15 @@ export const TextArea: FunctionComponent<
148152
}}
149153
rows={rows}
150154
maxLength={maxLength === -1 ? undefined : maxLength}
151-
placeholder={placeholder || locale.placeholder}
152-
{...rest}
155+
placeholder={
156+
placeholder !== undefined ? placeholder : locale.placeholder
157+
}
153158
/>
154159
{showCount && (
155160
<div
156-
className={`${classPrefix}-limit ${disabled ? `${classPrefix}-limit-disabled` : ''}`}
161+
className={classNames(`${classPrefix}-limit`, {
162+
[`${classPrefix}-limit-disabled`]: disabled,
163+
})}
157164
>
158165
{inputValue.length}/{maxLength < 0 ? 0 : maxLength}
159166
</div>

0 commit comments

Comments
 (0)