diff --git a/README.md b/README.md index 71eea61caa..d8f25484b0 100644 --- a/README.md +++ b/README.md @@ -92,6 +92,7 @@ You can check [index.js](https://github.com/xgfe/react-native-datepicker/blob/ma | onOpenModal | - | `function` | This is called when the DatePicker Modal open. | | onCloseModal | - | `function` | This is called when the DatePicker Modal close | | onPressMask | - | `function` | This is called when clicking the ios modal mask | +| onPressMask | 'cancel' | `enum` | The enum of `cancel` and `confirm`. Use `confirm` if pressing on the mask should confirm selected date | | modalOnResponderTerminationRequest | - | `function` | Set the callback for React Native's [Gesture Responder System](https://facebook.github.io/react-native/docs/gesture-responder-system.html#responder-lifecycle)'s call to `onResponderTerminationRequest`. By default this will reject a termination request, but can be overidden in case the View under the Modal is implementing custom gesture responders, and you wish for those to be overidden in certain cases. | | TouchableComponent | `TouchableHighlight` | `Component` | Replace the `TouchableHighlight` with a custom `Component`. For example : `TouchableOpacity` | | getDateStr | - | Function | A function to override how to format the date into a `String` for display, receives a `Date` instance diff --git a/__tests__/Datepicker.test.js b/__tests__/Datepicker.test.js index 0ded2ef586..7d634204bf 100644 --- a/__tests__/Datepicker.test.js +++ b/__tests__/Datepicker.test.js @@ -147,24 +147,48 @@ describe('DatePicker', () => { expect(onCloseModal).toHaveBeenCalledTimes(1); }); - it('onPressMask', () => { - const onPressMask = jest.fn(); - const wrapper = shallow(); - const datePicker = wrapper.instance(); - - datePicker.onPressMask(); - - expect(onPressMask).toHaveBeenCalledTimes(1); - - // call onPressCancel when without onPressMask cb func - const onPressCancel = jest.fn(); - const wrapper1 = shallow(); - const datePicker1 = wrapper1.instance(); - datePicker1.onPressCancel = onPressCancel; - - datePicker1.onPressMask(); - - expect(onPressCancel).toHaveBeenCalledTimes(1); + describe('onPressMask', () => { + it('calls onPressMask if onPressMask is passed', () => { + const onPressMask = jest.fn(); + const wrapper = shallow(); + const datePicker = wrapper.instance(); + + datePicker.onPressMask(); + + expect(onPressMask).toHaveBeenCalledTimes(1); + }) + + it('calls onPressCancel if onPressMask is not passed', () => { + const onPressCancel = jest.fn(); + const wrapper = shallow(); + const datePicker = wrapper.instance(); + datePicker.onPressCancel = onPressCancel; + + datePicker.onPressMask(); + + expect(onPressCancel).toHaveBeenCalledTimes(1); + }) + + it('calls onPressCancel when onPressMaskBehaviour is set to cancel and onPressMask is not passed', () => { + const onPressCancel = jest.fn(); + const wrapper = shallow(); + const datePicker = wrapper.instance(); + datePicker.onPressCancel = onPressCancel; + + datePicker.onPressMask(); + + expect(onPressCancel).toHaveBeenCalledTimes(1); + }) + it('calls onPressConfirm when onPressMaskBehaviour is set to confirm and onPressMask is not passed', () => { + const onPressConfirm = jest.fn(); + const wrapper = shallow(); + const datePicker = wrapper.instance(); + datePicker.onPressConfirm = onPressConfirm; + + datePicker.onPressMask(); + + expect(onPressConfirm).toHaveBeenCalledTimes(1); + }) }); it('onPressConfirm', () => { diff --git a/datepicker.js b/datepicker.js index ac6cacde29..c68b29f163 100644 --- a/datepicker.js +++ b/datepicker.js @@ -93,8 +93,10 @@ class DatePicker extends Component { onPressMask() { if (typeof this.props.onPressMask === 'function') { this.props.onPressMask(); + } else if (this.props.onPressMaskBehaviour === 'confirm') { + this.onPressConfirm() } else { - this.onPressCancel(); + this.onPressCancel() } } @@ -483,6 +485,7 @@ DatePicker.propTypes = { onDateChange: PropTypes.func, onOpenModal: PropTypes.func, onCloseModal: PropTypes.func, + onPressMaskBehaviour: PropTypes.oneOf(['cancel', 'confirm']), onPressMask: PropTypes.func, placeholder: PropTypes.string, modalOnResponderTerminationRequest: PropTypes.func,