Skip to content

Commit dc41d3f

Browse files
committed
Add ignorePlaceholder
1 parent ab4c1ec commit dc41d3f

File tree

4 files changed

+48
-2
lines changed

4 files changed

+48
-2
lines changed

.changeset/breezy-dodos-lick.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'react-textarea-autosize': minor
3+
---
4+
5+
Add `ignorePlaceholder` option

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ https://andarist.github.io/react-textarea-autosize/
4242
| `minRows` | `number` | Minimum number of rows to show for textarea |
4343
| `onHeightChange` | `func` | Function invoked on textarea height change, with height as first argument. The second function argument is an object containing additional information that might be useful for custom behaviors. Current options include `{ rowHeight: number }`. |
4444
| `cacheMeasurements` | `boolean` | Reuse previously computed measurements when computing height of textarea. Default: `false` |
45+
| `ignorePlaceholder` | `boolean` | Whether to ignore placeholder when computing height of textarea. Default: `false` |
4546

4647
Apart from these, the component accepts all props that are accepted by `<textarea/>`, like `style`, `onChange`, `value`, etc.
4748

example/index.tsx

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,42 @@ const SetRows = () => {
100100
);
101101
};
102102

103+
const WithPlaceholder = () => {
104+
return (
105+
<div>
106+
<h2>{'Component with placeholder'}</h2>
107+
<pre>
108+
{`
109+
<TextareaAutosize
110+
placeholder="The quick brown fox jumps over the lazy dog..."
111+
/>
112+
`}
113+
</pre>
114+
<TextareaAutosize placeholder="The quick brown fox jumps over the lazy dog..." />
115+
</div>
116+
);
117+
};
118+
119+
const WithIgnoredPlaceholder = () => {
120+
return (
121+
<div>
122+
<h2>{'Component with ignored placeholder'}</h2>
123+
<pre>
124+
{`
125+
<TextareaAutosize
126+
placeholder="The quick brown fox jumps over the lazy dog..."
127+
ignorePlaceholder
128+
/>
129+
`}
130+
</pre>
131+
<TextareaAutosize
132+
placeholder="The quick brown fox jumps over the lazy dog..."
133+
ignorePlaceholder
134+
/>
135+
</div>
136+
);
137+
};
138+
103139
const ControlledMode = () => {
104140
const [value, setValue] = React.useState(new Array(15).join('\nLine.'));
105141
return (
@@ -210,6 +246,8 @@ const Demo = () => {
210246
<MinMaxRowsBorderBox />
211247
<MaxRows />
212248
<SetRows />
249+
<WithPlaceholder />
250+
<WithIgnoredPlaceholder />
213251
<ControlledMode />
214252
<UncontrolledMode />
215253
<OnHeightChangeCallback />

src/index.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ export interface TextareaAutosizeProps extends Omit<TextareaProps, 'style'> {
2525
minRows?: number;
2626
onHeightChange?: (height: number, meta: TextareaHeightChangeMeta) => void;
2727
cacheMeasurements?: boolean;
28+
ignorePlaceholder?: boolean;
2829
style?: Style;
2930
}
3031

@@ -33,7 +34,8 @@ const TextareaAutosize: React.ForwardRefRenderFunction<
3334
TextareaAutosizeProps
3435
> = (
3536
{
36-
cacheMeasurements,
37+
cacheMeasurements = false,
38+
ignorePlaceholder = false,
3739
maxRows,
3840
minRows,
3941
onChange = noop,
@@ -75,7 +77,7 @@ const TextareaAutosize: React.ForwardRefRenderFunction<
7577

7678
const [height, rowHeight] = calculateNodeHeight(
7779
nodeSizingData,
78-
node.value || node.placeholder || 'x',
80+
node.value || (!ignorePlaceholder && node.placeholder) || 'x',
7981
minRows,
8082
maxRows,
8183
);

0 commit comments

Comments
 (0)