AutoWidthInput
import React from 'react';
import PropTypes from 'prop-types'; class AutoWidthInput extends React.Component { static propTypes = {
value: PropTypes.string,
placeholder: PropTypes.string,
minWidth: PropTypes.number,
onChange: PropTypes.func
} static defaultProps = {
minWidth: 28,
onChange: () => { }
} constructor(props) {
super(props);
this.state = {
inputWidth: props.minWidth
};
} componentDidMount() {
this.updateInputWidth();
} componentDidUpdate() {
this.updateInputWidth();
} updateInputWidth = () => {
let width = Math.max(this.sizer.scrollWidth, this.placeholderSizer.scrollWidth) + 2;
width = Math.max(width, this.props.minWidth);
const { inputWidth } = this.state;
if (inputWidth !== width) {
this.setState({ inputWidth: width });
}
}
handleChange = (e) => {
this.setState({ value: e.target.value });
this.props.onChange(e);
}
render() {
const { inputWidth } = this.state;
const { value, placeholder } = this.props;
return (
<div style={{ display: 'inline-block' }}>
<input
style={{ width: inputWidth }}
type="text"
value={value}
onChange={this.handleChange}
placeholder={placeholder}
/>
<div ref={(div) => { this.sizer = div; }} style={{ position: 'absolute', top: 0, left: 0, visibility: 'hidden', height: 0, overflow: 'scroll', whiteSpace: 'pre' }}>
{value}
</div>
<div ref={(div) => { this.placeholderSizer = div; }} style={{ position: 'absolute', top: 0, left: 0, visibility: 'hidden', height: 0, overflow: 'scroll', whiteSpace: 'pre' }}>
{placeholder}
</div>
</div>
);
}
} export default AutoWidthInput;
AutoWidthInput的更多相关文章
随机推荐
- Ext.apply(src,apply) 和 Ext.applyIf(src,apply)比较(转)
Ext.onReady(function(){ /* * Ext.apply(src,apply) 和 Ext.applyIf(src,apply) 两个方法的使用和区别比较 */ //Ext.app ...
- 2017-9-2 NOIP模拟赛
“与” (and.pas/.c/.cpp) 时间限制:1s:空间限制64MB 题目描述: 给你一个长度为n的序列A,请你求出一对Ai,Aj(1<=i<j<=n)使Ai“与”Aj最大. ...
- 前端三部曲之Html -- 1(html的基本结构和常见的meta标签的作用)
一个H5页面的基本结构是什么 我么在编辑器中输入html:5可以得到 <!DOCTYPE html> <!-- 声明文档类型 --> <html lang="e ...
- 更新常用的js工具函数
在手机调试时打印代码<script src="https://cdn.bootcss.com/vConsole/3.3.0/vconsole.min.js"></ ...
- Web 加入favicon
一.点击 制作自己的favicon图标; 二.在网页head中加入: <link rel="shortcut icon" href="favicon.ico& ...
- 浅谈最近公共祖先(LCA)
LCA(Least Common Ancestors),即最近公共祖先,是指在有根树中,找出某两个结点u和v最近的公共祖先. (来自百度百科) 一.倍增求LCA 预处理出距点u距离为2^0,2^1,2 ...
- Codeforces 140E(排列组合、dp)
要点 主要学到的东西:一个序列染色,相邻不染同色,恰用\(j\)种颜色的1.模式数.2.方案数.3.具体染色数. 从大的思路上来讲:先dp预处理出每一层的模式数:\(f[i][j]\)表示\(i\)个 ...
- Jaspersoft Studio简介
参考来源:https://community.jaspersoft.com/documentation/tibco-jaspersoft-studio-user-guide/v640/introduc ...
- [RDL]中多行组列组占比报表制作
结果如下: 生意额占比表达式:=iif(Fields!生意额.Value is nothing,"",Fields!生意额.Value/sum(Fields!生意额.Value, ...
- MapReduce编程入门实例之WordCount:分别在Eclipse和Hadoop集群上运行
上一篇博文如何在Eclipse下搭建Hadoop开发环境,今天给大家介绍一下如何分别分别在Eclipse和Hadoop集群上运行我们的MapReduce程序! 1. 在Eclipse环境下运行MapR ...