[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>
Этот коммит содержится в:
@@ -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;
|
return (
|
||||||
|
<Setting
|
||||||
|
label={label}
|
||||||
|
inputId={id}
|
||||||
|
helpText={helpText}
|
||||||
|
setByEnv={setByEnv}
|
||||||
|
>
|
||||||
|
<ReactSelect
|
||||||
|
id={id}
|
||||||
|
isMulti={true}
|
||||||
|
getOptionLabel={getOptionLabel}
|
||||||
|
options={values}
|
||||||
|
delimiter={','}
|
||||||
|
clearable={false}
|
||||||
|
isDisabled={disabled || setByEnv}
|
||||||
|
noResultsText={noResultText}
|
||||||
|
onChange={handleChange}
|
||||||
|
value={calculatedValue}
|
||||||
|
/>
|
||||||
|
<FormError error={error}/>
|
||||||
|
</Setting>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
render() {
|
export default React.memo(MultiSelectSetting);
|
||||||
return (
|
|
||||||
<Setting
|
|
||||||
label={this.props.label}
|
|
||||||
inputId={this.props.id}
|
|
||||||
helpText={this.props.helpText}
|
|
||||||
setByEnv={this.props.setByEnv}
|
|
||||||
>
|
|
||||||
<ReactSelect
|
|
||||||
id={this.props.id}
|
|
||||||
isMulti={true}
|
|
||||||
getOptionLabel={this.getOptionLabel}
|
|
||||||
options={this.props.values}
|
|
||||||
delimiter={','}
|
|
||||||
clearable={false}
|
|
||||||
isDisabled={this.props.disabled || this.props.setByEnv}
|
|
||||||
noResultsText={this.props.noResultText}
|
|
||||||
onChange={this.handleChange}
|
|
||||||
value={this.calculateValue()}
|
|
||||||
/>
|
|
||||||
<FormError error={this.state.error}/>
|
|
||||||
</Setting>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
Ссылка в новой задаче
Block a user