console: migrate CollapsibleToggle to TS (#4697)

This commit is contained in:
Kshitiz Kumar 2020-05-29 18:34:28 +05:30 committed by GitHub
parent 6b590cb4f8
commit a2a492e5d9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,5 +1,5 @@
import React from 'react'; import React from 'react';
import styles from './CollapsibleToggle.scss';
/** /**
* Accepts following props * Accepts following props
* `title, string || react-element `: Title of the collapsible toggle * `title, string || react-element `: Title of the collapsible toggle
@ -9,8 +9,24 @@ import React from 'react';
* `children, react-element`: The content which needs to be toggled * `children, react-element`: The content which needs to be toggled
*/ */
class CollapsibleToggle extends React.Component { interface CollapsibleToggleProps {
constructor(props) { title: string;
isOpen: boolean;
toggleHandler?: () => void;
testId: string;
useDefaultTitleStyle?: boolean;
}
interface CollapsibleToggleState {
isOpen: boolean;
toggleHandler: () => void;
}
class CollapsibleToggle extends React.Component<
CollapsibleToggleProps,
CollapsibleToggleState
> {
constructor(props: CollapsibleToggleProps) {
super(props); super(props);
this.state = { this.state = {
@ -20,35 +36,35 @@ class CollapsibleToggle extends React.Component {
}; };
} }
componentWillReceiveProps(nextProps: CollapsibleToggleProps) {
const { isOpen, toggleHandler } = nextProps;
if (toggleHandler) {
this.setState({ isOpen, toggleHandler });
}
}
defaultToggleHandler() { defaultToggleHandler() {
this.setState({ isOpen: !this.state.isOpen }); this.setState({ isOpen: !this.state.isOpen });
} }
componentWillReceiveProps(nextProps) {
const { isOpen, toggleHandler } = nextProps;
if (toggleHandler) {
this.setState({ isOpen: isOpen, toggleHandler: toggleHandler });
}
}
render() { render() {
const styles = require('./CollapsibleToggle.scss');
const { title, children, testId, useDefaultTitleStyle } = this.props; const { title, children, testId, useDefaultTitleStyle } = this.props;
const { isOpen, toggleHandler } = this.state; const { isOpen, toggleHandler } = this.state;
const getTitle = () => { const getTitle = () => {
let _title; let resultTitle;
if (useDefaultTitleStyle) { if (useDefaultTitleStyle) {
_title = <div className={styles.defaultCollapsibleTitle}>{title}</div>; resultTitle = (
<div className={styles.defaultCollapsibleTitle}>{title}</div>
);
} else { } else {
_title = title; resultTitle = title;
} }
return _title; return resultTitle;
}; };
const getChildren = () => { const getChildren = () => {
@ -61,6 +77,9 @@ class CollapsibleToggle extends React.Component {
className={styles.collapsibleToggle} className={styles.collapsibleToggle}
data-test={testId} data-test={testId}
onClick={toggleHandler} onClick={toggleHandler}
onKeyDown={toggleHandler}
role="button"
tabIndex={0}
> >
<span className={styles.collapsibleIndicatorWrapper}> <span className={styles.collapsibleIndicatorWrapper}>
<i <i