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 PostSchedule to make calendar/clock available as components #4180

Merged
merged 8 commits into from
Dec 27, 2017
Merged
Show file tree
Hide file tree
Changes from 3 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
22 changes: 22 additions & 0 deletions components/date-time/date.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* External dependencies
*/
import { default as ReactDatePicker } from 'react-datepicker';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor: this could be import ReactDatePicker from 'react-datepicker';

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Note that we may change react-datepicker later as it's not accessible.

import moment from 'moment';

/**
* WordPress dependencies
*/
import { settings } from '@wordpress/date';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer if we avoid using the date module in the components module to keep it as generic as possible?


export function DatePicker( { currentDate, onChange, locale = settings.l10n.locale, ...args } ) {
const momentDate = currentDate ? moment( currentDate ) : moment();

return <ReactDatePicker
inline
selected={ momentDate }
onChange={ onChange }
locale={ locale }
{ ...args }
/>;
}
26 changes: 26 additions & 0 deletions components/date-time/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* Internal dependencies
*/
import './style.scss';
import { DatePicker } from './date';
import { TimePicker } from './time';

export { DatePicker } from './date';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we already imported those, we could drop the from

export { DatePicker, TimePicker }

export { TimePicker } from './time';

export function DateTimePicker( { currentDate, onChange, locale, ...args } ) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe this could be exported as "default"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exporting the function as default doesn't seem possible; doing so returns undefined when React attempts to render it.

return [
<DatePicker
key="date-picker"
currentDate={ currentDate }
onChange={ onChange }
locale={ locale }
{ ...args }
/>,
<TimePicker
key="time-picker"
currentTime={ currentDate }
onChange={ onChange }
/>,
];
}
35 changes: 35 additions & 0 deletions components/date-time/style.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
@import '~react-datepicker/dist/react-datepicker';

.components-time-picker {
display: flex;
align-items: center;
justify-content: center;
margin-top: 10px;

.components-time-picker__input {
width: 40px;
}

.components-time-picker__separator {
padding: 0 4px;
}

.components-time-picker__am-button {
margin-left: 8px;
margin-right: -1px;
border-radius: 3px 0 0 3px;
}

.components-time-picker__pm-button {
margin-left: -1px;
border-radius: 0 3px 3px 0;
}

.components-time-picker__am-button.is-toggled,
.components-time-picker__pm-button.is-toggled {
background: $light-gray-300;
border-color: $dark-gray-100;
box-shadow: inset 0 2px 5px -3px $dark-gray-500;
transform: translateY(1px);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,27 @@
* External dependencies
*/
import { isInteger } from 'lodash';
import moment from 'moment';

/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { Button } from '@wordpress/components';
import Button from '../button';
import { settings } from '@wordpress/date';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here, we may avoid this dependency and just add a is12Hour prop instead?


export function TimePicker( { currentTime, onChange } ) {
const selected = currentTime ? moment( currentTime ) : moment();

// To know if the current timezone is a 12 hour time with look for "a" in the time format
// We also make sure this a is not escaped by a "/"
const is12Hour = /a(?!\\)/i.test(
settings.formats.time
.toLowerCase() // Test only the lower case a
.replace( /\\\\/g, '' ) // Replace "//" with empty strings
.split( '' ).reverse().join( '' ) // Reverse the string and test for "a" not followed by a slash
);

function PostScheduleClock( { is12Hour, selected, onChange } ) {
const minutes = selected.format( 'mm' );
const am = selected.format( 'A' );
const hours = selected.format( is12Hour ? 'hh' : 'HH' );
Expand Down Expand Up @@ -56,30 +69,30 @@ function PostScheduleClock( { is12Hour, selected, onChange } ) {
};

return (
<div className="editor-post-schedule__clock">
<div className="components-time-picker">
<input
className="editor-post-schedule__clock-input"
className="components-time-picker__input"
type="text"
value={ hours }
onChange={ updateHours }
/>
<span className="editor-post-schedule__clock-separator">:</span>
<span className="components-time-picker__separator">:</span>
<input
className="editor-post-schedule__clock-input"
className="components-time-picker__input"
type="text"
value={ minutes }
onChange={ updateMinutes }
/>
{ is12Hour && <div>
<Button
className="button editor-post-schedule__clock-am-button"
className="button components-time-picker__am-button"
isToggled={ am === 'AM' }
onClick={ setAM }
>
{ __( 'AM' ) }
</Button>
<Button
className="button editor-post-schedule__clock-pm-button"
className="button components-time-picker__pm-button"
isToggled={ am === 'PM' }
onClick={ setPM }
>
Expand All @@ -89,5 +102,3 @@ function PostScheduleClock( { is12Hour, selected, onChange } ) {
</div>
);
}

export default PostScheduleClock;
1 change: 1 addition & 0 deletions components/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export { default as Autocomplete } from './autocomplete';
export { default as Button } from './button';
export { default as ClipboardButton } from './clipboard-button';
export { default as Dashicon } from './dashicon';
export { DateTimePicker, DatePicker, TimePicker } from './date-time';
export { default as DropZone } from './drop-zone';
export { default as DropZoneProvider } from './drop-zone/provider';
export { default as Dropdown } from './dropdown';
Expand Down
36 changes: 7 additions & 29 deletions editor/components/post-schedule/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
* External dependencies
*/
import { connect } from 'react-redux';
import DatePicker from 'react-datepicker';
import moment from 'moment';

/**
* WordPress dependencies
Expand All @@ -13,41 +11,21 @@ import { settings } from '@wordpress/date';
/**
* Internal dependencies
*/
import './style.scss';
import PostScheduleClock from './clock';
import { DateTimePicker } from '@wordpress/components';
import { getEditedPostAttribute } from '../../store/selectors';
import { editPost } from '../../store/actions';

export function PostSchedule( { date, onUpdateDate } ) {
const momentDate = date ? moment( date ) : moment();
const handleChange = ( newDate ) => {
onUpdateDate( newDate.format( 'YYYY-MM-DDTHH:mm:ss' ) );
};

// To know if the current timezone is a 12 hour time with look for "a" in the time format
// We also make sure this a is not escaped by a "/"
const is12HourTime = /a(?!\\)/i.test(
settings.formats.time
.toLowerCase() // Test only the lower case a
.replace( /\\\\/g, '' ) // Replace "//" with empty strings
.split( '' ).reverse().join( '' ) // Reverse the string and test for "a" not followed by a slash
);

return [
<DatePicker
key="date-picker"
inline
selected={ momentDate }
onChange={ handleChange }
locale={ settings.l10n.locale }
/>,
<PostScheduleClock
key="clock"
selected={ momentDate }
onChange={ handleChange }
is12Hour={ is12HourTime }
/>,
];
return <DateTimePicker
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In general, for multi-lines elements, we tend to wrap them in () so something like

return (
  <Component
    prop={ value }
  />
);

key="date-time-picker"
currentDate={ date }
onChange={ handleChange }
locale={ settings.l10n.locale }
/>;
}

export default connect(
Expand Down
35 changes: 0 additions & 35 deletions editor/components/post-schedule/style.scss

This file was deleted.