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

FEATURE: Improve user experience for Dimension Switcher #3451

Merged
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ test('Switching dimensions', async t => {
const otherPageName = 'Untranslated page';

await Page.goToPage(translatedPageName);
await DimensionSwitcher.switchLanguageDimension('Latvian');
await DimensionSwitcher.switchSingleDimension('Latvian');
await t.click('#neos-NodeVariantCreationDialog-CreateEmpty');
await Page.waitForIframeLoading();
await t
.expect(await Page.getReduxState(state => state.cr.contentDimensions.active.language[0])).eql('lv', 'Dimension switched to Latvian')
.expect(Page.treeNode.withText(otherPageName).exists).notOk('Untranslated node gone from the tree');

subSection('Switch back to original dimension');
await DimensionSwitcher.switchLanguageDimension('English (US)');
await DimensionSwitcher.switchSingleDimension('English (US)');
await Page.waitForIframeLoading();
await t
.expect(await Page.getReduxState(state => state.cr.contentDimensions.active.language[0])).eql('en_US', 'Dimension back to English')
Expand Down
6 changes: 6 additions & 0 deletions Tests/IntegrationTests/pageModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ export class DimensionSwitcher {
.click(this.dimensionSwitcherFirstDimensionSelector)
.click(this.dimensionSwitcherFirstDimensionSelectorWithShallowDropDownContents.find('li').withText(name));
}

static async switchSingleDimension(name) {
await t
.click(this.dimensionSwitcher)
.click(ReactSelector('DimensionSelectorOption').withProps('option', {label: name}));
}
}

export class PublishDropDown {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import React, {PureComponent} from 'react';
import PropTypes from 'prop-types';
import SelectBox from '@neos-project/react-ui-components/src/SelectBox/';
import style from './style.module.css';
import {$get, $transform} from 'plow-js';
import mapValues from 'lodash.mapvalues';
import sortBy from 'lodash.sortby';
import {neos} from '@neos-project/neos-ui-decorators';
import DimensionSelectorOption from './DimensionSelectorOption';

const searchOptions = (searchTerm, processedSelectBoxOptions) =>
processedSelectBoxOptions.filter(option => option.label && option.label.toLowerCase().indexOf(searchTerm.toLowerCase()) !== -1);

@neos(globalRegistry => ({
i18nRegistry: globalRegistry.get('i18n')
}))
export default class DimensionSelector extends PureComponent {
static propTypes = {
icon: PropTypes.string.isRequired,
dimensionLabel: PropTypes.string.isRequired,
presets: PropTypes.object.isRequired,
activePreset: PropTypes.string.isRequired,
dimensionName: PropTypes.string.isRequired,
isLoading: PropTypes.bool,
onSelect: PropTypes.func.isRequired,
showDropDownHeaderIcon: PropTypes.bool,

i18nRegistry: PropTypes.object.isRequired
};

state = {
searchTerm: ''
};

render() {
const {
activePreset,
isLoading,
i18nRegistry,
dimensionName,
onSelect,
presets,
showDropDownHeaderIcon
} = this.props;

const presetOptions = mapValues(
presets,
(presetConfiguration, presetName) => {
return $transform(
{
label: $get('label'),
value: presetName,
disallowed: $get('disallowed')
},
presetConfiguration
);
}
);

const sortedPresetOptions = sortBy(presetOptions, ['label']);

const onPresetSelect = presetName => {
onSelect(dimensionName, presetName);
};

return (
<SelectBox
displayLoadingIndicator={isLoading}
options={this.state.searchTerm ? searchOptions(this.state.searchTerm, sortedPresetOptions) : sortedPresetOptions}
onValueChange={onPresetSelect}
value={activePreset}
allowEmpty={false}
headerIcon={showDropDownHeaderIcon ? this.props.icon : null}
displaySearchBox={sortedPresetOptions.length >= 10}
searchOptions={searchOptions(this.state.searchTerm, sortedPresetOptions)}
onSearchTermChange={this.handleSearchTermChange}
noMatchesFoundLabel={i18nRegistry.translate('Neos.Neos:Main:noMatchesFound')}
searchBoxLeftToTypeLabel={i18nRegistry.translate('Neos.Neos:Main:searchBoxLeftToType')}
threshold={0}
ListPreviewElement={DimensionSelectorOption}
className={style.dimensionSwitcherDropDown}
/>
)
}

handleSearchTermChange = searchTerm => {
this.setState({searchTerm});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,14 @@ import {connect} from 'react-redux';
import Button from '@neos-project/react-ui-components/src/Button/';
import Icon from '@neos-project/react-ui-components/src/Icon/';
import DropDown from '@neos-project/react-ui-components/src/DropDown/';
import SelectBox from '@neos-project/react-ui-components/src/SelectBox/';
import style from './style.module.css';
import backend from '@neos-project/neos-ui-backend-connector';
import {$get, $transform} from 'plow-js';
import mapValues from 'lodash.mapvalues';
import {selectors, actions} from '@neos-project/neos-ui-redux-store';
import I18n from '@neos-project/neos-ui-i18n';
import sortBy from 'lodash.sortby';
import {neos} from '@neos-project/neos-ui-decorators';
import DimensionSelectorOption from './DimensionSelectorOption';
import DimensionSelector from './DimensionSelector';

// TODO Add title prop to Icon component
const SelectedPreset = props => {
Expand All @@ -32,82 +30,6 @@ SelectedPreset.propTypes = {
dimensionName: PropTypes.string.isRequired
};

const searchOptions = (searchTerm, processedSelectBoxOptions) =>
processedSelectBoxOptions.filter(option => option.label && option.label.toLowerCase().indexOf(searchTerm.toLowerCase()) !== -1);

@neos(globalRegistry => ({
i18nRegistry: globalRegistry.get('i18n')
}))
class DimensionSelector extends PureComponent {
static propTypes = {
icon: PropTypes.string.isRequired,
dimensionLabel: PropTypes.string.isRequired,
presets: PropTypes.object.isRequired,
activePreset: PropTypes.string.isRequired,
dimensionName: PropTypes.string.isRequired,
isLoading: PropTypes.bool,
onSelect: PropTypes.func.isRequired,

i18nRegistry: PropTypes.object.isRequired
};

state = {
searchTerm: ''
};

render() {
const {icon, dimensionLabel, presets, dimensionName, activePreset, onSelect, isLoading, i18nRegistry} = this.props;

const presetOptions = mapValues(
presets,
(presetConfiguration, presetName) => {
return $transform(
{
label: $get('label'),
value: presetName,
disallowed: $get('disallowed')
},
presetConfiguration
);
}
);

const sortedPresetOptions = sortBy(presetOptions, ['label']);

const onPresetSelect = presetName => {
onSelect(dimensionName, presetName);
};

return (
<li key={dimensionName} className={style.dimensionCategory}>
<div className={style.dimensionLabel}>
<Icon icon={icon} padded="right" className={style.dimensionCategory__icon}/>
<I18n id={dimensionLabel}/>
</div>
<SelectBox
displayLoadingIndicator={isLoading}
options={this.state.searchTerm ? searchOptions(this.state.searchTerm, sortedPresetOptions) : sortedPresetOptions}
onValueChange={onPresetSelect}
value={activePreset}
allowEmpty={false}
displaySearchBox={sortedPresetOptions.length >= 10}
searchOptions={searchOptions(this.state.searchTerm, sortedPresetOptions)}
onSearchTermChange={this.handleSearchTermChange}
noMatchesFoundLabel={i18nRegistry.translate('Neos.Neos:Main:noMatchesFound')}
searchBoxLeftToTypeLabel={i18nRegistry.translate('Neos.Neos:Main:searchBoxLeftToType')}
threshold={0}
ListPreviewElement={DimensionSelectorOption}
className={style.dimensionSwitcherDropDown}
/>
</li>
);
}

handleSearchTermChange = searchTerm => {
this.setState({searchTerm});
}
}

@connect($transform({
contentDimensions: selectors.CR.ContentDimensions.byName,
allowedPresets: selectors.CR.ContentDimensions.allowedPresets,
Expand Down Expand Up @@ -142,6 +64,11 @@ export default class DimensionSwitcher extends PureComponent {
loadingPresets: {}
};

getDimensionIcon = (dimensionName, contentDimensionsObject) => {
const dimensionConfiguration = contentDimensionsObject[dimensionName];
return dimensionConfiguration?.icon || null;
}

//
// Merge active presets comming from redux with local transientPresets state (i.e. presents selected, but not yet applied)
//
Expand Down Expand Up @@ -228,74 +155,104 @@ export default class DimensionSwitcher extends PureComponent {
this.setState({isOpen: false});
}

renderSingleDimensionSelector = (dimensionName, contentDimensionsObject) => {
const dimensionConfiguration = contentDimensionsObject[dimensionName];
const icon = this.getDimensionIcon(dimensionName, contentDimensionsObject);
// First look for active preset in transient state, else take it from activePresets prop
const activePreset = this.getEffectivePresets(this.state.transientPresets)[dimensionName];
return (
<DimensionSelector
isLoading={this.state.loadingPresets[dimensionName]}
key={dimensionName}
dimensionName={dimensionName}
icon={icon}
dimensionLabel={$get('label', dimensionConfiguration)}
presets={this.presetsForDimension(dimensionName)}
activePreset={activePreset}
onSelect={this.handleSelectPreset}
showDropDownHeaderIcon={Object.keys(contentDimensionsObject).length === 1}
/>
);
}

render() {
const {contentDimensions, activePresets, i18nRegistry} = this.props;
const contentDimensionsObject = contentDimensions;
const contentDimensionsObjectKeys = Object.keys(contentDimensionsObject);

return contentDimensionsObjectKeys.length ? (
<DropDown.Stateless
style="darkest"
padded={true}
className={style.dropDown}
isOpen={this.state.isOpen}
onToggle={this.handleToggle}
onClose={this.handleClose}
>
<DropDown.Header
className={style.dropDown__header}
if (contentDimensionsObjectKeys.length === 1) {
const dimensionName = contentDimensionsObjectKeys[0];
return (
<div className={style.singleDimensionDropdown}>
{this.renderSingleDimensionSelector(dimensionName, contentDimensionsObject)}
</div>
)
}

if (contentDimensionsObjectKeys.length > 1) {
return (
<DropDown.Stateless
style="darkest"
padded={true}
className={style.dropDown}
isOpen={this.state.isOpen}
onToggle={this.handleToggle}
onClose={this.handleClose}
>
{contentDimensionsObjectKeys.map(dimensionName => {
const dimensionConfiguration = contentDimensionsObject[dimensionName];
const icon = $get('icon', dimensionConfiguration) && $get('icon', dimensionConfiguration);
return (<SelectedPreset
key={dimensionName}
dimensionName={dimensionName}
icon={icon}
dimensionLabel={i18nRegistry.translate($get('label', dimensionConfiguration))}
presetLabel={i18nRegistry.translate($get([dimensionName, 'label'], activePresets))}
/>
);
})}
</DropDown.Header>
<DropDown.Contents className={style.dropDown__contents}>
{contentDimensionsObjectKeys.map(dimensionName => {
const dimensionConfiguration = contentDimensionsObject[dimensionName];
const icon = $get('icon', dimensionConfiguration) && $get('icon', dimensionConfiguration);
// First look for active preset in transient state, else take it from activePresets prop
const activePreset = this.getEffectivePresets(this.state.transientPresets)[dimensionName];
return (<DimensionSelector
isLoading={this.state.loadingPresets[dimensionName]}
key={dimensionName}
dimensionName={dimensionName}
icon={icon}
dimensionLabel={$get('label', dimensionConfiguration)}
presets={this.presetsForDimension(dimensionName)}
activePreset={activePreset}
onSelect={this.handleSelectPreset}
/>
);
})}
{Object.keys(contentDimensions).length > 1 && <div className={style.buttonGroup}>
<Button
id="neos-DimensionSwitcher-Cancel"
onClick={this.handleClose}
style="lighter"
className={style.cancelButton}
<DropDown.Header
className={style.dropDown__header}
>
{contentDimensionsObjectKeys.map(dimensionName => {
const dimensionConfiguration = contentDimensionsObject[dimensionName];
const icon = this.getDimensionIcon(dimensionName, contentDimensionsObject);
return (
<SelectedPreset
key={dimensionName}
dimensionName={dimensionName}
icon={icon}
dimensionLabel={i18nRegistry.translate($get('label', dimensionConfiguration))}
presetLabel={i18nRegistry.translate($get([dimensionName, 'label'], activePresets))}
/>
);
})}
</DropDown.Header>
<DropDown.Contents className={style.dropDown__contents}>
{contentDimensionsObjectKeys.map(dimensionName => {
const dimensionConfiguration = contentDimensionsObject[dimensionName];
const icon = this.getDimensionIcon(dimensionName, contentDimensionsObject);
return (
<li key={dimensionName} className={style.dimensionCategory}>
<div className={style.dimensionLabel}>
<Icon icon={icon} padded="right" className={style.dimensionCategory__icon}/>
<I18n id={$get('label', dimensionConfiguration)}/>
</div>
{this.renderSingleDimensionSelector(dimensionName, contentDimensionsObject)}
</li>
)
})}
{Object.keys(contentDimensions).length > 1 && <div className={style.buttonGroup}>
<Button
id="neos-DimensionSwitcher-Cancel"
onClick={this.handleClose}
style="lighter"
className={style.cancelButton}
>
<I18n id="Neos.Neos:Main:cancel" fallback="Cancel"/>
</Button>
<Button
id="neos-DimensionSwitcher-Apply"
onClick={this.handleApplyPresets}
style="brand"
<I18n id="Neos.Neos:Main:cancel" fallback="Cancel"/>
</Button>
<Button
id="neos-DimensionSwitcher-Apply"
onClick={this.handleApplyPresets}
style="brand"
>
<I18n id="Neos.Neos:Main:apply" fallback="Apply"/>
</Button>
</div>}
</DropDown.Contents>
</DropDown.Stateless>
) : null;
<I18n id="Neos.Neos:Main:apply" fallback="Apply"/>
</Button>
</div>}
</DropDown.Contents>
</DropDown.Stateless>
)
}

return null;
}

presetsForDimension(dimensionName) {
Expand Down
Loading