Skip to content

Commit e07bab0

Browse files
authored
refactor: refactor data-count dom (#37)
1 parent 601da2a commit e07bab0

File tree

3 files changed

+39
-37
lines changed

3 files changed

+39
-37
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
"dependencies": {
4646
"@babel/runtime": "^7.10.1",
4747
"classnames": "^2.2.1",
48-
"rc-input": "^0.2.1",
48+
"rc-input": "~1.0.0",
4949
"rc-resize-observer": "^1.0.0",
5050
"rc-util": "^5.27.0"
5151
},

src/TextArea.tsx

Lines changed: 37 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -186,23 +186,55 @@ const TextArea = React.forwardRef<TextAreaRef, TextAreaProps>(
186186
val = fixEmojiLength(val, maxLength!);
187187
}
188188

189+
let suffixNode = suffix;
190+
let dataCount: ReactNode;
191+
if (showCount) {
192+
const valueLength = [...val].length;
193+
194+
if (typeof showCount === 'object') {
195+
dataCount = showCount.formatter({
196+
value: val,
197+
count: valueLength,
198+
maxLength,
199+
});
200+
} else {
201+
dataCount = `${valueLength}${hasMaxLength ? ` / ${maxLength}` : ''}`;
202+
}
203+
204+
suffixNode = (
205+
<>
206+
{suffixNode}
207+
<span
208+
className={classNames(`${prefixCls}-data-count`, classes?.count)}
209+
>
210+
{dataCount}
211+
</span>
212+
</>
213+
);
214+
}
215+
189216
const textarea = (
190217
<BaseInput
191218
value={val}
192219
allowClear={allowClear}
193220
handleReset={handleReset}
194-
suffix={suffix}
221+
suffix={suffixNode}
195222
prefixCls={prefixCls}
196223
classes={{
197-
affixWrapper: classNames(
198-
classes?.affixWrapper,
199-
allowClear ? `${prefixCls}-textarea-allow-clear` : '',
200-
),
224+
affixWrapper: classNames(classes?.affixWrapper, {
225+
[`${prefixCls}-show-count`]: showCount,
226+
[`${prefixCls}-textarea-allow-clear`]: allowClear,
227+
}),
201228
}}
202229
disabled={disabled}
203230
focused={focused}
204231
style={style}
205232
inputStyle={{ resize: style?.resize }}
233+
dataAttrs={{
234+
affixWrapper: {
235+
'data-count': typeof dataCount === 'string' ? dataCount : undefined,
236+
},
237+
}}
206238
inputElement={
207239
<ResizableTextArea
208240
{...rest}
@@ -225,37 +257,6 @@ const TextArea = React.forwardRef<TextAreaRef, TextAreaProps>(
225257
/>
226258
);
227259

228-
if (showCount) {
229-
const valueLength = [...val].length;
230-
231-
let dataCount: ReactNode;
232-
if (typeof showCount === 'object') {
233-
dataCount = showCount.formatter({
234-
value: val,
235-
count: valueLength,
236-
maxLength,
237-
});
238-
} else {
239-
dataCount = `${valueLength}${hasMaxLength ? ` / ${maxLength}` : ''}`;
240-
}
241-
242-
return (
243-
<div
244-
hidden={rest.hidden}
245-
className={classNames(
246-
`${prefixCls}-show-count`,
247-
className,
248-
classes?.countWrapper,
249-
)}
250-
style={style}
251-
data-count={dataCount}
252-
>
253-
{textarea}
254-
<span className={`${prefixCls}-data-count`}>{dataCount}</span>
255-
</div>
256-
);
257-
}
258-
259260
return textarea;
260261
},
261262
);

src/interface.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ export type TextAreaProps = Omit<HTMLTextareaProps, 'onResize'> & {
2626
textarea?: string;
2727
countWrapper?: string;
2828
affixWrapper?: string;
29+
count?: string;
2930
};
3031
} & Pick<BaseInputProps, 'allowClear' | 'suffix'>;
3132

0 commit comments

Comments
 (0)