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
Show file tree
Hide file tree
Changes from 9 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
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ dist: jammy
language: node_js
node_js:
- lts/*
- node
- "22"
sudo: false
before_install:
- curl -fsSL https://www.mongodb.org/static/pgp/server-4.4.asc | sudo gpg -o /usr/share/keyrings/mongodb-server-4.4.gpg --dearmor
Expand Down
26 changes: 17 additions & 9 deletions Slider/Slider.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/

import {forKey, forProp, forward, forwardWithPrevent, handle, not} from '@enact/core/handle';
import EnactPropTypes from '@enact/core/internal/prop-types';
import useHandlers from '@enact/core/useHandlers';
import {setDefaultProps} from '@enact/core/util';
import {usePublicClassNames} from '@enact/core/usePublicClassNames';
Expand Down Expand Up @@ -72,7 +73,7 @@ const sliderDefaultProps = {
*/
const SliderBase = (props) => {
const sliderProps = setDefaultProps(props, sliderDefaultProps);
const {active, className, css, disabled, focused, keyFrequency, showAnchor, ...rest} = sliderProps;
const {active, className, css, disabled, focused, keyFrequency, showAnchor, sliderRef, ...rest} = sliderProps;

validateSteppedOnce(p => p.knobStep, {
component: 'Slider',
Expand All @@ -88,7 +89,6 @@ const SliderBase = (props) => {
const tooltip = sliderProps.tooltip === true ? ProgressBarTooltip : sliderProps.tooltip;

const spotlightAccelerator = useRef();
const ref = useRef();
const {current: context} = useRef({lastWheelTimeStamp: 0});

const handlers = useHandlers({
Expand Down Expand Up @@ -146,18 +146,18 @@ const SliderBase = (props) => {
}, [keyFrequency]);

useLayoutEffect(() => {
const sliderRef = ref.current;
const sliderRefCurrent = sliderRef.current;

if (sliderRef) {
sliderRef.addEventListener('wheel', nativeEventHandlers.onWheel, {passive: false});
if (sliderRefCurrent) {
sliderRefCurrent.addEventListener('wheel', nativeEventHandlers.onWheel, {passive: false});
}
return () => {
if (sliderRef) {
sliderRef.removeEventListener('wheel', nativeEventHandlers.onWheel, {passive: false});
if (sliderRefCurrent) {
sliderRefCurrent.removeEventListener('wheel', nativeEventHandlers.onWheel, {passive: false});
}
};

}, [ref, nativeEventHandlers.onWheel]);
}, [sliderRef, nativeEventHandlers.onWheel]);

delete rest.activateOnSelect;
delete rest.knobStep;
Expand All @@ -178,7 +178,7 @@ const SliderBase = (props) => {
progressBarComponent={
<ProgressBar css={mergedCss} />
}
ref={ref}
ref={sliderRef}
step={step}
tooltipComponent={
<ComponentOverride
Expand Down Expand Up @@ -345,6 +345,14 @@ SliderBase.propTypes = /** @lends sandstone/Slider.SliderBase.prototype */ {
*/
showAnchor: PropTypes.bool,

/**
* Called with the reference to the Slider node.
*
* @type {Object|Function}
* @public
*/
sliderRef: EnactPropTypes.ref,

/**
* The amount to increment or decrement the value.
*
Expand Down
229 changes: 104 additions & 125 deletions Slider/SliderBehaviorDecorator.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,16 @@
import {forward, forwardCustom} from '@enact/core/handle';
import hoc from '@enact/core/hoc';
import platform from '@enact/core/platform';
import {setDefaultProps} from "@enact/core/util";
paul-beldean-lgp marked this conversation as resolved.
Show resolved Hide resolved
import Pause from '@enact/spotlight/Pause';
import IString from 'ilib/lib/IString';
import PropTypes from 'prop-types';
import {Component, createRef} from 'react';
import {useCallback, useEffect, useRef, 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 @@ -26,6 +21,12 @@ const defaultConfig = {
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 @@ -40,63 +41,33 @@ const defaultConfig = {
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.sliderRef = createRef();

this.state = {
active: false,
dragging: false,
focused: false,
useHintText: true,
prevValue: props.value
};
}

static getDerivedStateFromProps (props, state) {
if (props.value !== state.prevValue) {
return {
useHintText: false,
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] = useState(() => new Pause());
paul-beldean-lgp marked this conversation as resolved.
Show resolved Hide resolved
const sliderRef = useRef();
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);

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();
};
});
paul-beldean-lgp marked this conversation as resolved.
Show resolved Hide resolved

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 @@ -107,91 +78,99 @@ const SliderBehaviorDecorator = hoc(defaultConfig, (config, Wrapped) => {
}

return valueText;
}
}, [sliderBehaviorProps, useHintText]);

focusSlider () {
let slider = this.sliderRef.current.node;
const focusSlider = useCallback(() => {
let slider = sliderRef.current;
if (slider.getAttribute('role') !== 'slider') {
slider = slider.querySelector('[role="slider"]');
}
slider.focus();
}
}, [sliderRef]);

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

handleBlur (ev) {
forward('onBlur', ev, this.props);
this.setState({
focused: false,
useHintText: true
});
}
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

handleDragStart () {
const handleDragStart = useCallback(() => {
// on platforms with a touchscreen, we want to focus slider when dragging begins
if (platform.touchScreen) {
this.focusSlider();
focusSlider();
}
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();
paused.pause();
setDragging(true);
}, [focusSlider, paused, setDragging]);

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;
}

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}
sliderRef={sliderRef}
/>
);

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;
}
};

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}
ref={this.sliderRef}
/>
);
}
SliderBehavior.displayName = 'SliderBehaviorDecorator';
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