html输入框自动调整,react-input-autosize - React的自动调整大小输入框

React-Input-Autosize是一款React组件,提供自动调整大小的文本输入框。它包括本地演示和例子,可以通过NPM安装或直接在页面中使用UMD构建。组件依赖于value prop来工作,需要注意样式改变、CSP限制和自定义字体大小的适配问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

React-Input-Autosize

A text input for React that resizes itself to the current content.

Demo & Examples

To run the examples locally, run:

npm install

npm start

Then open localhost:8000 in a browser.

Installation

The easiest way to use React-Input-Autosize is to install it from NPM and include it in your own React build process (using Browserify, rollup, webpack, etc).

You can also use the umd build by including dist/AutosizeInput.js in your page. If you use this, make sure you have already included a umd React build.

npm install react-input-autosize --save

Usage

React-Input-Autosize generates an input field, wrapped in a

tag so it can detect the size of its value. Otherwise it behaves very similarly to a standard React input.

import AutosizeInput from 'react-input-autosize';

name="form-field-name"

value={inputValue}

onChange={function(event) {

// event.target.value contains the new value

}}

/>

Gotchas

Changing the styles at runtime

The styles applied to the input are only copied when the component mounts. Because of this, subsequent changes to the stylesheet may cause size to be detected incorrectly.

To work around this, either re-mount the input (e.g. by providing a different key prop) or call the copyInputStyles() method after the styles change.

CSP and the IE "clear" indicator

The input will automatically inject a stylesheet that hides IE/Edge's "clear" indicator, which otherwise breaks the UI. This has the downside of being incompatible with some CSP policies.

To work around this, you can pass the injectStyles={false} prop, but if you do this I strongly recommend targeting the input element in your own stylesheet with the following rule:

input::-ms-clear {display: none;}

Custom font sizes

If your input uses custom font sizes, you will need to provide the custom size to AutosizeInput.

name="form-field-name"

value={inputValue}

style={{ fontSize: 36 }}

onChange={function(event) {

// event.target.value contains the new value

}}

/>

Uncontrolled input

AutosizeInput is a controlled input and depends on the value prop to work as intended. It does not support being used as an uncontrolled input.

License

Copyright (c) 2018 Jed Watson. MIT License.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值