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

Add a prop to configure whether <kbd>esc</kbd> should close the panel #106

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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ API
| minuteStep | Number | 1 | interval between minutes in picker |
| secondStep | Number | 1 | interval between seconds in picker |
| focusOnOpen | Boolean | false | automatically focus the input when the picker opens |
| closeOnEsc | Boolean | true | whether <kbd>esc</kbd> should close the picker |
| inputReadOnly | Boolean | false | set input to read only |

## Test Case
Expand Down
2 changes: 1 addition & 1 deletion src/Header.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ class Header extends Component {

onKeyDown = (e) => {
const { onEsc, onKeyDown } = this.props;
if (e.keyCode === 27) {
if (e.keyCode === 27 && onEsc) {
onEsc();
}

Expand Down
5 changes: 4 additions & 1 deletion src/Panel.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class Panel extends Component {
secondStep: PropTypes.number,
addon: PropTypes.func,
focusOnOpen: PropTypes.bool,
closeOnEsc: PropTypes.bool,
onKeyDown: PropTypes.func,
};

Expand All @@ -60,6 +61,7 @@ class Panel extends Component {
addon: noop,
onKeyDown: noop,
inputReadOnly: false,
closeOnEsc: true,
};

constructor(props) {
Expand Down Expand Up @@ -117,6 +119,7 @@ class Panel extends Component {
disabledSeconds, hideDisabledOptions, allowEmpty, showHour, showMinute, showSecond,
format, defaultOpenValue, clearText, onEsc, addon, use12Hours, onClear,
focusOnOpen, onKeyDown, hourStep, minuteStep, secondStep, inputReadOnly,
closeOnEsc,
} = this.props;
const {
value, currentSelectPanel,
Expand All @@ -143,7 +146,7 @@ class Panel extends Component {
defaultOpenValue={defaultOpenValue}
value={value}
currentSelectPanel={currentSelectPanel}
onEsc={onEsc}
onEsc={closeOnEsc ? onEsc : undefined}
format={format}
placeholder={placeholder}
hourOptions={hourOptions}
Expand Down
5 changes: 4 additions & 1 deletion src/TimePicker.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ export default class Picker extends Component {
minuteStep: PropTypes.number,
secondStep: PropTypes.number,
focusOnOpen: PropTypes.bool,
closeOnEsc: PropTypes.bool,
onKeyDown: PropTypes.func,
autoFocus: PropTypes.bool,
id: PropTypes.string,
Expand Down Expand Up @@ -86,6 +87,7 @@ export default class Picker extends Component {
addon: noop,
use12Hours: false,
focusOnOpen: false,
closeOnEsc: true,
onKeyDown: noop,
};

Expand Down Expand Up @@ -173,7 +175,7 @@ export default class Picker extends Component {
prefixCls, placeholder, disabledHours,
disabledMinutes, disabledSeconds, hideDisabledOptions, inputReadOnly,
allowEmpty, showHour, showMinute, showSecond, defaultOpenValue, clearText,
addon, use12Hours, focusOnOpen, onKeyDown, hourStep, minuteStep, secondStep,
addon, use12Hours, focusOnOpen, closeOnEsc, onKeyDown, hourStep, minuteStep, secondStep,
} = this.props;
return (
<Panel
Expand Down Expand Up @@ -202,6 +204,7 @@ export default class Picker extends Component {
secondStep={secondStep}
addon={addon}
focusOnOpen={focusOnOpen}
closeOnEsc={closeOnEsc}
onKeyDown={onKeyDown}
/>
);
Expand Down
12 changes: 8 additions & 4 deletions tests/Header.spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -335,8 +335,8 @@ describe('Header', () => {
});
});

it('exit correctly', (done) => {
const picker = renderPicker();
const closeOnEscSpec = (closeOnEsc) => (done) => {
const picker = renderPicker({ closeOnEsc });
expect(picker.state.open).not.to.be.ok();
const input = TestUtils.scryRenderedDOMComponentsWithClass(picker,
'rc-time-picker-input')[0];
Expand All @@ -359,15 +359,19 @@ describe('Header', () => {
});
setTimeout(next, 100);
}, (next) => {
expect(picker.state.open).to.be(false);
expect(picker.state.open).to.be(!closeOnEsc);
expect((header).value).to.be('01:02:03');
expect((input).value).to.be('01:02:03');

next();
}], () => {
done();
});
});
};

it('exit correctly', closeOnEscSpec(true));

it('stays open if `closeOnEsc` is `false`', closeOnEscSpec(false));

it('focus on open', (done) => {
const picker = renderPicker({
Expand Down