1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-21 09:38:01 +02:00
joplin/packages/app-mobile/utils/dialogs.js

83 lines
1.9 KiB
JavaScript
Raw Normal View History

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 }}/>
const dialogs = {};
2017-07-13 01:01:15 +02:00
dialogs.confirmRef = (ref, message) => {
if (!ref) throw new Error('ref is required');
2017-07-13 01:01:15 +02:00
return new Promise((resolve) => {
2017-07-15 18:25:33 +02:00
Keyboard.dismiss();
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
});
};
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!');
2021-09-19 16:27:23 +02:00
return dialogs.confirmRef(parentComponent.dialogbox, message);
};
dialogs.pop = (parentComponent, message, buttons, options = null) => {
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!');
if (!options) options = {};
if (!('buttonFlow' in options)) options.buttonFlow = 'auto';
return new Promise((resolve) => {
Keyboard.dismiss();
const btns = [];
for (let i = 0; i < buttons.length; i++) {
btns.push({
text: buttons[i].text,
callback: () => {
parentComponent.dialogbox.close();
resolve(buttons[i].id);
},
});
}
parentComponent.dialogbox.pop({
2019-07-29 15:43:53 +02:00
content: message,
btns: btns,
buttonFlow: options.buttonFlow,
});
});
2019-07-29 15:43:53 +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 };