2017-08-25 108 views
0

在连接到这个职位的片段时,不需要高度的变化,我想,当用户点击一个div来呈现input标签。该输入具有取div的地方,从而避免给予用户的任何暗示该组件被改变(如editablecell属性)。阵营 - 渲染输入字段

当输入被渲染,我遇到的eleme

const styles = { 
 
    cell: { 
 
    width: "200px", 
 
    border: "1px solid black", 
 
    lineHeight: "30px", 
 
    textAlign: "center", 
 
    verticalAlign: "middle" 
 
    }, 
 
    input: { 
 
    width: "200px" 
 
    } 
 
}; 
 

 
const text = "Click on me"; 
 

 
class App extends React.Component { 
 
    state = { 
 
    active: false 
 
    }; 
 

 
    handleClick =() => { 
 
    this.setState(prevState => { 
 
     return { 
 
     active: !prevState.active 
 
     }; 
 
    }); 
 
    }; 
 

 
    handleKeyPress = evt => { 
 
    if (evt.charCode === 13) { 
 
     this.setState(prevState => { 
 
     return { 
 
      active: !prevState.active 
 
     }; 
 
     }); 
 
    } 
 
    }; 
 

 
    render() { 
 
    if (this.state.active) { 
 
     return (
 
     <div> 
 
      <input 
 
      type="text" 
 
      style={styles.cell} 
 
      onKeyPress={this.handleKeyPress} 
 
      placeholder="Press ENTER when done" 
 
      /> 
 
      <p>Notice how the height is changing</p> 
 
     </div> 
 
    ); 
 
    } else { 
 
     return (
 
     <div> 
 
      <div style={styles.cell} onClick={this.handleClick}> 
 
      {text} 
 
      </div> 
 
      <p>Notice how the height is changing</p> 
 
     </div> 
 
    ); 
 
    } 
 
    } 
 
} 
 

 
ReactDOM.render(<App />, document.getElementById("root"));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script> 
 
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script> 
 

 
<div id="root"></div>

回答

1

你改变divinput彼此的高度变化,input有一个额外的padding因此,在增加height由1px的。添加padding: 1pxstyles.cell应该修复。

1

它与填充一个问题。用值设置它,它会按预期工作,看看这个代码。

const styles = { 
 
    cell: { 
 
    width: "200px", 
 
    border: "1px solid black", 
 
    lineHeight: "30px", 
 
    textAlign: "center", 
 
    verticalAlign: "middle", 
 
    padding: "0" 
 
    }, 
 
    input: { 
 
    width: "200px" 
 
    } 
 
}; 
 

 
const text = "Click on me"; 
 

 
class App extends React.Component { 
 
    state = { 
 
    active: false 
 
    }; 
 

 
    handleClick =() => { 
 
    this.setState(prevState => { 
 
     return { 
 
     active: !prevState.active 
 
     }; 
 
    }); 
 
    }; 
 

 
    handleKeyPress = evt => { 
 
    if (evt.charCode === 13) { 
 
     this.setState(prevState => { 
 
     return { 
 
      active: !prevState.active 
 
     }; 
 
     }); 
 
    } 
 
    }; 
 

 
    render() { 
 
    if (this.state.active) { 
 
     return (
 
     <div> 
 
      <input 
 
      type="text" 
 
      style={styles.cell} 
 
      onKeyPress={this.handleKeyPress} 
 
      placeholder="Press ENTER when done" 
 
      /> 
 
      <p>Notice how the height is changing</p> 
 
     </div> 
 
    ); 
 
    } else { 
 
     return (
 
     <div> 
 
      <div style={styles.cell} onClick={this.handleClick}> 
 
      {text} 
 
      </div> 
 
      <p>Notice how the height is not changing</p> 
 
     </div> 
 
    ); 
 
    } 
 
    } 
 
} 
 

 
ReactDOM.render(<App />, document.getElementById("root"));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script> 
 
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script> 
 

 
<div id="root"></div>