From 6f4d362f9f3eb1e0b0ab4617b3f7a04d6fe015ff Mon Sep 17 00:00:00 2001 From: Tanmay Vardhaman Thole <72058456+tanmaythole@users.noreply.github.com> Date: Thu, 19 Oct 2023 16:28:36 +0530 Subject: [PATCH] migrate DropdownSetting component from class based to function based component (#24796) * migrate DropdownSetting component to function comp * code optimized * snapshot updated * snapshot updated --------- Co-authored-by: Mattermost Build --- .../message_export_settings.test.jsx.snap | 12 +-- .../__snapshots__/push_settings.test.tsx.snap | 9 +- .../schema_admin_settings.test.jsx.snap | 6 +- .../custom_plugin_settings.test.tsx.snap | 6 +- .../admin_console/dropdown_setting.tsx | 99 +++++++++---------- 5 files changed, 56 insertions(+), 76 deletions(-) diff --git a/webapp/channels/src/components/admin_console/__snapshots__/message_export_settings.test.jsx.snap b/webapp/channels/src/components/admin_console/__snapshots__/message_export_settings.test.jsx.snap index 3e5cbacbb9..e44cb8abdf 100644 --- a/webapp/channels/src/components/admin_console/__snapshots__/message_export_settings.test.jsx.snap +++ b/webapp/channels/src/components/admin_console/__snapshots__/message_export_settings.test.jsx.snap @@ -72,7 +72,7 @@ exports[`components/MessageExportSettings should match snapshot, disabled, actia setByEnv={false} value="01:00" /> - } id="exportFormat" - isDisabled={false} label={ - } id="exportFormat" - isDisabled={false} label={ - } id="exportFormat" - isDisabled={false} label={ - } id="exportFormat" - isDisabled={false} label={ - - - - } id="FirstSettings.settingc" - isDisabled={false} key="Config_dropdown_FirstSettings.settingc" label="Setting Three" onChange={[Function]} @@ -269,7 +268,7 @@ exports[`components/admin_console/SchemaAdminSettings should match snapshot with - } id="SecondSettings.settingi" - isDisabled={false} key="Config_language_SecondSettings.settingi" label="Setting Nine" onChange={[Function]} diff --git a/webapp/channels/src/components/admin_console/custom_plugin_settings/__snapshots__/custom_plugin_settings.test.tsx.snap b/webapp/channels/src/components/admin_console/custom_plugin_settings/__snapshots__/custom_plugin_settings.test.tsx.snap index efb9159ddc..bfec48a54c 100644 --- a/webapp/channels/src/components/admin_console/custom_plugin_settings/__snapshots__/custom_plugin_settings.test.tsx.snap +++ b/webapp/channels/src/components/admin_console/custom_plugin_settings/__snapshots__/custom_plugin_settings.test.tsx.snap @@ -76,7 +76,7 @@ exports[`components/admin_console/CustomPluginSettings should match snapshot wit } value={true} /> - } id="settingc" - isDisabled={false} key="testplugin_dropdown_settingc" label="Setting Three" onChange={[Function]} @@ -387,7 +386,7 @@ exports[`components/admin_console/CustomPluginSettings should match snapshot wit } value={false} /> - } id="PluginSettings.Plugins.testplugin.settingc" - isDisabled={false} key="testplugin_dropdown_PluginSettings.Plugins.testplugin.settingc" label="Setting Three" onChange={[Function]} diff --git a/webapp/channels/src/components/admin_console/dropdown_setting.tsx b/webapp/channels/src/components/admin_console/dropdown_setting.tsx index ada42e9b75..0728af1adb 100644 --- a/webapp/channels/src/components/admin_console/dropdown_setting.tsx +++ b/webapp/channels/src/components/admin_console/dropdown_setting.tsx @@ -1,8 +1,7 @@ // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. // See LICENSE.txt for license information. -import PropTypes from 'prop-types'; -import React, {PureComponent} from 'react'; +import React, {memo, useCallback, useMemo} from 'react'; import type {ReactNode, ChangeEvent} from 'react'; import type {EmailSettings} from '@mattermost/types/config'; @@ -20,57 +19,49 @@ type Props = { helpText?: ReactNode; } -export default class DropdownSetting extends PureComponent { - static propTypes = { - id: PropTypes.string.isRequired, - values: PropTypes.array.isRequired, - label: PropTypes.node.isRequired, - value: PropTypes.string.isRequired, - onChange: PropTypes.func.isRequired, - disabled: PropTypes.bool, - setByEnv: PropTypes.bool.isRequired, - helpText: PropTypes.node, - }; +const DropdownSetting = ({ + id, + values, + label, + value, + onChange, + disabled = false, + setByEnv, + helpText, +}: Props) => { + const handleChange = useCallback((e: ChangeEvent) => { + onChange(id, e.target.value); + }, [onChange, id]); - static defaultProps = { - isDisabled: false, - }; - - handleChange = (e: ChangeEvent) => { - this.props.onChange(this.props.id, e.target.value); - }; - - render() { - const options = []; - for (const {value, text} of this.props.values) { - options.push( - , - ); - } - - return ( - + values.map(({value: val, text}) => ( + - ); - } -} + {text} + + )), [values]); + + return ( + + + + ); +}; + +export default memo(DropdownSetting);