From c582f497391611715761530f8e0b99f0bf41e07f Mon Sep 17 00:00:00 2001 From: Rita Anene <92169163+Camillarhi@users.noreply.github.com> Date: Fri, 9 Aug 2024 08:59:27 +0100 Subject: [PATCH] [MM-57716] Convert ./components/setting_item.tsx from Class Component to Function Component (#27615) * refactor: convert setting_item from class to functional component * fix: removed type casting 'FC' * fix: fixed failing tests * fix: made 'max' prop nullable * fix: revert snapshot file to master * fix: Update the `useEffect` dependency list to trigger only on `active` prop changes --------- Co-authored-by: Mattermost Build --- .../channels/src/components/setting_item.tsx | 69 ++-- .../user_settings_display.test.tsx.snap | 332 +++++++++--------- .../user_settings_security.test.tsx.snap | 20 +- 3 files changed, 213 insertions(+), 208 deletions(-) diff --git a/webapp/channels/src/components/setting_item.tsx b/webapp/channels/src/components/setting_item.tsx index 11bf69c480..4bc2bdeb6e 100644 --- a/webapp/channels/src/components/setting_item.tsx +++ b/webapp/channels/src/components/setting_item.tsx @@ -1,11 +1,11 @@ // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. // See LICENSE.txt for license information. -import React from 'react'; -import type {ReactNode, RefObject} from 'react'; +import type {ReactNode} from 'react'; +import React, {useEffect, useRef} from 'react'; -import SettingItemMin from 'components/setting_item_min'; import type SettingItemMinComponent from 'components/setting_item_min'; +import SettingItemMin from 'components/setting_item_min'; type Props = { @@ -27,7 +27,7 @@ type Props = { /** * The setting UI when it is maximized (open) */ - max: ReactNode; + max?: ReactNode; // Props to pass through for SettingItemMin updateSection: (section: string) => void; @@ -41,37 +41,42 @@ type Props = { collapsedEditButtonWhenDisabled?: ReactNode; } -export default class SettingItem extends React.PureComponent { - minRef: RefObject; +const SettingItem = ({ + active, + areAllSectionsInactive, + section, + max, + updateSection, + title, + isDisabled, + describe, + collapsedEditButtonWhenDisabled, +}: Props) => { + const minRef = useRef(null); - constructor(props: Props) { - super(props); - - this.minRef = React.createRef(); - } - - componentDidUpdate(prevProps: Props) { + useEffect(() => { // We want to bring back focus to the edit button when the section is opened and then closed along with all sections are closed - if (!this.props.active && prevProps.active && this.props.areAllSectionsInactive) { - this.minRef.current?.focus(); + + if (!active && areAllSectionsInactive) { + minRef.current?.focus(); } + }, [active]); + + if (active) { + return <>{max}; } - render() { - if (this.props.active) { - return this.props.max; - } + return ( + + ); +}; - return ( - - ); - } -} +export default React.memo(SettingItem); diff --git a/webapp/channels/src/components/user_settings/display/__snapshots__/user_settings_display.test.tsx.snap b/webapp/channels/src/components/user_settings/display/__snapshots__/user_settings_display.test.tsx.snap index b194d5f2ec..5a1d7c9f7e 100644 --- a/webapp/channels/src/components/user_settings/display/__snapshots__/user_settings_display.test.tsx.snap +++ b/webapp/channels/src/components/user_settings/display/__snapshots__/user_settings_display.test.tsx.snap @@ -30,7 +30,7 @@ exports[`components/user_settings/display/UserSettingsDisplay should match snaps className="divider-dark first" />
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- - - - - - - - - - -