2017-11-03 02:09:34 +02:00
|
|
|
const DialogBox = require('react-native-dialogbox').default;
|
|
|
|
const { Keyboard } = require('react-native');
|
2017-07-13 01:01:15 +02:00
|
|
|
|
|
|
|
// Add this at the bottom of the component:
|
|
|
|
//
|
|
|
|
// <DialogBox ref={dialogbox => { this.dialogbox = dialogbox }}/>
|
|
|
|
|
2020-03-14 01:46:14 +02:00
|
|
|
const dialogs = {};
|
2017-07-13 01:01:15 +02:00
|
|
|
|
2021-09-10 20:05:47 +02:00
|
|
|
dialogs.confirmRef = (ref, message) => {
|
|
|
|
if (!ref) throw new Error('ref is required');
|
2017-07-13 01:01:15 +02:00
|
|
|
|
2019-09-13 00:16:42 +02:00
|
|
|
return new Promise((resolve) => {
|
2017-07-15 18:25:33 +02:00
|
|
|
Keyboard.dismiss();
|
|
|
|
|
2021-09-10 20:05:47 +02:00
|
|
|
ref.confirm({
|
2017-07-13 01:01:15 +02:00
|
|
|
content: message,
|
2019-07-29 15:43:53 +02:00
|
|
|
|
2017-07-13 01:01:15 +02:00
|
|
|
ok: {
|
|
|
|
callback: () => {
|
|
|
|
resolve(true);
|
2019-07-29 15:43:53 +02:00
|
|
|
},
|
2017-07-13 01:01:15 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
cancel: {
|
|
|
|
callback: () => {
|
|
|
|
resolve(false);
|
2019-07-29 15:43:53 +02:00
|
|
|
},
|
2017-07-13 01:01:15 +02:00
|
|
|
},
|
|
|
|
});
|
2017-07-15 18:25:33 +02:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2021-09-10 20:05:47 +02:00
|
|
|
dialogs.confirm = (parentComponent, message) => {
|
|
|
|
if (!parentComponent) throw new Error('parentComponent is required');
|
|
|
|
if (!('dialogbox' in parentComponent)) throw new Error('A "dialogbox" component must be defined on the parent component!');
|
|
|
|
|
|
|
|
return dialogs.confirmRef(parentComponent.dialogBox, message);
|
|
|
|
};
|
|
|
|
|
2018-02-22 20:58:15 +02:00
|
|
|
dialogs.pop = (parentComponent, message, buttons, options = null) => {
|
2017-11-23 20:41:35 +02:00
|
|
|
if (!parentComponent) throw new Error('parentComponent is required');
|
|
|
|
if (!('dialogbox' in parentComponent)) throw new Error('A "dialogbox" component must be defined on the parent component!');
|
2017-08-24 20:10:03 +02:00
|
|
|
|
2018-02-22 20:58:15 +02:00
|
|
|
if (!options) options = {};
|
|
|
|
if (!('buttonFlow' in options)) options.buttonFlow = 'auto';
|
|
|
|
|
2019-09-13 00:16:42 +02:00
|
|
|
return new Promise((resolve) => {
|
2017-08-24 20:10:03 +02:00
|
|
|
Keyboard.dismiss();
|
|
|
|
|
2020-03-14 01:46:14 +02:00
|
|
|
const btns = [];
|
2017-08-24 20:10:03 +02:00
|
|
|
for (let i = 0; i < buttons.length; i++) {
|
|
|
|
btns.push({
|
2018-02-22 20:58:15 +02:00
|
|
|
text: buttons[i].text,
|
2017-08-24 20:10:03 +02:00
|
|
|
callback: () => {
|
|
|
|
parentComponent.dialogbox.close();
|
|
|
|
resolve(buttons[i].id);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
parentComponent.dialogbox.pop({
|
2019-07-29 15:43:53 +02:00
|
|
|
content: message,
|
2017-08-24 20:10:03 +02:00
|
|
|
btns: btns,
|
2018-02-22 20:58:15 +02:00
|
|
|
buttonFlow: options.buttonFlow,
|
2017-08-24 20:10:03 +02:00
|
|
|
});
|
|
|
|
});
|
2019-07-29 15:43:53 +02:00
|
|
|
};
|
2017-08-24 20:10:03 +02:00
|
|
|
|
2017-07-15 18:25:33 +02:00
|
|
|
dialogs.error = (parentComponent, message) => {
|
|
|
|
Keyboard.dismiss();
|
|
|
|
return parentComponent.dialogbox.alert(message);
|
2019-07-29 15:43:53 +02:00
|
|
|
};
|
2017-07-13 01:01:15 +02:00
|
|
|
|
2018-03-27 01:55:44 +02:00
|
|
|
dialogs.info = (parentComponent, message) => {
|
|
|
|
Keyboard.dismiss();
|
|
|
|
return parentComponent.dialogbox.alert(message);
|
2019-07-29 15:43:53 +02:00
|
|
|
};
|
2018-03-27 01:55:44 +02:00
|
|
|
|
2019-07-29 15:43:53 +02:00
|
|
|
dialogs.DialogBox = DialogBox;
|
2017-07-15 18:25:33 +02:00
|
|
|
|
2019-07-29 15:43:53 +02:00
|
|
|
module.exports = { dialogs };
|