2020-06-15 20:47:54 +02:00
|
|
|
import { Platform } from 'react-native';
|
2018-12-11 23:52:46 +01:00
|
|
|
import prompt from 'react-native-prompt-android';
|
2021-10-16 16:37:51 +02:00
|
|
|
import loc from '../loc';
|
2018-03-31 02:03:58 +02:00
|
|
|
|
2022-09-05 20:34:02 +02:00
|
|
|
module.exports = (
|
|
|
|
title: string,
|
|
|
|
text: string,
|
|
|
|
isCancelable = true,
|
|
|
|
type: PromptType | PromptTypeIOS | PromptTypeAndroid = 'secure-text',
|
|
|
|
isOKDestructive = false,
|
|
|
|
continueButtonText = loc._.ok,
|
|
|
|
): Promise<string> => {
|
2020-10-20 10:20:52 +02:00
|
|
|
const keyboardType = type === 'numeric' ? 'numeric' : 'default';
|
|
|
|
|
2020-06-15 20:47:54 +02:00
|
|
|
if (Platform.OS === 'ios' && type === 'numeric') {
|
|
|
|
// `react-native-prompt-android` on ios does not support numeric input
|
|
|
|
type = 'plain-text';
|
|
|
|
}
|
|
|
|
|
2018-09-18 08:48:53 +02:00
|
|
|
return new Promise((resolve, reject) => {
|
2022-09-05 20:34:02 +02:00
|
|
|
const buttons: Array<PromptButton> = isCancelable
|
2018-10-09 06:25:36 +02:00
|
|
|
? [
|
|
|
|
{
|
2021-10-16 16:37:51 +02:00
|
|
|
text: loc._.cancel,
|
2018-10-09 06:25:36 +02:00
|
|
|
onPress: () => {
|
|
|
|
reject(Error('Cancel Pressed'));
|
|
|
|
},
|
|
|
|
style: 'cancel',
|
2018-09-18 08:48:53 +02:00
|
|
|
},
|
2018-10-09 06:25:36 +02:00
|
|
|
{
|
2021-10-16 16:37:51 +02:00
|
|
|
text: continueButtonText,
|
2018-10-09 06:25:36 +02:00
|
|
|
onPress: password => {
|
2020-10-20 10:20:52 +02:00
|
|
|
console.log('OK Pressed');
|
2018-10-09 06:25:36 +02:00
|
|
|
resolve(password);
|
|
|
|
},
|
2021-02-25 03:21:25 +01:00
|
|
|
style: isOKDestructive ? 'destructive' : 'default',
|
2018-03-31 02:03:58 +02:00
|
|
|
},
|
2018-10-09 06:25:36 +02:00
|
|
|
]
|
|
|
|
: [
|
|
|
|
{
|
2021-10-16 16:37:51 +02:00
|
|
|
text: continueButtonText,
|
2018-10-09 06:25:36 +02:00
|
|
|
onPress: password => {
|
2020-10-20 10:20:52 +02:00
|
|
|
console.log('OK Pressed');
|
2018-10-09 06:25:36 +02:00
|
|
|
resolve(password);
|
|
|
|
},
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
2018-12-11 23:52:46 +01:00
|
|
|
prompt(title, text, buttons, {
|
2022-10-31 13:25:26 +01:00
|
|
|
type,
|
2018-12-11 23:52:46 +01:00
|
|
|
cancelable: isCancelable,
|
2022-09-05 20:34:02 +02:00
|
|
|
// @ts-ignore suppressed because its supported only on ios and is absent from type definitions
|
2020-10-20 10:20:52 +02:00
|
|
|
keyboardType,
|
2018-12-11 23:52:46 +01:00
|
|
|
});
|
2018-03-31 02:03:58 +02:00
|
|
|
});
|
|
|
|
};
|