-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Changes from 3 commits
af76c90
1bffdbe
2fc83ba
7332917
7f88073
f19b6eb
c40319d
385efdf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { default as ReactDatePicker } from 'react-datepicker'; | ||
import moment from 'moment'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { settings } from '@wordpress/date'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd prefer if we avoid using the |
||
|
||
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 } | ||
/>; | ||
} |
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'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since we already imported those, we could drop the
|
||
export { TimePicker } from './time'; | ||
|
||
export function DateTimePicker( { currentDate, onChange, locale, ...args } ) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe this could be exported as "default" There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 } | ||
/>, | ||
]; | ||
} |
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 |
---|---|---|
|
@@ -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'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here, we may avoid this dependency and just add a |
||
|
||
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' ); | ||
|
@@ -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 } | ||
> | ||
|
@@ -89,5 +102,3 @@ function PostScheduleClock( { is12Hour, selected, onChange } ) { | |
</div> | ||
); | ||
} | ||
|
||
export default PostScheduleClock; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,8 +2,6 @@ | |
* External dependencies | ||
*/ | ||
import { connect } from 'react-redux'; | ||
import DatePicker from 'react-datepicker'; | ||
import moment from 'moment'; | ||
|
||
/** | ||
* WordPress dependencies | ||
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In general, for multi-lines elements, we tend to wrap them in return (
<Component
prop={ value }
/>
); |
||
key="date-time-picker" | ||
currentDate={ date } | ||
onChange={ handleChange } | ||
locale={ settings.l10n.locale } | ||
/>; | ||
} | ||
|
||
export default connect( | ||
|
This file was deleted.
There was a problem hiding this comment.
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';
There was a problem hiding this comment.
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.