Skip to content

Commit 06d70ac

Browse files
authored
πŸ› fix: should parse textarea value and placeholder
Merge pull request #161 from ant-design/fix/textarea
2 parents 05ce9c7 + 143d84b commit 06d70ac

File tree

5 files changed

+64
-6
lines changed

5 files changed

+64
-6
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import React from 'react';
2+
import { useElements, TestLayout } from '@docs-utils';
3+
import { Input } from 'antd';
4+
5+
const {TextArea} = Input
6+
7+
/**
8+
*
9+
*/
10+
export default () => {
11+
const { elements, ref } = useElements();
12+
13+
return (
14+
<TestLayout elements={elements}>
15+
<div ref={ref}>
16+
<TextArea placeholder="ζ΅‹θ―• TextArea" />
17+
</div>
18+
</TestLayout>
19+
);
20+
};

β€Ždocs/e2e/basic/text.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,12 @@ placeholder ε’ŒθΎ“ε…₯ηš„ε€Όιƒ½εœ¨δΌͺη±»ι‡Œ
9595

9696
<code src="./demos/Text/Input.tsx" />
9797

98+
### 解析 TextArea `placeholder` ε’Œ `value`
99+
100+
placeholder ε’ŒθΎ“ε…₯ηš„ε€Όιƒ½εœ¨δΌͺη±»ι‡Œ
101+
102+
<code src="./demos/Text/TextArea.tsx" />
103+
98104
### Input ζ–‡ζœ¬ε±…δΈ­
99105

100106
<code src="./demos/Text/InputAligin.tsx" />

β€Žsrc/parser/inputText.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { getTextLinesAndRange } from '../utils/text';
55
* θ§£ζžθΎ“ε…₯ζ‘†ζ–‡ζœ¬
66
*/
77
export const parseInputTextToText = (
8-
node: HTMLInputElement,
8+
node: HTMLInputElement | HTMLTextAreaElement,
99
): Text | undefined => {
1010
// εˆ€ζ–­δΈ€δΈ‹ζ˜―ε¦ζœ‰δΌͺη±»
1111
const inputTextStyle: CSSStyleDeclaration = getComputedStyle(
@@ -16,7 +16,7 @@ export const parseInputTextToText = (
1616

1717
/// *** 倄理 input ηš„ζ–‡ζœ¬ε€Ό *** ///
1818

19-
const { value, placeholder } = node as HTMLInputElement;
19+
const { value, placeholder } = node;
2020
if (!value && !placeholder) return;
2121
if (value) {
2222
pseudoText = node.type === 'password' ? value.replace(/./g, 'β€’') : value;
@@ -97,7 +97,7 @@ export const parseInputTextToText = (
9797
const { lineHeight } = inputTextStyle;
9898

9999
// TODO: θΏ˜ζœ‰δ»€δΉˆζ—Άε€™ιœ€θ¦εž‚η›΄ε±…δΈ­ε‘’?
100-
if (parseFloat(lineHeight) > rangeBCR.height) {
100+
if (node.nodeName !== 'TEXTAREA' && parseFloat(lineHeight) > rangeBCR.height) {
101101
// ιœ€θ¦εž‚η›΄ε±…δΈ­ηš„εœ°ζ–Ή
102102
console.log(y, nodeBCR.y);
103103
console.log(nodeBCR.height, rangeBCR.height);

β€Žsrc/utils/nodeType.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ export const isImageNode = (node: Element): node is HTMLImageElement => {
4646
*/
4747
export const isTextInputNode = (node: Element): node is HTMLInputElement => {
4848
return (
49-
isNodeType(node, 'input') &&
49+
isNodeType(node, ['input', 'textarea']) &&
5050
(node as HTMLInputElement).type !== 'checkbox' &&
5151
(node as HTMLInputElement).type !== 'radio'
5252
);

β€Žtests/__tests__/parser/InputText.spec.ts

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { parseInputTextToText, Text } from 'html2sketch';
1+
import type { Text } from 'html2sketch';
2+
import { parseInputTextToText } from 'html2sketch';
23

34
describe('parseInputTextToText', () => {
45
beforeAll(() => {
@@ -7,7 +8,7 @@ describe('parseInputTextToText', () => {
78
body{
89
margin: 0;
910
}
10-
.input::placeholder{
11+
.input::placeholder {
1112
color:red;
1213
}
1314
</style>`;
@@ -18,11 +19,14 @@ describe('parseInputTextToText', () => {
1819
1920
2021
<input id="input" />
22+
<textarea id="pure-textarea"></textarea>
2123
2224
<input id="input-placeholder" class="input" placeholder="ζ΅‹θ―•θΎ“ε…₯摆" />
25+
<textarea id="textarea-placeholder" class="input" placeholder="ζ΅‹θ―•θΎ“ε…₯摆"></textarea>
2326
2427
<div>
2528
<input id="input-value" placeholder="ζ΅‹θ―•θΎ“ε…₯摆" value="θΏ™ζ˜―ε€Ό" />
29+
<textarea id="textarea-value" placeholder="ζ΅‹θ―•θΎ“ε…₯摆">θΏ™ζ˜―ε€Ό</textarea>
2630
</div>
2731
2832
@@ -41,6 +45,14 @@ describe('parseInputTextToText', () => {
4145
expect(input).toBeUndefined();
4246
});
4347

48+
it('textarea δΈθΏ”ε›ž', () => {
49+
const node = document.getElementById('pure-textarea') as HTMLTextAreaElement;
50+
console.log(node)
51+
const input = parseInputTextToText(node);
52+
53+
expect(input).toBeUndefined();
54+
});
55+
4456
it('input-placeholder θ§£ζžζˆζ–‡ζœ¬', () => {
4557
const node = document.getElementById(
4658
'input-placeholder',
@@ -51,6 +63,18 @@ describe('parseInputTextToText', () => {
5163
expect(json._class).toBe('text');
5264
expect(json.attributedString.string).toBe('ζ΅‹θ―•θΎ“ε…₯摆');
5365
});
66+
67+
it('textarea-placeholder θ§£ζžζˆζ–‡ζœ¬', () => {
68+
const node = document.getElementById(
69+
'textarea-placeholder',
70+
) as HTMLInputElement;
71+
const input = parseInputTextToText(node) as Text;
72+
expect(input.textStyle.color.red).toBe(255);
73+
const json = input.toSketchJSON();
74+
expect(json._class).toBe('text');
75+
expect(json.attributedString.string).toBe('ζ΅‹θ―•θΎ“ε…₯摆');
76+
});
77+
5478
it('input-value θ§£ζžζˆζ–‡ζœ¬', () => {
5579
const node = document.getElementById('input-value') as HTMLInputElement;
5680
const input = parseInputTextToText(node) as Text;
@@ -60,6 +84,14 @@ describe('parseInputTextToText', () => {
6084
expect(input.x).toBeLessThanOrEqual(2);
6185
});
6286

87+
it('textarea-value θ§£ζžζˆζ–‡ζœ¬', () => {
88+
const node = document.getElementById('textarea-value') as HTMLTextAreaElement;
89+
const input = parseInputTextToText(node) as Text;
90+
const json = input.toSketchJSON();
91+
expect(json._class).toBe('text');
92+
expect(json.attributedString.string).toBe('θΏ™ζ˜―ε€Ό');
93+
});
94+
6395
it('input-center θ§£ζžζˆζ–‡ζœ¬', () => {
6496
const node = document.getElementById('input-center') as HTMLInputElement;
6597
const input = parseInputTextToText(node) as Text;

0 commit comments

Comments
Β (0)