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

WRR-5260: Convert Slider/SlideBehaviourDecorator to functional component #1728

Open
wants to merge 15 commits into
base: develop
Choose a base branch
from
Open
Changes from 12 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
221 changes: 100 additions & 121 deletions Slider/SliderBehaviorDecorator.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,15 @@
import {forward, forwardCustom} from '@enact/core/handle';
import hoc from '@enact/core/hoc';
import {setDefaultProps} from '@enact/core/util';
import Pause from '@enact/spotlight/Pause';
import IString from 'ilib/lib/IString';
import PropTypes from 'prop-types';
import {Component} from 'react';
import {useCallback, useEffect, useMemo, useState} from 'react';

import $L from '../internal/$L';

import {forwardSpotlightEvents} from './utils';

const toggleActive = ({active}) => {
return {
active: !active
};
};

const defaultConfig = {
// FIXME: This is a compromise to maintain a single decorator for Slider and IncrementSlider
// that handles both a consolidated focus state and spotlight directional event mgmt. When this
Expand All @@ -25,6 +20,12 @@
emitSpotlightEvents: null
};

const sliderDefaultProps = {
daniel-stoian-lgp marked this conversation as resolved.
Show resolved Hide resolved
max: 100,
min: 0,
orientation: 'horizontal'
};

