2017-08-25 139 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>