Skip to content

Commit

Permalink
Merge pull request #317 from tkachenko-tatiana/develop
Browse files Browse the repository at this point in the history
Today button implementation
  • Loading branch information
dmtrKovalenko authored Apr 2, 2018
2 parents 290d130 + d255785 commit 7b38957
Show file tree
Hide file tree
Showing 13 changed files with 223 additions and 5 deletions.
61 changes: 61 additions & 0 deletions docs/prop-types.json
Original file line number Diff line number Diff line change
Expand Up @@ -2859,6 +2859,27 @@
},
"required": true,
"description": ""
},
"todayLabel": {
"type": {
"name": "string"
},
"required": true,
"description": ""
},
"showTodayButton": {
"type": {
"name": "bool"
},
"required": true,
"description": ""
},
"onSetToday": {
"type": {
"name": "func"
},
"required": true,
"description": ""
}
}
}
Expand Down Expand Up @@ -2999,6 +3020,13 @@
"description": "",
"displayName": "ModalWrapper",
"methods": [
{
"name": "handleSetTodayDate",
"docblock": null,
"modifiers": [],
"params": [],
"returns": null
},
{
"name": "open",
"docblock": null,
Expand Down Expand Up @@ -3114,6 +3142,28 @@
"computed": false
}
},
"todayLabel": {
"type": {
"name": "string"
},
"required": false,
"description": "\"Today\" label message",
"defaultValue": {
"value": "'Today'",
"computed": false
}
},
"showTodayButton": {
"type": {
"name": "bool"
},
"required": false,
"description": "If true today button will be displayed\n<b>Note*</b> that clear button has higher priority",
"defaultValue": {
"value": "false",
"computed": false
}
},
"onOpen": {
"type": {
"name": "func"
Expand Down Expand Up @@ -3180,6 +3230,17 @@
"computed": true
}
},
"onSetToday": {
"type": {
"name": "func"
},
"required": false,
"description": "",
"defaultValue": {
"value": "undefined",
"computed": true
}
},
"children": {
"type": {
"name": "node"
Expand Down
12 changes: 12 additions & 0 deletions docs/src/Examples/Demo/BasicDatePicker.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,18 @@ export default class BasicDatePicker extends PureComponent {
animateYearScrolling={false}
/>
</div>

<div className="picker">
<DatePicker
label="With today button"
showTodayButton
disableFuture
maxDateMessage="Date must be less than today"
value={selectedDate}
onChange={this.handleDateChange}
animateYearScrolling={false}
/>
</div>
</Fragment>
);
}
Expand Down
10 changes: 10 additions & 0 deletions docs/src/Examples/Demo/BasicDateTimePicker.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,16 @@ export default class BasicDateTimePicker extends PureComponent {
label="24h clock"
/>
</div>

<div className="picker">
<DateTimePicker
value={selectedDate}
disablePast
onChange={this.handleDateChange}
label="With Today Button"
showTodayButton
/>
</div>
</Fragment>
);
}
Expand Down
10 changes: 10 additions & 0 deletions docs/src/Examples/Demo/TimePickerBasic.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,16 @@ export default class BasicUsage extends PureComponent {
onChange={this.handleDateChange}
/>
</div>

<div className="picker">
<TimePicker
showTodayButton
todayLabel="now"
label="With today button"
value={selectedDate}
onChange={this.handleDateChange}
/>
</div>
</Fragment>
);
}
Expand Down
74 changes: 72 additions & 2 deletions lib/__tests__/_shared/ModalDialog.test.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,86 @@
import React from 'react';
import { shallow } from '../test-utils';
import ModalDialog from '../../src/_shared/ModalDialog';
import { ModalDialog } from '../../src/_shared/ModalDialog';

const initialProps = {
onAccept: jest.fn(),
onDismiss: jest.fn(),
onClear: jest.fn(),
okLabel: 'OK',
cancelLabel: 'Cancel',
clearLabel: 'Clear',
clearable: false,
todayLabel: 'Today',
showTodayButton: false,
onSetToday: jest.fn(),
classes: {},
children: 'Test',
};

describe('ModalDialog', () => {
let component;
const props = { ...initialProps };

beforeEach(() => {
component = shallow(<ModalDialog />);
component = shallow(<ModalDialog {...props} />);
});

it('Should renders', () => {
// console.log(component.debug());
expect(component).toBeTruthy();
});

it('Should render dialog content', () => {
expect(component.find('WithStyles(DialogContent)').props().children).toBe(props.children);
});

it('Should render dialog actions with 2 buttons', () => {
expect(component.find('WithStyles(DialogActions)').length).toBe(1);
expect(component.find('WithStyles(Button)').at(0).props().children).toBe('Cancel');
expect(component.find('WithStyles(Button)').at(1).props().children).toBe('OK');
});

it('Should handle on OK button click', () => {
component.find('WithStyles(Button)').at(1).simulate('click');
expect(props.onAccept).toHaveBeenCalled();
});

it('Should handle on Cancel button click', () => {
component.find('WithStyles(Button)').at(0).simulate('click');
expect(props.onDismiss).toHaveBeenCalled();
});
});

