Skip to content

Commit

Permalink
Add seconds to DateTimePicker view
Browse files Browse the repository at this point in the history
- Add seconds as an available view in DateTimePickerView
- Use DateTimePickerView type as the type for views and openTo
- Change typography variant depending on whether seconds are being displayed or not
- set number of grid elements depending on seconds view
  • Loading branch information
JamesMHenderson committed Jul 7, 2020
1 parent 510c745 commit ccbd2fb
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 18 deletions.
11 changes: 11 additions & 0 deletions docs/pages/demo/datetime-picker/CustomDateTimePicker.example.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,17 @@ function CustomDateTimePicker(props) {
onChange={handleClearedDateChange}
renderInput={props => <TextField {...props} helperText="Clear Initial State" />}
/>

<DateTimePicker
value={selectedDate}
onChange={handleDateChange}
renderInput={props => <TextField {...props} helperText="With Seconds" />}
views={['year', 'month', 'date', 'hours', 'minutes', 'seconds']}
inputFormat={props.__willBeReplacedGetFormatString({
moment: 'YYYY/MM/DD HH:mm:ss A',
dateFns: 'yyyy/MM/dd HH:mm:ss a',
})}
/>
</React.Fragment>
);
}
Expand Down
28 changes: 14 additions & 14 deletions docs/prop-types.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/src/DateTimePicker/DateTimePicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { ParsableDate, defaultMinDate, defaultMaxDate } from '../constants/prop-
export type DateTimePickerView = 'year' | 'date' | 'month' | 'hours' | 'minutes' | 'seconds';

export interface BaseDateTimePickerProps
extends WithViewsProps<'year' | 'date' | 'month' | 'hours' | 'minutes'>,
extends WithViewsProps<'year' | 'date' | 'month' | 'hours' | 'minutes' | 'seconds'>,
ValidationProps<DateAndTimeValidationError, ParsableDate>,
ExportedClockViewProps,
ExportedCalendarViewProps {
Expand Down
32 changes: 29 additions & 3 deletions lib/src/DateTimePicker/DateTimePickerToolbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import { MaterialUiPickersDate } from '../typings/date';
import { ToolbarComponentProps } from '../Picker/Picker';
import { withDefaultProps } from '../_shared/withDefaultProps';
import { WrapperVariantContext } from '../wrappers/WrapperVariantContext';
import { useMemo } from 'react';
import { arrayIncludes } from '../_helpers/utils';

const muiComponentConfig = { name: 'MuiPickersDateTimePickerToolbar' };

Expand Down Expand Up @@ -57,6 +59,7 @@ export const DateTimePickerToolbar: React.FC<ToolbarComponentProps> = withDefaul
toolbarFormat,
toolbarPlaceholder = '––',
toolbarTitle = 'SELECT DATE & TIME',
views,
}) => {
const utils = useUtils();
const classes = useStyles();
Expand All @@ -81,6 +84,9 @@ export const DateTimePickerToolbar: React.FC<ToolbarComponentProps> = withDefaul
return utils.format(date, 'shortDate');
}, [date, toolbarFormat, toolbarPlaceholder, utils]);

const hasSeconds = useMemo(() => arrayIncludes(views, 'seconds'), [views]);
const timeTypographyText = hasSeconds ? 'h4' : 'h3';

return (
<React.Fragment>
{wrapperVariant !== 'desktop' && (
Expand Down Expand Up @@ -114,25 +120,45 @@ export const DateTimePickerToolbar: React.FC<ToolbarComponentProps> = withDefaul
<div className={classes.timeContainer}>
<ToolbarButton
tabIndex={-1}
variant="h3"
variant={timeTypographyText}
data-mui-test="hours"
onClick={() => setOpenView('hours')}
selected={openView === 'hours'}
value={date ? formatHours(date) : '--'}
typographyClassName={classes.timeTypography}
/>

<ToolbarText variant="h3" value=":" className={classes.separator} />
<ToolbarText variant={timeTypographyText} value=":" className={classes.separator} />

<ToolbarButton
tabIndex={-1}
variant="h3"
variant={timeTypographyText}
data-mui-test="minutes"
onClick={() => setOpenView('minutes')}
selected={openView === 'minutes'}
value={date ? utils.format(date, 'minutes') : '--'}
typographyClassName={classes.timeTypography}
/>

{hasSeconds && (
<>
<ToolbarText
variant={timeTypographyText}
value=":"
className={classes.separator}
/>

<ToolbarButton
tabIndex={-1}
variant={timeTypographyText}
data-mui-test="seconds"
onClick={() => setOpenView('seconds')}
selected={openView === 'seconds'}
value={date ? utils.format(date, 'seconds') : '--'}
typographyClassName={classes.timeTypography}
/>
</>
)}
</div>
</PickerToolbar>
)}
Expand Down
25 changes: 25 additions & 0 deletions lib/src/__tests__/DateTimePickerRoot.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,28 @@ describe('e2e - DateTimePicker', () => {
toHaveBeenCalledExceptMoment(onChangeMock, [utilsToUse.date('2018-01-01T12:00:00.000Z')]);
});
});

describe('e2e - DateTimePicker with seconds', () => {
it('Should render hour view', () => {
const onChangeMock = jest.fn();

const component = mount(
<MobileDateTimePicker
ampm
open
openTo="hours"
value={utilsToUse.date('2018-01-01T00:00:00.000Z')}
onChange={onChangeMock}
renderInput={props => <TextField {...props} />}
views={['year', 'month', 'date', 'hours', 'minutes', 'seconds']}
/>
);

component
.find('ToolbarButton')
.at(4)
.simulate('click');

expect(component.find('ClockView').props().type).toBe('seconds');
});
});

0 comments on commit ccbd2fb

Please sign in to comment.