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-13724: Converted DebounceDecorator to functional component #1784

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from
Open
Changes from 2 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
102 changes: 51 additions & 51 deletions internal/DebounceDecorator/DebounceDecorator.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import hoc from '@enact/core/hoc';
import {Job} from '@enact/core/util';
import PropTypes from 'prop-types';
import {Component} from 'react';
import {useCallback, useEffect, useMemo} from 'react';

/**
* Default config for {@link sandstone/internal/DebounceDecorator.DebounceDecorator}.
Expand Down Expand Up @@ -60,66 +60,66 @@ const defaultConfig = {
const DebounceDecorator = hoc(defaultConfig, (config, Wrapped) => {
const {cancel, debounce, delay} = config;

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

static propTypes = /** @lends sandstone/internal/DebounceDecorator.DebounceDecorator.prototype */ {
/**
* Handler for `onChange` events
*
* `'onChange'` can be changed to a different prop name by specifying the `debounce`
* config option.
*
* @see {@link sandstone/internal/DebounceDecorator.DebounceDecorator.defaultConfig#debounce}
* @name onChange
* @memberof sandstone/internal/DebounceDecorator.DebounceDecorator.prototype
* @type {Function}
* @public
*/
[debounce]: PropTypes.func
};

constructor (props) {
super(props);
this.job = new Job(this.emitEvent.bind(this), delay);
}

componentWillUnmount () {
this.job.stop();
}
const Debounce = (props) => {
ion-andrusciac-lgp marked this conversation as resolved.
Show resolved Hide resolved
let debounceProps = props;

emitEvent (ev) {
if (this.props[debounce]) {
this.props[debounce](ev);
const emitEvent = useCallback((ev) => {
if (props[debounce]) {
props[debounce](ev);
}
}

handleEvent = (ev) => {
this.job.start(ev);
};
}, [props]);

handleCancel = (ev) => {
if (this.props[cancel]) {
this.props[cancel](ev);
}
this.job.stop();
};
const job = useMemo(() => new Job(emitEvent, delay), [emitEvent]);
ion-andrusciac-lgp marked this conversation as resolved.
Show resolved Hide resolved

render () {
let props = this.props;
useEffect(() => {
return () => {
job.stop();
};
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);

if (debounce || cancel) {
props = {...props};
const handleEvent = useCallback((ev) => {
job.start(ev);
}, [job]);

if (debounce) props[debounce] = this.handleEvent;
if (cancel) props[cancel] = this.handleCancel;
const handleCancel = useCallback((ev) => {
if (props[cancel]) {
props[cancel](ev);
}
job.stop();
}, [job, props]);

return (
<Wrapped {...props} />
);
if (debounce || cancel) {
debounceProps = {...props};

if (debounce) debounceProps[debounce] = handleEvent;
if (cancel) debounceProps[cancel] = handleCancel;
}

return (
<Wrapped {...debounceProps} />
);
};

Debounce.displayName = 'DebounceDecorator';

Debounce.propTypes = {/** @lends sandstone/internal/DebounceDecorator.DebounceDecorator.prototype */
/**
* Handler for `onChange` events
*
* `'onChange'` can be changed to a different prop name by specifying the `debounce`
* config option.
*
* @see {@link sandstone/internal/DebounceDecorator.DebounceDecorator.defaultConfig#debounce}
* @name onChange
* @memberof sandstone/internal/DebounceDecorator.DebounceDecorator.prototype
* @type {Function}
* @public
*/
[debounce]: PropTypes.func
};

return Debounce;
});

export default DebounceDecorator;
Expand Down
Loading