describe('ModalDialog with Clear Button', () => {
let component;
const props = {
...initialProps,
clearable: true,
};

beforeEach(() => {
component = shallow(<ModalDialog {...props} />);
});

it('Should handle on Clear button click', () => {
component.find('WithStyles(Button)').at(0).simulate('click');
expect(props.onClear).toHaveBeenCalled();
});
});

describe('ModalDialog with Today Button', () => {
let component;
const props = {
...initialProps,
showTodayButton: true,
};

beforeEach(() => {
component = shallow(<ModalDialog {...props} />);
});

it('Should handle on Clear button click', () => {
component.find('WithStyles(Button)').at(0).simulate('click');
expect(props.onSetToday).toHaveBeenCalled();
});
});
1 change: 1 addition & 0 deletions lib/src/DatePicker/DatePickerWrapper.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ export class DatePickerWrapper extends PickerBase {
onAccept={this.handleAccept}
onChange={this.handleTextFieldChange}
onDismiss={this.handleDismiss}
onSetToday={this.handleSetTodayDate}
labelFunc={labelFunc}
minDate={minDate}
maxDate={maxDate}
Expand Down
1 change: 1 addition & 0 deletions lib/src/DateTimePicker/DateTimePickerWrapper.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ export class DateTimePickerWrapper extends PickerBase {
onChange={this.handleTextFieldChange}
onDismiss={this.handleDismiss}
onClear={this.handleClear}
onSetToday={this.handleSetTodayDate}
dialogContentClassName={classes.dialogContent}
minDate={minDate}
maxDate={maxDate}
Expand Down
1 change: 1 addition & 0 deletions lib/src/TimePicker/TimePickerWrapper.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export class TimePickerWrapper extends PickerBase {
onAccept={this.handleAccept}
onChange={this.handleTextFieldChange}
onDismiss={this.handleDismiss}
onSetToday={this.handleSetTodayDate}
{...other}
>
<TimePicker
Expand Down
1 change: 1 addition & 0 deletions lib/src/_shared/ModalDialog.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export interface DateTextFieldProps extends DialogProps {
onAccept: ButtonProps['onClick'];
onDismiss: ButtonProps['onClick'];
onClear: ButtonProps['onClick'];
onSetToday: ButtonProps['onClick'];
dialogContentClassName?: string;
invalidLabel?: string;
okLabel?: ReactNode;
Expand Down
28 changes: 26 additions & 2 deletions lib/src/_shared/ModalDialog.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,30 @@ const styles = {
marginRight: 'auto',
},
},
todayDialogAction: {
'&:first-child': {
marginRight: 'auto',
},
},
dialogAction: {
// empty but may be needed for override
},
};

const ModalDialog = ({
export const ModalDialog = ({
children,
classes,
onAccept,
onDismiss,
onClear,
onSetToday,
okLabel,
cancelLabel,
clearLabel,
todayLabel,
dialogContentClassName,
clearable,
showTodayButton,
...other
}) => (
<Dialog onClose={onDismiss} classes={{ paper: classes.dialogRoot }} {...other}>
Expand All @@ -55,7 +63,10 @@ const ModalDialog = ({
<DialogActions
classes={{
root: clearable && classes.dialogActions,
action: classnames(classes.dialogAction, { [classes.clearableDialogAction]: clearable }),
action: classnames(classes.dialogAction, {
[classes.clearableDialogAction]: clearable,
[classes.todayDialogAction]: !clearable && showTodayButton,
}),
}}
>
{ clearable &&
Expand All @@ -68,6 +79,16 @@ const ModalDialog = ({
</Button>
}

{ !clearable && showTodayButton &&
<Button
color="primary"
onClick={onSetToday}
aria-label={todayLabel}
>
{ todayLabel }
</Button>
}

<Button
color="primary"
onClick={onDismiss}
Expand Down Expand Up @@ -99,6 +120,9 @@ ModalDialog.propTypes = {
cancelLabel: PropTypes.string.isRequired,
clearLabel: PropTypes.string.isRequired,
clearable: PropTypes.bool.isRequired,
todayLabel: PropTypes.string.isRequired,
showTodayButton: PropTypes.bool.isRequired,
onSetToday: PropTypes.func.isRequired,
};

ModalDialog.defaultProps = {
Expand Down
4 changes: 4 additions & 0 deletions lib/src/_shared/PickerBase.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,8 @@ export default class PickerBase extends PureComponent {
this.setState({ date }, this.handleAccept);
}
}

handleSetTodayDate = () => {
this.props.onChange(this.props.utils.date());
}
}
1 change: 1 addition & 0 deletions lib/src/wrappers/ModalWrapper.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export interface ModalWrapperProps extends Partial<DateTextFieldProps> {
onAccept?: () => void;
onDismiss?: () => void;
onClear?: () => void;
onSetToday?: () => void;
onOpen?: () => void;
onClose?: () => void;
dialogContentClassName?: string;
Expand Down
Loading

0 comments on commit 7b38957

Please sign in to comment.