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

Optimize bundle (extract react-select into separate chunk) #8

Merged
merged 2 commits into from
Oct 22, 2020
Merged
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
84 changes: 50 additions & 34 deletions src/Widgets/LayoutSelectWidget.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
import React, { Component } from 'react';
import { compose } from 'redux';
import { defineMessages, injectIntl } from 'react-intl';
import Select, { components } from 'react-select';

import loadable from '@loadable/component';

import { isEqual } from 'lodash';
import { variants } from '../grid';
import { FormFieldWrapper, Icon } from '@plone/volto/components';
Expand All @@ -17,6 +19,8 @@ import {
} from '@plone/volto/components/manage/Widgets/SelectStyling';
import checkSVG from '@plone/volto/icons/check.svg';

const ReactSelectLib = loadable.lib(() => import('react-select'));

const gridColsAreEqual = (gridCols1, gridCols2) => {
return isEqual(gridCols1, gridCols2);
};
Expand All @@ -27,21 +31,27 @@ const variantToGridCols = (v) => {

export const Option = (props) => {
return (
<components.Option {...props}>
<Icon
name={
variants.find((v) =>
gridColsAreEqual(variantToGridCols(v), props.value),
).icon
}
size="24px"
/>
<div>{props.label}</div>
{props.isFocused && !props.isSelected && (
<Icon name={checkSVG} size="24px" color="#b8c6c8" />
)}
{props.isSelected && <Icon name={checkSVG} size="24px" color="#007bc1" />}
</components.Option>
<ReactSelectLib>
{({ components }) => {
return (
<components.Option {...props}>
<Icon
name={
variants.find((v) =>
gridColsAreEqual(variantToGridCols(v), props.value),
).icon
}
size="24px"
/>
<div>{props.label}</div>
{props.isFocused && !props.isSelected && (
<Icon name={checkSVG} size="24px" color="#b8c6c8" />
)}
{props.isSelected && <Icon name={checkSVG} size="24px" color="#007bc1" />}
</components.Option>
);
}}
</ReactSelectLib>
);
};

Expand Down Expand Up @@ -119,25 +129,31 @@ export class LayoutSelectWidget extends Component {

return (
<FormFieldWrapper {...this.props}>
<Select
id={`field-${id}`}
key={this.props.choices}
name={id}
value={this.state.selectedOption}
isDisabled={this.props.isDisabled}
className="react-select-container"
classNamePrefix="react-select"
options={choices.map((x) => {
return { value: x[0], label: x[1] };
})}
styles={customSelectStyles}
theme={selectTheme}
components={{ DropdownIndicator, Option, SingleValue }}
onChange={(data) => {
this.setState({ selectedOption: data });
return onChange?.(id, data.value);
<ReactSelectLib>
{({ default: Select }) => {
return (
<Select
id={`field-${id}`}
key={this.props.choices}
name={id}
value={this.state.selectedOption}
isDisabled={this.props.isDisabled}
className="react-select-container"
classNamePrefix="react-select"
options={choices.map((x) => {
return { value: x[0], label: x[1] };
})}
styles={customSelectStyles}
theme={selectTheme}
components={{ DropdownIndicator, Option, SingleValue }}
onChange={(data) => {
this.setState({ selectedOption: data });
return onChange?.(id, data.value);
}}
/>
);
}}
/>
</ReactSelectLib>
</FormFieldWrapper>
);
}
Expand Down