// Adds sandstone-specific slider behaviors
// * aria-valuetext handling
// * use aria-valuetext when set
Expand All @@ -39,62 +40,32 @@
const SliderBehaviorDecorator = hoc(defaultConfig, (config, Wrapped) => {
const {emitSpotlightEvents} = config;

return class extends Component {
static displayName = 'SliderBehaviorDecorator';

static propTypes = {
activateOnSelect: PropTypes.bool,
'aria-valuetext': PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
max: PropTypes.number,
min: PropTypes.number,
orientation: PropTypes.string,
value: PropTypes.number
};

static defaultProps = {
max: 100,
min: 0,
orientation: 'horizontal'
};

constructor (props) {
super(props);

this.paused = new Pause();
this.handleActivate = this.handleActivate.bind(this);
this.handleBlur = this.handleBlur.bind(this);
this.handleDragEnd = this.handleDragEnd.bind(this);
this.handleDragStart = this.handleDragStart.bind(this);
this.handleFocus = this.handleFocus.bind(this);
this.handleSpotlightEvents = this.handleSpotlightEvents.bind(this);
this.bounds = {};

this.state = {
active: false,
dragging: false,
focused: false,
useHintText: true,
prevValue: props.value
};
}
const SliderBehavior = (props) => {
const sliderBehaviorProps = setDefaultProps(props, sliderDefaultProps);
daniel-stoian-lgp marked this conversation as resolved.
Show resolved Hide resolved

const paused = useMemo(() => new Pause(), []);
const [active, setActive] = useState(false);
const [dragging, setDragging] = useState(false);
const [focused, setFocused] = useState(false);
const [useHintText, setUseHintText] = useState(true);
const [prevValue, setPrevValue] = useState(sliderBehaviorProps.value);

static getDerivedStateFromProps (props, state) {
if (props.value !== state.prevValue) {
return {
useHintText: false,
prevValue: props.value
};
useEffect(() => {
if (sliderBehaviorProps.value !== prevValue) {
setUseHintText(false);
setPrevValue(sliderBehaviorProps.value);
}
return null;
}
}, [prevValue, sliderBehaviorProps.value]);

componentWillUnmount () {
this.paused.resume();
}

getValueText () {
const {'aria-valuetext': ariaValueText, max, min, orientation, value = min} = this.props;
const {useHintText} = this.state;
useEffect(() => {
return () => {
paused.resume();
};
}, []); // eslint-disable-line react-hooks/exhaustive-deps

const getValueText = useCallback(() => {
const {'aria-valuetext': ariaValueText, max, min, orientation, value = min} = sliderBehaviorProps;

const valueText = (ariaValueText != null) ? ariaValueText : value;
const verticalHint = `${new IString($L('From {startValue} to {lastValue}')).format({startValue: min, lastValue: max})} ${valueText} ${$L('change a value with up down button')}`;
Expand All @@ -105,78 +76,86 @@
}

return valueText;
}

handleActivate () {
forwardCustom('onActivate')(null, this.props);
this.setState(toggleActive);
}

handleBlur (ev) {
forward('onBlur', ev, this.props);
this.setState({
focused: false,
useHintText: true
});
}

handleDragStart () {
this.paused.pause();
this.setState({dragging: true});
}

handleDragEnd () {
this.paused.resume();
this.setState({dragging: false});
}

handleFocus (ev) {
forward('onFocus', ev, this.props);
if (!this.props.activateOnSelect) {
this.handleActivate();
}, [sliderBehaviorProps, useHintText]);

const handleActivate = useCallback(() => {
forwardCustom('onActivate')(null, sliderBehaviorProps);
setActive(prevState => !prevState);
}, [setActive, sliderBehaviorProps]);
daniel-stoian-lgp marked this conversation as resolved.
Show resolved Hide resolved

const handleBlur = useCallback((ev) => {
forward('onBlur', ev, sliderBehaviorProps);
setFocused(false);
setUseHintText(true);
}, [setFocused, setUseHintText, sliderBehaviorProps]);
daniel-stoian-lgp marked this conversation as resolved.
Show resolved Hide resolved

const handleDragStart = useCallback(() => {
paused.pause();
setDragging(true);
}, [paused, setDragging]);
daniel-stoian-lgp marked this conversation as resolved.
Show resolved Hide resolved

const handleDragEnd = useCallback(() => {
paused.resume();
setDragging(false);
}, [paused, setDragging]);
daniel-stoian-lgp marked this conversation as resolved.
Show resolved Hide resolved

const handleFocus = useCallback((ev) => {
forward('onFocus', ev, sliderBehaviorProps);
if (!sliderBehaviorProps.activateOnSelect) {
handleActivate();
}
this.setState({focused: true});
}
setFocused(true);
}, [handleActivate, setFocused, sliderBehaviorProps]);
daniel-stoian-lgp marked this conversation as resolved.
Show resolved Hide resolved

handleSpotlightEvents (ev) {
const handleSpotlightEvents = useCallback((ev) => {
if (!emitSpotlightEvents) {
forward('onKeyDown', ev, this.props);
forward('onKeyDown', ev, sliderBehaviorProps);
}
forwardSpotlightEvents(ev, sliderBehaviorProps);
}, [sliderBehaviorProps]);

forwardSpotlightEvents(ev, this.props);
const sliderProps = Object.assign({}, sliderBehaviorProps);

if (!emitSpotlightEvents) {
// Remove spotlight props before hitting spottable since we've handled them uniquely
delete sliderProps.onSpotlightLeft;
delete sliderProps.onSpotlightRight;
delete sliderProps.onSpotlightUp;
delete sliderProps.onSpotlightDown;

sliderProps.onKeyDown = handleSpotlightEvents;
} else {
sliderProps[emitSpotlightEvents] = handleSpotlightEvents;

Check warning on line 128 in Slider/SliderBehaviorDecorator.js

View check run for this annotation

Codecov / codecov/patch

Slider/SliderBehaviorDecorator.js#L127-L128

Added lines #L127 - L128 were not covered by tests
}

render () {
const props = Object.assign({}, this.props);
return (
<Wrapped
role="slider"
{...sliderProps}
active={active}
aria-valuetext={getValueText()}
focused={focused || dragging}
onActivate={handleActivate}
onBlur={handleBlur}
onDragStart={handleDragStart}
onDragEnd={handleDragEnd}
onFocus={handleFocus}
/>
);
};

if (!emitSpotlightEvents) {
// Remove spotlight props before hitting spottable since we've handled them uniquely
delete props.onSpotlightLeft;
delete props.onSpotlightRight;
delete props.onSpotlightUp;
delete props.onSpotlightDown;

props.onKeyDown = this.handleSpotlightEvents;
} else {
props[emitSpotlightEvents] = this.handleSpotlightEvents;
}
SliderBehavior.displayName = 'SliderBehaviorDecorator';

return (
<Wrapped
role="slider"
{...props}
active={this.state.active}
aria-valuetext={this.getValueText()}
focused={this.state.focused || this.state.dragging}
onActivate={this.handleActivate}
onBlur={this.handleBlur}
onDragStart={this.handleDragStart}
onDragEnd={this.handleDragEnd}
onFocus={this.handleFocus}
/>
);
}
SliderBehavior.propTypes = {
paul-beldean-lgp marked this conversation as resolved.
Show resolved Hide resolved
activateOnSelect: PropTypes.bool,
'aria-valuetext': PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
max: PropTypes.number,
min: PropTypes.number,
orientation: PropTypes.string,
value: PropTypes.number
};

return SliderBehavior;
});

export default SliderBehaviorDecorator;
Expand Down