make session dropdown use hooks

pull/1381/head
Audric Ackermann 5 years ago
parent e5ef061fbe
commit d2ada105ed
No known key found for this signature in database
GPG Key ID: 999F434D76324AD4

@ -63,9 +63,6 @@ const {
SessionConfirm, SessionConfirm,
} = require('../../ts/components/session/SessionConfirm'); } = require('../../ts/components/session/SessionConfirm');
const {
SessionDropdown,
} = require('../../ts/components/session/SessionDropdown');
const { const {
SessionRegistrationView, SessionRegistrationView,
} = require('../../ts/components/session/SessionRegistrationView'); } = require('../../ts/components/session/SessionRegistrationView');
@ -267,7 +264,6 @@ exports.setup = (options = {}) => {
SessionSeedModal, SessionSeedModal,
SessionPasswordModal, SessionPasswordModal,
SessionPasswordPrompt, SessionPasswordPrompt,
SessionDropdown,
MediaGallery, MediaGallery,
Message, Message,
MessageBody, MessageBody,

@ -1,4 +1,4 @@
import React from 'react'; import React, { useState } from 'react';
import { SessionIcon, SessionIconSize, SessionIconType } from './icon/'; import { SessionIcon, SessionIconSize, SessionIconType } from './icon/';
import { import {
@ -8,11 +8,7 @@ import {
// THIS IS DROPDOWN ACCORDIAN STYLE OPTIONS SELECTOR ELEMENT, NOT A CONTEXTMENU // THIS IS DROPDOWN ACCORDIAN STYLE OPTIONS SELECTOR ELEMENT, NOT A CONTEXTMENU
interface State { type Props = {
expanded: boolean;
}
interface Props {
label: string; label: string;
onClick?: any; onClick?: any;
expanded?: boolean; expanded?: boolean;
@ -24,72 +20,49 @@ interface Props {
active?: boolean; active?: boolean;
onClick?: any; onClick?: any;
}>; }>;
} };
export class SessionDropdown extends React.Component<Props, State> {
public static defaultProps = {
expanded: false,
};
constructor(props: any) {
super(props);
this.state = {
expanded: !!this.props.expanded,
};
this.toggleDropdown = this.toggleDropdown.bind(this);
}
public render() { export const SessionDropdown = (props: Props) => {
const { label, options } = this.props; const { label, options } = props;
const { expanded } = this.state; const [expanded, setExpanded] = useState(!!props.expanded);
const chevronOrientation = expanded ? 180 : 0; const chevronOrientation = expanded ? 180 : 0;
return ( return (
<div className="session-dropdown"> <div className="session-dropdown">
<div <div
className="session-dropdown__label" className="session-dropdown__label"
onClick={this.toggleDropdown} onClick={() => {
role="button" setExpanded(!expanded);
> }}
{label} role="button"
<SessionIcon >
iconType={SessionIconType.Chevron} {label}
iconSize={SessionIconSize.Small} <SessionIcon
iconRotation={chevronOrientation} iconType={SessionIconType.Chevron}
/> iconSize={SessionIconSize.Small}
</div> iconRotation={chevronOrientation}
/>
{expanded && (
<div className="session-dropdown__list-container">
{options.map((item: any) => {
return (
<SessionDropdownItem
key={item.content}
content={item.content}
icon={item.icon}
type={item.type}
active={item.active}
onClick={() => {
this.handleItemClick(item.onClick);
}}
/>
);
})}
</div>
)}
</div> </div>
);
}
public toggleDropdown() { {expanded && (
this.setState({ <div className="session-dropdown__list-container">
expanded: !this.state.expanded, {options.map((item: any) => {
}); return (
} <SessionDropdownItem
key={item.content}
public handleItemClick(itemOnClickFn: any) { content={item.content}
this.setState({ expanded: false }, itemOnClickFn()); icon={item.icon}
} type={item.type}
} active={item.active}
onClick={() => {
setExpanded(false);
item.onClick();
}}
/>
);
})}
</div>
)}
</div>
);
};

@ -8,54 +8,40 @@ export enum SessionDropDownItemType {
Danger = 'danger', Danger = 'danger',
} }
interface Props { type Props = {
content: string; content: string;
type: SessionDropDownItemType; type: SessionDropDownItemType;
icon: SessionIconType | null; icon: SessionIconType | null;
active: boolean; active: boolean;
onClick: any; onClick: any;
} };
export class SessionDropdownItem extends React.PureComponent<Props> {
public static defaultProps = {
type: SessionDropDownItemType.Default,
icon: null,
active: false,
onClick: () => null,
};
constructor(props: any) {
super(props);
this.clickHandler = this.clickHandler.bind(this);
}
public render() {
const { content, type, icon, active } = this.props;
return ( export const SessionDropdownItem = (props: Props) => {
<div const clickHandler = (e: any) => {
className={classNames( if (props.onClick) {
'session-dropdown__item',
active ? 'active' : '',
type || ''
)}
role="button"
onClick={this.clickHandler}
>
{icon ? (
<SessionIcon iconType={icon} iconSize={SessionIconSize.Small} />
) : (
''
)}
<div className="item-content">{content}</div>
</div>
);
}
private clickHandler(e: any) {
if (this.props.onClick) {
e.stopPropagation(); e.stopPropagation();
this.props.onClick(); props.onClick();
} }
} };
}
const { content, type, icon, active } = props;
return (
<div
className={classNames(
'session-dropdown__item',
active ? 'active' : '',
type || SessionDropDownItemType.Default
)}
role="button"
onClick={clickHandler}
>
{icon ? (
<SessionIcon iconType={icon} iconSize={SessionIconSize.Small} />
) : (
''
)}
<div className="item-content">{content}</div>
</div>
);
};

Loading…
Cancel
Save