Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.5k views
in Technique[技术] by (71.8m points)

javascript - How I can change a Button into a input text?

So, I'm trying to create a button that when I click it, it should change into a input with a another button for submit. I'm using react and bootstrap.

 <div className="cold-md-2 col-sm-3">
    <div className="card-body">
        <button class="btn btn-lg btn-block btn-group py-3" type="button">New group 
        <i class="fas fa-plus"></i></button>
     </div>
 </div>
question from:https://stackoverflow.com/questions/65834754/how-i-can-change-a-button-into-a-input-text

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You should probably create a state indicating wheter should render a button or an input, then on your render you check wich one you should render.

export default class Test extends PureComponent {
    constructor(props) {
        super(props);

        this.state = {
            type: 'button'
        };
    }

    toggleType() {
        this.setState({
            type: this.state.type === 'button' ? 'input' : 'button'
        });
    }

    render() {
        if (this.state.type === 'input')
            return <span>In here its the input HTML</span>;

        return (
            <button onClick={this.toggleType.bind(this)} type="button">Toggle</button>
        );
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...