Skip to content

Commit

Permalink
Attempt (WIP) to solve the issue with a new widget
Browse files Browse the repository at this point in the history
  • Loading branch information
silviubogan committed Oct 9, 2020
1 parent abf5158 commit 9f633d6
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 4 deletions.
9 changes: 8 additions & 1 deletion src/ColumnsBlock/ColumnsBlockEdit.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -138,14 +138,21 @@ class ColumnsBlockEdit extends React.Component {
componentDidUpdate(prevProps) {
const cols = this.props.data.data?.blocks_layout?.items || [];
const prevCols = prevProps.data.data?.blocks_layout?.items || [];
if (cols.length !== prevCols.length) {

console.log('val', this.props.data.gridCols);
if (
cols.length !==
prevCols.length /* ||
this.props.data.gridCols?.length === 0 */
) {
const available_variants = variants.filter(
({ defaultData }) => defaultData?.gridCols?.length === cols.length,
);
const variant = available_variants?.[0];
if (variant) {
this.props.onChangeBlock(this.props.block, {
...this.props.data,
gridSize: variant.defaultData.gridSize,
gridCols: variant.defaultData.gridCols,
});
}
Expand Down
1 change: 1 addition & 0 deletions src/ColumnsBlock/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export const ColumnsBlockSchema = () => ({
},
gridCols: {
title: 'Layout',
widget: 'layout_select',
choices: [],
},
},
Expand Down
88 changes: 88 additions & 0 deletions src/Widgets/LayoutSelectWidget.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/**
* LayoutSelectWidget component.
* @module volto-columns-block/Widgets/LayoutSelectWidget
*/

import React, { Component } from 'react';
import { compose } from 'redux';
import { defineMessages, injectIntl } from 'react-intl';
import loadable from '@loadable/component';

import { FormFieldWrapper } from '@plone/volto/components';

import {
Option,
DropdownIndicator,
selectTheme,
customSelectStyles,
} from '@plone/volto/components/manage/Widgets/SelectStyling';

const Select = loadable(() => import('react-select'));

/**
* SelectWidget component class.
* @function SelectWidget
* @returns {string} Markup of the component.
*/
export class LayoutSelectWidget extends Component {
state = {
selectedOption: null,
};

componentDidMount() {
this.setState({
selectedOption: {
value: this.props.value,
label: this.props.choices.find((x) => x[0] === this.props.value)?.[1],
},
});
}

componentDidUpdate(prevProps) {
if (prevProps.value !== this.props.value) {
this.setState({
selectedOption: {
value: this.props.value,
label: this.props.choices.find((x) => x[0] === this.props.value)?.[1],
},
});
}
}

/**
* Render method.
* @method render
* @returns {string} Markup for the component.
*/
render() {
const { id, choices, value, onChange } = this.props;

console.log('value', value);

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 }}
onChange={(data) => {
this.setState({ selectedOption: data });
return onChange?.(id, data.value);
}}
/>
</FormFieldWrapper>
);
}
}

export default compose(injectIntl)(LayoutSelectWidget);
4 changes: 2 additions & 2 deletions src/Widgets/index.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
import ColumnsWidget from './ColumnsWidget';
export default ColumnsWidget;
export ColumnsWidget from './ColumnsWidget';
export LayoutSelectWidget from './LayoutSelectWidget';
3 changes: 2 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import columnSVG from './ColumnsBlock/icons/three-columns.svg';

import { ColumnsBlockView, ColumnsBlockEdit } from './ColumnsBlock';
import ColumnsWidget from './Widgets/ColumnsWidget';
import { ColumnsWidget, LayoutSelectWidget } from './Widgets';
import ColorPickerWidget from './Widgets/SimpleColorPickerWidget.jsx';
import { gridSizes, variants } from './grid';
import { COLUMNSBLOCK } from './constants';
Expand Down Expand Up @@ -47,6 +47,7 @@ export default function install(config) {

config.widgets.type.columns = ColumnsWidget;
config.widgets.widget.simple_color_picker = ColorPickerWidget;
config.widgets.widget.layout_select = LayoutSelectWidget;

return config;
}

0 comments on commit 9f633d6

Please sign in to comment.