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的更多相关文章
随机推荐
- SpringMVC基础配置及使用
SpringMVC基础配置及使用 SpringMVC:1.SpringMVC和Spring的关系: 软件开发的三层架构: web层[表示层.表现层]---->Service层----> ...
- 【Unity】物理碰撞实验
http://www.cnblogs.com/javawebsoa/archive/2013/05/18/3085818.html 这几天为了准备面试,所以决定对平时学习中的盲点扫盲一下,首先想到的就 ...
- Unity 5.6中的混合光照(下)
https://mp.weixin.qq.com/s/DNQFsWpZm-ybIlF3DTAk2A 在<Unity 5.6中的混合光照(上)>中,我们介绍了混合模式,以及Subtracti ...
- 截取HTML中的JSON数据并利用GSON进行解析(Android)
截取HTML中的JSON数据并利用GSON进行解析(Android) 前言 最近在做的一个Android项目,需要自行搭建服务器,队友选择买了阿里云的服务器ESC产品,在数据获取上,我们采用了Andr ...
- 剑指Offer的学习笔记(C#篇)-- 树的子结构
题目描述 输入两棵二叉树A,B,判断B是不是A的子结构.(ps:我们约定空树不是任意一个树的子结构) 一 . 二叉树的概念 树形结构是一种典型的非线性结构,除了用于表示相邻关系外,还可 ...
- UIActionSheet的最后一项点击失效
在开发过程中,发现有时候UIActionSheet的最后一项点击失效,点最后一项的上半区域时有效,这是在特定情况下才会发生,这个场景就是试用了UITabBar的时候才有.解决办法: 在showView ...
- 深入理解JVM的类加载
前言: 前面又说到Java程序实际上是将.class文件放入JVM中运行.虚拟机把描述类的数据从Class文件加载到内存,并对数据进行校验,转换,解析和初始化,最终形成可以被虚拟机直接使用的Java类 ...
- SpringMVC之WebMVC介绍
一.MVC是什么 二.常用的MVC框架 三.MVC模式的优缺点 四.SpringMVC简介
- Codeforces 1105D(双层广搜)
要点 题意:可以拐弯,即哈密顿距离 注意不可以直接一个一个搜,这过程中会把下一轮的标记上,导致同一轮的其它点没能正常完成应有的搜索 因此采用双层广搜,把同一轮先都出队列再的一起搜 #include & ...
- Codeforces Round #431 (Div. 2) A
Where do odds begin, and where do they end? Where does hope emerge, and will they ever break? Given ...