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

refactor(TimePicker): added support for showing seconds #1406

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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 react/src/lib/TimePicker/TimeSelector/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class TimeSelector extends React.Component {
militaryTime
? (parseInt(newValue, 10) > 23 ? 23 : parseInt(newValue, 10)) || ''
: (parseInt(newValue, 10) > 12 ? 12 : parseInt(newValue, 10)) || '';
} else if (unit === 'm') {
} else if (unit === 'm' || unit === 's') {
newValue =
parseInt(newValue, 10) > 59 ? 59 : parseInt(newValue, 10) || '';
}
Expand Down
54 changes: 37 additions & 17 deletions react/src/lib/TimePicker/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,29 +93,33 @@ class TimePicker extends React.Component {
this.setState({ selectedTime: newTime }, () => this.triggerOnChange(dayChange));
};

setTime = (hour, minute, pre) => {
setTime = (hour, minute, second, pre) => {
const meridianHour =
pre === 'PM' && parseInt(hour) < 12
? parseInt(hour) + 12
: pre === 'AM' && parseInt(hour) === 12 ? 0 : hour;
: pre === 'AM' && parseInt(hour) === 12
? 0
: hour;

this.setState({
selectedTime: this.state.selectedTime
.clone()
.hour(meridianHour)
.minute(minute)
}, () => this.triggerOnChange(0));
this.setState(
{
selectedTime: this.state.selectedTime
.clone()
.hour(meridianHour)
.minute(minute)
.second(second),
},
() => this.triggerOnChange(0)
);
};

onSelectKeyDown = (unit, e) => {
e.preventDefault();

const { minuteInterval, militaryTime } = this.props;
const hour =
!this.hour.value && unit === 'h'
? militaryTime ? 0 : 1
: this.hour.value;
const { showSeconds, minuteInterval, militaryTime } = this.props;
const hour = !this.hour.value && unit === 'h' ? (militaryTime ? 0 : 1) : this.hour.value;
const minute = !this.minute.value && unit === 'm' ? 0 : this.minute.value;
const second = showSeconds && (!this.second.value && unit === 's' ? 0 : this.second.value);
const pre = !militaryTime && this.pre.value;

if (e.keyCode === 65 && unit === 'pre') {
Expand All @@ -137,7 +141,7 @@ class TimePicker extends React.Component {

return this.changeTime(unit, -minuteInterval);
} else {
this.setTime(hour, minute, pre);
this.setTime(hour, minute, second, pre);
}
};

Expand All @@ -151,18 +155,19 @@ class TimePicker extends React.Component {
};

render() {
const { militaryTime, minuteInterval } = this.props;
const { showSeconds, militaryTime, minuteInterval } = this.props;
const { anchorNode, inputId, isOpen } = this.state;

// Force the global locale onto our display moment
let selectedMoment = this.state.selectedTime.locale(moment.locale());
// Splits the moment string into seperate parts in order to add functionality to the TimePicker
const timeString = militaryTime
? selectedMoment.format('HH:mm')
: selectedMoment.format('LT');
? selectedMoment.format(showSeconds ? 'HH:mm:ss' : 'HH:mm')
: selectedMoment.format(showSeconds ? 'LTS' : 'LT');

const hourText = selectedMoment.format(militaryTime ? 'HH' : 'hh');
const minuteText = selectedMoment.format('mm');
const secondText = selectedMoment.format('ss');
const postText = selectedMoment.format('A');

const text = (
Expand Down Expand Up @@ -210,6 +215,18 @@ class TimePicker extends React.Component {
onUpClick={() => this.changeTime('m', minuteInterval)}
onDownClick={() => this.changeTime('m', -minuteInterval)}
/>
{showSeconds && (
<TimeSelector
unit="s"
min={0}
value={secondText}
onWheel={this.onSelectWheel}
inputRef={ref => (this.second = ref)}
onKeyDown={this.onSelectKeyDown}
onUpClick={() => this.changeTime('s', 1)}
onDownClick={() => this.changeTime('s', -1)}
/>
)}
{!militaryTime && (
<TimeSelector
unit="pre"
Expand Down Expand Up @@ -240,6 +257,8 @@ TimePicker.propTypes = {
className: PropTypes.string,
/** @prop Set Input element ID | '' */
inputId: PropTypes.string,
/** @prop Choose to show seconds | false */
showSeconds: PropTypes.bool,
/** @prop Choose to use military time | false */
militaryTime: PropTypes.bool,
/** @prop Determine the minute interval | 1 */
Expand All @@ -253,6 +272,7 @@ TimePicker.propTypes = {
TimePicker.defaultProps = {
className: '',
inputId: '',
showSeconds: false,
militaryTime: false,
minuteInterval: 1,
onChange: null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ ShallowWrapper {
minuteInterval={1}
onChange={null}
selectedTime={2018-01-29T09:12:40.000Z}
showSeconds={false}
/>,
Symbol(enzyme.__renderer__): Object {
"batchedUpdates": [Function],
Expand Down