From d2ada105ed587158f4b12988f2a98ce370904080 Mon Sep 17 00:00:00 2001 From: Audric Ackermann Date: Tue, 17 Nov 2020 10:51:39 +1100 Subject: [PATCH] make session dropdown use hooks --- js/modules/signal.js | 4 - ts/components/session/SessionDropdown.tsx | 115 +++++++----------- ts/components/session/SessionDropdownItem.tsx | 72 +++++------ 3 files changed, 73 insertions(+), 118 deletions(-) diff --git a/js/modules/signal.js b/js/modules/signal.js index 3f0e6e026..404efed5f 100644 --- a/js/modules/signal.js +++ b/js/modules/signal.js @@ -63,9 +63,6 @@ const { SessionConfirm, } = require('../../ts/components/session/SessionConfirm'); -const { - SessionDropdown, -} = require('../../ts/components/session/SessionDropdown'); const { SessionRegistrationView, } = require('../../ts/components/session/SessionRegistrationView'); @@ -267,7 +264,6 @@ exports.setup = (options = {}) => { SessionSeedModal, SessionPasswordModal, SessionPasswordPrompt, - SessionDropdown, MediaGallery, Message, MessageBody, diff --git a/ts/components/session/SessionDropdown.tsx b/ts/components/session/SessionDropdown.tsx index b6b005e73..746828a64 100644 --- a/ts/components/session/SessionDropdown.tsx +++ b/ts/components/session/SessionDropdown.tsx @@ -1,4 +1,4 @@ -import React from 'react'; +import React, { useState } from 'react'; import { SessionIcon, SessionIconSize, SessionIconType } from './icon/'; import { @@ -8,11 +8,7 @@ import { // THIS IS DROPDOWN ACCORDIAN STYLE OPTIONS SELECTOR ELEMENT, NOT A CONTEXTMENU -interface State { - expanded: boolean; -} - -interface Props { +type Props = { label: string; onClick?: any; expanded?: boolean; @@ -24,72 +20,49 @@ interface Props { active?: boolean; onClick?: any; }>; -} - -export class SessionDropdown extends React.Component { - public static defaultProps = { - expanded: false, - }; - - constructor(props: any) { - super(props); - - this.state = { - expanded: !!this.props.expanded, - }; - - this.toggleDropdown = this.toggleDropdown.bind(this); - } +}; - public render() { - const { label, options } = this.props; - const { expanded } = this.state; - const chevronOrientation = expanded ? 180 : 0; +export const SessionDropdown = (props: Props) => { + const { label, options } = props; + const [expanded, setExpanded] = useState(!!props.expanded); + const chevronOrientation = expanded ? 180 : 0; - return ( -
-
- {label} - -
- - {expanded && ( -
- {options.map((item: any) => { - return ( - { - this.handleItemClick(item.onClick); - }} - /> - ); - })} -
- )} + return ( +
+
{ + setExpanded(!expanded); + }} + role="button" + > + {label} +
- ); - } - public toggleDropdown() { - this.setState({ - expanded: !this.state.expanded, - }); - } - - public handleItemClick(itemOnClickFn: any) { - this.setState({ expanded: false }, itemOnClickFn()); - } -} + {expanded && ( +
+ {options.map((item: any) => { + return ( + { + setExpanded(false); + item.onClick(); + }} + /> + ); + })} +
+ )} +
+ ); +}; diff --git a/ts/components/session/SessionDropdownItem.tsx b/ts/components/session/SessionDropdownItem.tsx index 0a73678f5..f9775b4f3 100644 --- a/ts/components/session/SessionDropdownItem.tsx +++ b/ts/components/session/SessionDropdownItem.tsx @@ -8,54 +8,40 @@ export enum SessionDropDownItemType { Danger = 'danger', } -interface Props { +type Props = { content: string; type: SessionDropDownItemType; icon: SessionIconType | null; active: boolean; onClick: any; -} - -export class SessionDropdownItem extends React.PureComponent { - 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 ( -
- {icon ? ( - - ) : ( - '' - )} -
{content}
-
- ); - } - - private clickHandler(e: any) { - if (this.props.onClick) { +export const SessionDropdownItem = (props: Props) => { + const clickHandler = (e: any) => { + if (props.onClick) { e.stopPropagation(); - this.props.onClick(); + props.onClick(); } - } -} + }; + + const { content, type, icon, active } = props; + + return ( +
+ {icon ? ( + + ) : ( + '' + )} +
{content}
+
+ ); +};