• 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={{  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;
    

      

  • 相关阅读:
    mac 下安装jenkins
    Appium元素定位难点:tap坐标定位不准确
    Appium元素定位难点:混合式的native+webview
    Linux 上安装 appium
    springMVC之AOP
    设计模式之装饰模式
    设计模式之桥接模式
    MyBatis特殊字符转义
    python+urllib+beautifulSoup实现一个简单的爬虫
    设计模式之代理模式
  • 原文地址:https://www.cnblogs.com/laneyfu/p/9299306.html
Copyright © 2020-2023  润新知