[MM-60074] Convert ./components/admin_console/multiselect_settings.tsx from Class Component to Function Component (#27908)

* feat: convert multiselect_settings to functional component

* fix: comments addressed and linting done

* fix: failing test

---------

Co-authored-by: Mattermost Build <build@mattermost.com>
Этот коммит содержится в:
Michael
2024-08-23 12:39:51 +01:00
коммит произвёл GitHub
родитель 16c85c41aa
Коммит 56961b3b49
2 изменённых файлов: 58 добавлений и 56 удалений

Просмотреть файл

@@ -424,7 +424,7 @@ exports[`components/admin_console/SchemaAdminSettings should match snapshot with
] ]
} }
/> />
<MultiSelectSetting <Memo(MultiSelectSetting)
disabled={false} disabled={false}
helpText={ helpText={
<SchemaText <SchemaText

Просмотреть файл

@@ -1,9 +1,9 @@
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information. // See LICENSE.txt for license information.
import React from 'react'; import React, {useState, useCallback, useMemo} from 'react';
import ReactSelect from 'react-select';
import type {ValueType} from 'react-select'; import type {ValueType} from 'react-select';
import ReactSelect from 'react-select';
import FormError from 'components/form_error'; import FormError from 'components/form_error';
@@ -26,67 +26,69 @@ interface Props {
noResultText?: React.ReactNode; noResultText?: React.ReactNode;
} }
interface State { const getOptionLabel = ({text}: { text: string}) => text;
error: boolean;
}
export default class MultiSelectSetting extends React.PureComponent< const MultiSelectSetting: React.FC<Props> = ({
Props, id,
State values,
> { label,
static defaultProps: Partial<Props> = { selected,
disabled: false, onChange,
}; disabled = false,
setByEnv,
helpText,
noResultText,
}) => {
const [error, setError] = useState(false);
constructor(props: Props) { const handleChange = useCallback((newValue: ValueType<Option>) => {
super(props); const updatedValues = newValue ? (newValue as Option[]).map((n) => {
this.state = {error: false};
}
handleChange = (newValue: ValueType<Option>) => {
const values = newValue ? (newValue as Option[]).map((n) => {
return n.value; return n.value;
}) : []; }) : [];
this.props.onChange(this.props.id, values); onChange(id, updatedValues);
this.setState({error: false}); setError(false);
}; }, [id, onChange]);
calculateValue = () => { const valuesMap = useMemo(() => {
return this.props.selected.reduce<Option[]>((values, item) => { return values.reduce((map, v) => {
const found = this.props.values.find((e) => e.value === item); map[v.value] = v;
return map;
}, {} as Record<string, Option>);
}, [values]);
const calculatedValue = useMemo(() => {
return selected.reduce<Option[]>((result, item) => {
const found = valuesMap[item];
if (found) { if (found) {
values.push(found); result.push(found);
} }
return values; return result;
}, []); }, []);
}; }, [selected, valuesMap]);
getOptionLabel = ({text}: { text: string}) => text;
render() {
return ( return (
<Setting <Setting
label={this.props.label} label={label}
inputId={this.props.id} inputId={id}
helpText={this.props.helpText} helpText={helpText}
setByEnv={this.props.setByEnv} setByEnv={setByEnv}
> >
<ReactSelect <ReactSelect
id={this.props.id} id={id}
isMulti={true} isMulti={true}
getOptionLabel={this.getOptionLabel} getOptionLabel={getOptionLabel}
options={this.props.values} options={values}
delimiter={','} delimiter={','}
clearable={false} clearable={false}
isDisabled={this.props.disabled || this.props.setByEnv} isDisabled={disabled || setByEnv}
noResultsText={this.props.noResultText} noResultsText={noResultText}
onChange={this.handleChange} onChange={handleChange}
value={this.calculateValue()} value={calculatedValue}
/> />
<FormError error={this.state.error}/> <FormError error={error}/>
</Setting> </Setting>
); );
} };
}
export default React.memo(MultiSelectSetting);