[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 <build@mattermost.com>
Этот коммит содержится в:
@@ -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<Props> {
|
||||
minRef: RefObject<SettingItemMinComponent>;
|
||||
const SettingItem = ({
|
||||
active,
|
||||
areAllSectionsInactive,
|
||||
section,
|
||||
max,
|
||||
updateSection,
|
||||
title,
|
||||
isDisabled,
|
||||
describe,
|
||||
collapsedEditButtonWhenDisabled,
|
||||
}: Props) => {
|
||||
const minRef = useRef<SettingItemMinComponent>(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 (
|
||||
<SettingItemMin
|
||||
ref={minRef}
|
||||
title={title}
|
||||
updateSection={updateSection}
|
||||
describe={describe}
|
||||
section={section}
|
||||
isDisabled={isDisabled}
|
||||
collapsedEditButtonWhenDisabled={collapsedEditButtonWhenDisabled}
|
||||
/>
|
||||
);
|
||||
};
|
||||
|
||||
return (
|
||||
<SettingItemMin
|
||||
ref={this.minRef}
|
||||
title={this.props.title}
|
||||
updateSection={this.props.updateSection}
|
||||
describe={this.props.describe}
|
||||
section={this.props.section}
|
||||
isDisabled={this.props.isDisabled}
|
||||
collapsedEditButtonWhenDisabled={this.props.collapsedEditButtonWhenDisabled}
|
||||
/>
|
||||
);
|
||||
}
|
||||
}
|
||||
export default React.memo(SettingItem);
|
||||
|
||||
Разница между файлами не показана из-за своего большого размера
Загрузить разницу
@@ -26,7 +26,7 @@ exports[`components/user_settings/display/UserSettingsDisplay should match snaps
|
||||
<div
|
||||
className="divider-dark first"
|
||||
/>
|
||||
<SettingItem
|
||||
<Memo(SettingItem)
|
||||
active={false}
|
||||
areAllSectionsInactive={false}
|
||||
max={null}
|
||||
@@ -67,7 +67,7 @@ exports[`components/user_settings/display/UserSettingsDisplay should match snaps
|
||||
<div
|
||||
className="divider-light"
|
||||
/>
|
||||
<SettingItem
|
||||
<Memo(SettingItem)
|
||||
active={false}
|
||||
areAllSectionsInactive={false}
|
||||
describe={
|
||||
@@ -163,7 +163,7 @@ exports[`components/user_settings/display/UserSettingsDisplay should match snaps
|
||||
<div
|
||||
className="divider-dark first"
|
||||
/>
|
||||
<SettingItem
|
||||
<Memo(SettingItem)
|
||||
active={false}
|
||||
areAllSectionsInactive={false}
|
||||
max={null}
|
||||
@@ -204,7 +204,7 @@ exports[`components/user_settings/display/UserSettingsDisplay should match snaps
|
||||
<div
|
||||
className="divider-light"
|
||||
/>
|
||||
<SettingItem
|
||||
<Memo(SettingItem)
|
||||
active={false}
|
||||
areAllSectionsInactive={false}
|
||||
describe={
|
||||
@@ -300,7 +300,7 @@ exports[`components/user_settings/display/UserSettingsDisplay should match snaps
|
||||
<div
|
||||
className="divider-dark first"
|
||||
/>
|
||||
<SettingItem
|
||||
<Memo(SettingItem)
|
||||
active={false}
|
||||
areAllSectionsInactive={false}
|
||||
max={null}
|
||||
@@ -341,7 +341,7 @@ exports[`components/user_settings/display/UserSettingsDisplay should match snaps
|
||||
<div
|
||||
className="divider-light"
|
||||
/>
|
||||
<SettingItem
|
||||
<Memo(SettingItem)
|
||||
active={false}
|
||||
areAllSectionsInactive={false}
|
||||
describe={
|
||||
@@ -437,7 +437,7 @@ exports[`components/user_settings/display/UserSettingsDisplay should match snaps
|
||||
<div
|
||||
className="divider-dark first"
|
||||
/>
|
||||
<SettingItem
|
||||
<Memo(SettingItem)
|
||||
active={false}
|
||||
areAllSectionsInactive={false}
|
||||
max={null}
|
||||
@@ -478,7 +478,7 @@ exports[`components/user_settings/display/UserSettingsDisplay should match snaps
|
||||
<div
|
||||
className="divider-light"
|
||||
/>
|
||||
<SettingItem
|
||||
<Memo(SettingItem)
|
||||
active={false}
|
||||
areAllSectionsInactive={false}
|
||||
describe={
|
||||
@@ -574,7 +574,7 @@ exports[`components/user_settings/display/UserSettingsDisplay should match snaps
|
||||
<div
|
||||
className="divider-dark first"
|
||||
/>
|
||||
<SettingItem
|
||||
<Memo(SettingItem)
|
||||
active={false}
|
||||
areAllSectionsInactive={false}
|
||||
max={null}
|
||||
@@ -616,7 +616,7 @@ exports[`components/user_settings/display/UserSettingsDisplay should match snaps
|
||||
<div
|
||||
className="divider-light"
|
||||
/>
|
||||
<SettingItem
|
||||
<Memo(SettingItem)
|
||||
active={false}
|
||||
areAllSectionsInactive={false}
|
||||
describe={
|
||||
|
||||
Ссылка в новой задаче
Block a user