Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

migrated ThreeDotsMenu/index.js to a functional component #24017

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 50 additions & 62 deletions src/components/ThreeDotsMenu/index.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
import React, {Component} from 'react';
import React, {useState, useRef} from 'react';
import {View} from 'react-native';
import PropTypes from 'prop-types';
import _ from 'underscore';
import Icon from '../Icon';
import PopoverMenu from '../PopoverMenu';
import styles from '../../styles/styles';
import withLocalize, {withLocalizePropTypes} from '../withLocalize';
import useLocalize from '../../hooks/useLocalize';
import Tooltip from '../Tooltip';
import * as Expensicons from '../Icon/Expensicons';
import ThreeDotsMenuItemPropTypes from './ThreeDotsMenuItemPropTypes';
import CONST from '../../CONST';
import PressableWithoutFeedback from '../Pressable/PressableWithoutFeedback';

const propTypes = {
...withLocalizePropTypes,

/** Tooltip for the popup icon */
iconTooltip: PropTypes.string,

Expand Down Expand Up @@ -61,68 +59,58 @@ const defaultProps = {
},
};
bondydaa marked this conversation as resolved.
Show resolved Hide resolved

class ThreeDotsMenu extends Component {
constructor(props) {
super(props);

this.hidePopoverMenu = this.hidePopoverMenu.bind(this);
this.showPopoverMenu = this.showPopoverMenu.bind(this);
this.state = {
isPopupMenuVisible: false,
};
this.buttonRef = React.createRef(null);
}

showPopoverMenu() {
this.setState({isPopupMenuVisible: true});
}

hidePopoverMenu() {
this.setState({isPopupMenuVisible: false});
}

render() {
return (
<>
<View>
<Tooltip text={this.props.translate(this.props.iconTooltip)}>
<PressableWithoutFeedback
onPress={() => {
this.showPopoverMenu();
if (this.props.onIconPress) {
this.props.onIconPress();
}
}}
ref={this.buttonRef}
style={[styles.touchableButtonImage, ...this.props.iconStyles]}
accessibilityRole={CONST.ACCESSIBILITY_ROLE.BUTTON}
accessibilityLabel={this.props.translate(this.props.iconTooltip)}
>
<Icon
src={this.props.icon}
fill={this.props.iconFill}
/>
</PressableWithoutFeedback>
</Tooltip>
</View>
<PopoverMenu
onClose={this.hidePopoverMenu}
isVisible={this.state.isPopupMenuVisible}
anchorPosition={this.props.anchorPosition}
anchorAlignment={this.props.anchorAlignment}
onItemSelected={this.hidePopoverMenu}
menuItems={this.props.menuItems}
anchorRef={this.buttonRef}
withoutOverlay
/>
</>
);
}
function ThreeDotsMenu({iconTooltip, icon, iconFill, iconStyles, onIconPress, menuItems, anchorPosition, anchorAlignment}) {
const [isPopupMenuVisible, setPopupMenuVisible] = useState(false);
const buttonRef = useRef(null);
const {translate} = useLocalize();

const showPopoverMenu = () => {
setPopupMenuVisible(true);
};

const hidePopoverMenu = () => {
setPopupMenuVisible(false);
};

return (
<>
<View>
<Tooltip text={translate(iconTooltip)}>
<PressableWithoutFeedback
onPress={() => {
showPopoverMenu();
if (onIconPress) {
onIconPress();
}
}}
ref={buttonRef}
style={[styles.touchableButtonImage, ...iconStyles]}
accessibilityRole={CONST.ACCESSIBILITY_ROLE.BUTTON}
accessibilityLabel={translate(iconTooltip)}
>
<Icon
src={icon}
fill={iconFill}
/>
</PressableWithoutFeedback>
</Tooltip>
</View>
<PopoverMenu
onClose={hidePopoverMenu}
isVisible={isPopupMenuVisible}
anchorPosition={anchorPosition}
anchorAlignment={anchorAlignment}
onItemSelected={hidePopoverMenu}
menuItems={menuItems}
/>
</>
);
}

ThreeDotsMenu.propTypes = propTypes;
ThreeDotsMenu.defaultProps = defaultProps;
ThreeDotsMenu.displayName = 'ThreeDotsMenu';

export default withLocalize(ThreeDotsMenu);
export default ThreeDotsMenu;

export {ThreeDotsMenuItemPropTypes};
Loading