Indeterminate checkbox in React JSX

前端 未结 7 1565
旧时难觅i
旧时难觅i 2021-01-01 17:00

How do I render an indeterminate checkbox via JSX?

Here\'s what I\'ve tried:

function ICB({what}) {
  return 

        
相关标签:
7条回答
  • 2021-01-01 17:32

    React v15 implementation:

    import React from 'react';
    
    export default class Checkbox extends React.Component {
        componentDidMount() {
            this.el.indeterminate = this.props.indeterminate;
        }
    
        componentDidUpdate(prevProps, prevState) {
            if(prevProps.indeterminate !== this.props.indeterminate) {
                this.el.indeterminate = this.props.indeterminate;
            }
        }
    
        render() {
            const {indeterminate, ...attrs} = this.props;
            return <input ref={el => {this.el = el}} type="checkbox" {...attrs}/>;
        }
    }
    
    0 讨论(0)
提交回复
热议问题