Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactored SelectMetadataField from class based component to functional Component #6200

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/client/news/6200.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Refactor the `SelectMetadataField` component from a class based component to a functional component. @codic-yeeshu
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
* @module components/manage/Widgets/SelectWidget
*/

import { map, intersection, filter, toPairs, groupBy } from 'lodash';
import React, { Component } from 'react';
import { map, filter, toPairs, groupBy } from 'lodash';
import React, { useEffect } from 'react';
import PropTypes from 'prop-types';
import { injectLazyLibs } from '@plone/volto/helpers/Loadable/Loadable';
import { compose } from 'redux';
Expand All @@ -29,188 +29,114 @@ const messages = defineMessages({
},
});

/**
* SelectWidget component class.
* @function SelectWidget
* @returns {string} Markup of the component.
*/
class SelectWidget extends Component {
/**
* Property types.
* @property {Object} propTypes Property types.
* @static
*/
static propTypes = {
id: PropTypes.string.isRequired,
title: PropTypes.string.isRequired,
description: PropTypes.string,
required: PropTypes.bool,
error: PropTypes.arrayOf(PropTypes.string),
loading: PropTypes.bool,
value: PropTypes.oneOfType([
PropTypes.object,
PropTypes.string,
PropTypes.bool,
]),
onChange: PropTypes.func.isRequired,
onBlur: PropTypes.func,
onClick: PropTypes.func,
onEdit: PropTypes.func,
onDelete: PropTypes.func,
wrapped: PropTypes.bool,
querystring: PropTypes.object,
};

/**
* Default properties
* @property {Object} defaultProps Default properties.
* @static
*/
static defaultProps = {
description: null,
required: false,
items: {
vocabulary: null,
},
widgetOptions: {
vocabulary: null,
},
error: [],
choices: [],
loading: false,
value: null,
onChange: () => {},
onBlur: () => {},
onClick: () => {},
onEdit: null,
onDelete: null,
};

state = {
selectedOption: this.props.value
? { label: this.props.value.title, value: this.props.value.value }
: {},
};

/**
* Component did mount
* @method componentDidMount
* @returns {undefined}
*/
componentDidMount() {
if (!this.props.choices && this.props.vocabBaseUrl) {
this.props.getVocabulary({ vocabNameOrURL: this.props.vocabBaseUrl });
}
}

/**
* Initiate search with new query
* @method loadOptions
* @param {string} search Search query.
* @param {string} previousOptions The previous options rendered.
* @param {string} additional Additional arguments to pass to the next loadOptions.
* @returns {undefined}
*/
loadOptions = (search, previousOptions, additional) => {
let hasMore = this.props.itemsTotal > previousOptions.length;
if (hasMore) {
const offset = this.state.search !== search ? 0 : additional.offset;
this.props.getVocabulary({
vocabNameOrURL: this.props.vocabBaseUrl,
query: search,
start: offset,
});
this.setState({ search });

return {
options:
intersection(previousOptions, this.props.choices).length ===
this.props.choices.length
? []
: this.props.choices,
hasMore: hasMore,
additional: {
offset: offset === additional.offset ? offset + 25 : offset,
},
};
const SelectWidget = (props) => {
const {
id,
value,
onChange,
placeholder,
querystring,
filterOptions = identity,
vocabBaseUrl,
getVocabulary,
choices,
intl,
} = props;

useEffect(() => {
if (!choices && vocabBaseUrl) {
getVocabulary({ vocabNameOrURL: vocabBaseUrl });
}
return null;
};

/* Customized to pass object instead of plain string value */
handleChange = (selectedOption) => {
this.setState({ selectedOption });
this.props.onChange(this.props.id, {
value: selectedOption.value,
title: selectedOption.label,
});
};

/**
* Render method.
* @method render
* @returns {string} Markup for the component.
*/
render() {
const {
id,
// choices,
value,
onChange,
placeholder,
querystring,
filterOptions = identity,
} = this.props;
const isDisabled = false;
const { indexes = [] } = querystring;

const Select = this.props.reactSelect.default;

return (
<FormFieldWrapper {...this.props}>
<Select
id={`field-${id}`}
name={id}
placeholder={
placeholder ?? this.props.intl.formatMessage(messages.select)
}
isDisabled={isDisabled}
className="react-select-container"
classNamePrefix="react-select"
options={map(
toPairs(
groupBy(toPairs(filterOptions(indexes)), (item) => item[1].group),
}, [choices, vocabBaseUrl, getVocabulary]);

const { indexes = [] } = querystring;
const isDisabled = false;

const Select = props.reactSelect.default;

return (
<FormFieldWrapper {...props}>
<Select
id={`field-${id}`}
name={id}
placeholder={placeholder ?? intl.formatMessage(messages.select)}
isDisabled={isDisabled}
className="react-select-container"
classNamePrefix="react-select"
options={map(
toPairs(
groupBy(toPairs(filterOptions(indexes)), (item) => item[1].group),
),
(group) => ({
label: group[0],
options: map(
filter(group[1], (item) => item[1].enabled),
(field) => ({
label: field[1].title,
value: field[0],
}),
),
(group) => ({
label: group[0],
options: map(
filter(group[1], (item) => item[1].enabled),
(field) => ({
label: field[1].title,
value: field[0],
}),
),
}),
)}
styles={customSelectStyles}
theme={selectTheme}
components={{ DropdownIndicator, Option }}
value={{ value: value?.value, label: indexes[value?.value]?.title }}
onChange={(data) => {
let dataValue = [];
if (Array.isArray(data)) {
for (let obj of data) {
dataValue.push(obj.value);
}
return onChange(id, dataValue);
}),
)}
styles={customSelectStyles}
theme={selectTheme}
components={{ DropdownIndicator, Option }}
value={{ value: value?.value, label: indexes[value?.value]?.title }}
onChange={(data) => {
let dataValue = [];
if (Array.isArray(data)) {
for (let obj of data) {
dataValue.push(obj.value);
}
return onChange(id, data);
}}
/>
</FormFieldWrapper>
);
}
}
return onChange(id, dataValue);
}
return onChange(id, data);
}}
/>
</FormFieldWrapper>
);
};

SelectWidget.propTypes = {
id: PropTypes.string.isRequired,
title: PropTypes.string.isRequired,
description: PropTypes.string,
required: PropTypes.bool,
error: PropTypes.arrayOf(PropTypes.string),
loading: PropTypes.bool,
value: PropTypes.oneOfType([
PropTypes.object,
PropTypes.string,
PropTypes.bool,
]),
onChange: PropTypes.func.isRequired,
onBlur: PropTypes.func,
onClick: PropTypes.func,
onEdit: PropTypes.func,
onDelete: PropTypes.func,
wrapped: PropTypes.bool,
querystring: PropTypes.object,
};

SelectWidget.defaultProps = {
description: null,
required: false,
items: {
vocabulary: null,
},
widgetOptions: {
vocabulary: null,
},
error: [],
choices: [],
loading: false,
value: null,
onChange: () => {},
onBlur: () => {},
onClick: () => {},
onEdit: null,
onDelete: null,
};

export default compose(
withQueryString,
Expand Down
Loading