From d7af685e6e29efec125a116a1e654350b854e6a8 Mon Sep 17 00:00:00 2001 From: Janvo Aldred Date: Tue, 14 Jul 2020 03:16:14 -0500 Subject: [PATCH] Refactored URLPopover to use React Hook (#23918) --- .../src/components/url-popover/index.js | 104 ++++++++---------- .../components/url-popover/stories/index.js | 52 +++++++++ 2 files changed, 95 insertions(+), 61 deletions(-) create mode 100644 packages/block-editor/src/components/url-popover/stories/index.js diff --git a/packages/block-editor/src/components/url-popover/index.js b/packages/block-editor/src/components/url-popover/index.js index c2e3a766555f9..89a863e84a760 100644 --- a/packages/block-editor/src/components/url-popover/index.js +++ b/packages/block-editor/src/components/url-popover/index.js @@ -2,7 +2,7 @@ * WordPress dependencies */ import { __ } from '@wordpress/i18n'; -import { Component } from '@wordpress/element'; +import { useState } from '@wordpress/element'; import { Button, Popover } from '@wordpress/components'; import { chevronDown } from '@wordpress/icons'; @@ -12,73 +12,55 @@ import { chevronDown } from '@wordpress/icons'; import LinkViewer from './link-viewer'; import LinkEditor from './link-editor'; -class URLPopover extends Component { - constructor() { - super( ...arguments ); +function URLPopover( { + additionalControls, + children, + renderSettings, + position = 'bottom center', + focusOnMount = 'firstElement', + ...popoverProps +} ) { + const [ isSettingsExpanded, setIsSettingsExpanded ] = useState( false ); - this.toggleSettingsVisibility = this.toggleSettingsVisibility.bind( - this - ); + const showSettings = !! renderSettings && isSettingsExpanded; - this.state = { - isSettingsExpanded: false, - }; - } + const toggleSettingsVisibility = () => { + setIsSettingsExpanded( ! isSettingsExpanded ); + }; - toggleSettingsVisibility() { - this.setState( { - isSettingsExpanded: ! this.state.isSettingsExpanded, - } ); - } - - render() { - const { - additionalControls, - children, - renderSettings, - position = 'bottom center', - focusOnMount = 'firstElement', - ...popoverProps - } = this.props; - - const { isSettingsExpanded } = this.state; - - const showSettings = !! renderSettings && isSettingsExpanded; - - return ( - -
-
- { children } - { !! renderSettings && ( -
- { showSettings && ( -
- { renderSettings() } -
+ return ( + +
+
+ { children } + { !! renderSettings && ( +
- { additionalControls && ! showSettings && ( -
- { additionalControls } + { showSettings && ( +
+ { renderSettings() }
) } - - ); - } +
+ { additionalControls && ! showSettings && ( +
+ { additionalControls } +
+ ) } + + ); } URLPopover.LinkEditor = LinkEditor; diff --git a/packages/block-editor/src/components/url-popover/stories/index.js b/packages/block-editor/src/components/url-popover/stories/index.js new file mode 100644 index 0000000000000..cd07f94ec4bbb --- /dev/null +++ b/packages/block-editor/src/components/url-popover/stories/index.js @@ -0,0 +1,52 @@ +/** + * WordPress dependencies + */ +import { Button, ToggleControl } from '@wordpress/components'; +import { useState } from '@wordpress/element'; +import { __ } from '@wordpress/i18n'; +import { keyboardReturn } from '@wordpress/icons'; + +/** + * Internal dependencies + */ +import URLPopover from '../'; + +export default { title: 'BlockEditor/URLPopover' }; + +const TestURLPopover = () => { + const [ isVisible, setVisiblility ] = useState( false ); + const [ url, setUrl ] = useState( '' ); + + const close = () => setVisiblility( false ); + const setTarget = () => {}; + + return ( + <> + + { isVisible && ( + ( + + ) } + > +
+ +