All files / app/ui/toggle Toggle.tsx

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 3727x                     27x   129x   129x     27x 199x 199x   199x           129x 9x   9x   9x   27x  
import { Component, ReactNode } from 'react';
 
export interface ToggleProps {
    openByDefault?: boolean;
    children(props: any): ReactNode;
}
 
export interface ToggleState {
    isOpen: boolean;
}
 
export default class Toggle extends Component<ToggleProps, ToggleState> {
    constructor(props: ToggleProps) {
        super(props);
 
        this.state = { isOpen: !!props.openByDefault };
    }
 
    render(): ReactNode {
        const { children } = this.props;
        const { isOpen } = this.state;
 
        return children({
            isOpen,
            toggle: this.toggle,
        });
    }
 
    private toggle: (event: Event) => void = event => {
        const { isOpen } = this.state;
 
        event.preventDefault();
 
        this.setState({ isOpen: !isOpen });
    };
}