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的更多相关文章
随机推荐
- Android 自定义ViewGroup 实战篇 -> 实现FlowLayout
转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/38352503 ,本文出自[张鸿洋的博客] 1.概述 上一篇已经基本给大家介绍了如 ...
- 数据可视化系列--svg入门基础(一)
一.前言 1.SVG(Scalable Vector Graphics)可伸缩矢量图形 特点: (1)使用xml格式来定义图形: (2)用来定义web上的使用的矢量图: (3)改变图像尺寸,图片质量不 ...
- 51nod1282(最小表示法&&枚举)
题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1282 题意:中文题目诶- 思路:指针不可转,刻盘可转,显然,对 ...
- 51nod1102(单调栈/预处理)
题目链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1102 题意:中文题诶- 思路:单调栈/预处理 (这篇博客就不 ...
- [Xcode 实际操作]七、文件与数据-(5 )复制、移动、删除文件和删除文件夹
目录:[Swift]Xcode实际操作 本文将演示如何复制.移动和删除文件. 在项目导航区,打开视图控制器的代码文件[ViewController.swift] import UIKit class ...
- MCP|BFY|Proteome Analysis of Human Neutrophil Granulocytes From Patients With Monogenic Disease Using Data-independent Acquisition(单基因疾病患者中性粒细胞的DIA蛋白质组分析)
文献名:Proteome Analysis of Human Neutrophil Granulocytes From Patients With Monogenic Disease Using Da ...
- linux下提示/usr/lib64/libstdc++.so.6: version `GLIBCXX_3.4.14' not found 解决办法
1.查看gcc版本中包含哪些库. strings /usr/lib64/libstdc++.so.6 | grep GLIBC GLIBCXX_3. GLIBCXX_3.4.1 GLIBCXX_3.4 ...
- Python面向对象之结构与成员
1.面向对象结构分析: ----面相对象整体大致分为两块区域: --------第一部分:静态字段(静态变量)部分 --------第二部分:方法部分 --每个大区域可以分为多个小部分: class ...
- Python面向对象之鸭子类型
python没有多态?他有什么? 他有鸭子类型. 鸭子类型 : 看着像鸭子,他就是鸭子. 比如一些类,他们中有一些方法,有着相同的功能, 这时为我们将这些相同功能的名字命名为一样的. 那么这些类 都互 ...
- Hive进阶_Hive的表连接
等值连接 select e.empno, d.deptno from emp e, dept d where e.deptno=d.deptno; 不等值连接 select e.empno, e.en ...