Skip to content

Commit

Permalink
WRR-6385: Converted sandstone/TimePicker/TimePickerBase HourPicker to…
Browse files Browse the repository at this point in the history
… functional component (#1732)

* converted HourPicker to functional component

* fixed lint warning

* fixed review issues

* empty commit

* fixed review issues

* updated .travis.yml

---------

Co-authored-by: Juwon Jeong <juwon.jeong@lge.com>
  • Loading branch information
paul-beldean-lgp and juwonjeong authored Dec 4, 2024
1 parent 313bc48 commit 3e51a0b
Showing 1 changed file with 25 additions and 37 deletions.
62 changes: 25 additions & 37 deletions TimePicker/TimePickerBase.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import kind from '@enact/core/kind';
import {Component, Fragment} from 'react';
import {Fragment, useEffect, useState} from 'react';
import PropTypes from 'prop-types';

import $L from '../internal/$L';
Expand Down Expand Up @@ -27,45 +27,33 @@ const hours12 = [
* @ui
* @private
*/
class HourPicker extends Component {
static propTypes = {
hasMeridiem: PropTypes.bool,
value: PropTypes.number
};

constructor (props) {
super(props);

this.state = {
noAnimation: false,
prevValue: props.value
};
}

static getDerivedStateFromProps (props, state) {
if (state.prevValue !== props.value) {
const hours = props.hasMeridiem ? hours12 : hours24;
const HourPicker = (props) => {
const {hasMeridiem, value, ...rest} = props;
const hours = hasMeridiem ? hours12 : hours24;

return {
noAnimation: hours[state.prevValue] === hours[props.value],
prevValue: props.value
};
}

return null;
}
const [noAnimation, setNoAnimation] = useState(false);
const [prevValue, setPrevValue] = useState(value);

render () {
const {hasMeridiem, ...rest} = this.props;
const hours = hasMeridiem ? hours12 : hours24;
useEffect(() => {
if (prevValue !== value) {
const hoursArray = hasMeridiem ? hours12 : hours24;

return (
<DateComponentPicker {...rest} noAnimation={this.state.noAnimation}>
{hours}
</DateComponentPicker>
);
}
}
setNoAnimation(hoursArray[prevValue] === hoursArray[value]);
setPrevValue(value);
}
}, [hasMeridiem, prevValue, value]);

return (
<DateComponentPicker {...rest} noAnimation={noAnimation} value={value}>
{hours}
</DateComponentPicker>
);
};

HourPicker.propTypes = {
hasMeridiem: PropTypes.bool,
value: PropTypes.number
};

/**
* {@link sandstone/TimePicker.TimePickerBase} is the stateless functional time picker
Expand Down

0 comments on commit 3e51a0b

Please sign in to comment.