BlueWallet/helpers/prompt.ts

57 lines
1.5 KiB
TypeScript
Raw Normal View History

2020-06-15 20:47:54 +02:00
import { Platform } from 'react-native';
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> => {
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
? [
{
2021-10-16 16:37:51 +02:00
text: loc._.cancel,
onPress: () => {
reject(Error('Cancel Pressed'));
},
style: 'cancel',
2018-09-18 08:48:53 +02:00
},
{
2021-10-16 16:37:51 +02:00
text: continueButtonText,
onPress: password => {
console.log('OK Pressed');
resolve(password);
},
style: isOKDestructive ? 'destructive' : 'default',
2018-03-31 02:03:58 +02:00
},
]
: [
{
2021-10-16 16:37:51 +02:00
text: continueButtonText,
onPress: password => {
console.log('OK Pressed');
resolve(password);
},
},
];
prompt(title, text, buttons, {
2022-10-31 13:25:26 +01:00
type,
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
keyboardType,
});
2018-03-31 02:03:58 +02:00
});
};