2016-06-10 16:44:01 +08:00
---
order: 6
2016-08-22 09:53:39 +08:00
title:
2019-05-07 14:57:32 +08:00
zh-CN: 适应文本高度的文本域
en-US: Autosizing the height to fit the content
2016-06-10 16:44:01 +08:00
---
2016-08-22 09:53:39 +08:00
## zh-CN
2019-10-11 18:12:28 +08:00
`autoSize` 属性适用于 `textarea` 节点,并且只有高度会自动变化。另外 `autoSize` 可以设定为一个对象,指定最小行数和最大行数。
2016-06-10 16:44:01 +08:00
2016-08-22 09:53:39 +08:00
## en-US
2020-06-17 10:58:24 +08:00
`autoSize` prop for a `textarea` type of `Input` makes the height to automatically adjust based on the content. An option object can be provided to `autoSize` to specify the minimum and maximum number of lines the textarea will automatically adjust.
2016-08-22 09:53:39 +08:00
2022-05-19 09:46:26 +08:00
```tsx
2016-06-10 16:44:01 +08:00
import { Input } from 'antd';
2022-05-23 14:37:16 +08:00
import React, { useState } from 'react';
2018-06-27 15:55:04 +08:00
2017-05-22 14:44:58 +08:00
const { TextArea } = Input;
2016-06-10 16:44:01 +08:00
2022-05-19 09:46:26 +08:00
const App: React.FC = () => {
const [value, setValue] = useState('');
2019-08-22 11:55:37 +08:00
2022-05-14 16:40:42 +08:00
return (
< >
< TextArea placeholder = "Autosize height based on content lines" autoSize / >
< div style = {{ margin: ' 24px 0 ' } } / >
< TextArea
placeholder="Autosize height with minimum and maximum number of lines"
autoSize={{ minRows: 2, maxRows: 6 }}
/>
< div style = {{ margin: ' 24px 0 ' } } / >
< TextArea
value={value}
2022-05-19 09:46:26 +08:00
onChange={e => setValue(e.target.value)}
2022-05-14 16:40:42 +08:00
placeholder="Controlled autosize"
autoSize={{ minRows: 3, maxRows: 5 }}
/>
< />
);
};
2022-05-19 09:46:26 +08:00
export default App;
2019-05-07 14:57:32 +08:00
```