Skip to content

Add ignorePlaceholder #369

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/breezy-dodos-lick.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'react-textarea-autosize': minor
---

Add `ignorePlaceholder` option
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ https://andarist.github.io/react-textarea-autosize/
| `minRows` | `number` | Minimum number of rows to show for textarea |
| `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 }`. |
| `cacheMeasurements` | `boolean` | Reuse previously computed measurements when computing height of textarea. Default: `false` |
| `ignorePlaceholder` | `boolean` | Whether to ignore placeholder when computing height of textarea. Default: `false` |

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

Expand Down
38 changes: 38 additions & 0 deletions example/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,42 @@ const SetRows = () => {
);
};

const WithPlaceholder = () => {
return (
<div>
<h2>{'Component with placeholder'}</h2>
<pre>
{`
<TextareaAutosize
placeholder="The quick brown fox jumps over the lazy dog..."
/>
`}
</pre>
<TextareaAutosize placeholder="The quick brown fox jumps over the lazy dog..." />
</div>
);
};

const WithIgnoredPlaceholder = () => {
return (
<div>
<h2>{'Component with ignored placeholder'}</h2>
<pre>
{`
<TextareaAutosize
placeholder="The quick brown fox jumps over the lazy dog..."
ignorePlaceholder
/>
`}
</pre>
<TextareaAutosize
placeholder="The quick brown fox jumps over the lazy dog..."
ignorePlaceholder
/>
</div>
);
};

const ControlledMode = () => {
const [value, setValue] = React.useState(new Array(15).join('\nLine.'));
return (
Expand Down Expand Up @@ -210,6 +246,8 @@ const Demo = () => {
<MinMaxRowsBorderBox />
<MaxRows />
<SetRows />
<WithPlaceholder />
<WithIgnoredPlaceholder />
<ControlledMode />
<UncontrolledMode />
<OnHeightChangeCallback />
Expand Down
6 changes: 4 additions & 2 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export interface TextareaAutosizeProps extends Omit<TextareaProps, 'style'> {
minRows?: number;
onHeightChange?: (height: number, meta: TextareaHeightChangeMeta) => void;
cacheMeasurements?: boolean;
ignorePlaceholder?: boolean;
style?: Style;
}

Expand All @@ -33,7 +34,8 @@ const TextareaAutosize: React.ForwardRefRenderFunction<
TextareaAutosizeProps
> = (
{
cacheMeasurements,
cacheMeasurements = false,
ignorePlaceholder = false,
maxRows,
minRows,
onChange = noop,
Expand Down Expand Up @@ -75,7 +77,7 @@ const TextareaAutosize: React.ForwardRefRenderFunction<

const [height, rowHeight] = calculateNodeHeight(
nodeSizingData,
node.value || node.placeholder || 'x',
node.value || (!ignorePlaceholder && node.placeholder) || 'x',
minRows,
maxRows,
);
Expand Down