2018-05-15 17:59:36 +00:00
|
|
|
// @flow
|
2020-02-11 11:25:52 +00:00
|
|
|
import { NativeModules } from 'react-native';
|
|
|
|
import processColor from 'react-native/Libraries/StyleSheet/processColor';
|
2015-11-01 17:28:44 +00:00
|
|
|
|
2020-02-11 11:25:52 +00:00
|
|
|
import type { ColorValue } from 'react-native/Libraries/StyleSheet/StyleSheetTypes';
|
2018-05-15 17:59:36 +00:00
|
|
|
|
|
|
|
type IdKey = string | 'id';
|
|
|
|
type LabelKey = string | 'label';
|
2020-02-11 11:25:52 +00:00
|
|
|
type ListItem = { label: string, id?: any };
|
2018-05-15 17:59:36 +00:00
|
|
|
|
2018-05-31 05:10:29 +00:00
|
|
|
type OptionsAlert = {|
|
2020-02-11 11:25:52 +00:00
|
|
|
...OptionsCommon,
|
|
|
|
|};
|
2018-05-31 05:10:29 +00:00
|
|
|
|
2018-05-15 17:59:36 +00:00
|
|
|
type OptionsCommon = {|
|
2020-02-11 11:25:52 +00:00
|
|
|
title?: null | string,
|
|
|
|
titleColor?: ColorValue,
|
|
|
|
content?: null | string,
|
|
|
|
contentIsHtml?: boolean,
|
|
|
|
contentColor?: string,
|
|
|
|
positiveText?: string, // default "OK"
|
|
|
|
negativeText?: string,
|
|
|
|
neutralText?: string,
|
|
|
|
positiveColor?: ColorValue,
|
|
|
|
negativeColor?: ColorValue,
|
|
|
|
neutralColor?: ColorValue,
|
|
|
|
backgroundColor?: ColorValue,
|
|
|
|
cancelable?: boolean,
|
|
|
|
linkColor?: ColorValue, // applies if contentIsHtml is true, and there are <a> elements in content string
|
|
|
|
forceStacking?: boolean,
|
|
|
|
checkboxLabel?: string,
|
|
|
|
checkboxDefaultValue?: boolean,
|
|
|
|
|};
|
|
|
|
|
|
|
|
type ListItemJustLabel = { label: string };
|
|
|
|
type ListItemJustId = { id: string };
|
|
|
|
type ListItemFull = { label: string, id: any };
|
2018-05-15 17:59:36 +00:00
|
|
|
type ListItemBare = {};
|
|
|
|
|
2019-03-21 00:41:52 +00:00
|
|
|
type OptionsRadioList = {|
|
2020-02-11 11:25:52 +00:00
|
|
|
maxNumberOfItems?: number,
|
|
|
|
type: typeof ListType.listRadio,
|
|
|
|
widgetColor?: ColorValue, // radio color
|
|
|
|
|};
|
2019-03-21 00:41:52 +00:00
|
|
|
type OptionsCheckboxList = {|
|
2020-02-11 11:25:52 +00:00
|
|
|
maxNumberOfItems?: number,
|
|
|
|
type: typeof ListType.listCheckbox,
|
|
|
|
neutralIsClear?: boolean,
|
|
|
|
widgetColor?: ColorValue, // checkbox color
|
|
|
|
|};
|
|
|
|
|
|
|
|
type OptionsPicker =
|
|
|
|
| {|
|
|
|
|
...OptionsCommon,
|
|
|
|
type?: typeof ListType.listPlain,
|
|
|
|
maxNumberOfItems?: number,
|
|
|
|
items: ListItemJustLabel[],
|
|
|
|
|}
|
|
|
|
| {|
|
|
|
|
...OptionsCommon,
|
|
|
|
type?: typeof ListType.listPlain,
|
|
|
|
maxNumberOfItems?: number,
|
|
|
|
items: ListItemBare[],
|
|
|
|
labelKey: string,
|
|
|
|
|}
|
|
|
|
| {|
|
|
|
|
// radio - no preselected
|
|
|
|
...OptionsCommon,
|
|
|
|
...OptionsRadioList,
|
|
|
|
items: ListItemJustLabel[],
|
|
|
|
|}
|
|
|
|
| {|
|
|
|
|
// radio - no preselected
|
|
|
|
...OptionsCommon,
|
|
|
|
...OptionsRadioList,
|
|
|
|
items: ListItemBare[],
|
|
|
|
labelKey: string,
|
|
|
|
|}
|
|
|
|
| {|
|
|
|
|
// radio - preselected - ListItemFull
|
|
|
|
...OptionsCommon,
|
|
|
|
...OptionsRadioList,
|
|
|
|
items: ListItemFull[],
|
|
|
|
selectedId: any,
|
|
|
|
|}
|
|
|
|
| {|
|
|
|
|
// radio - preselected - ListItemJustlabel
|
|
|
|
...OptionsCommon,
|
|
|
|
...OptionsRadioList,
|
|
|
|
items: ListItemJustLabel[],
|
|
|
|
idKey: string,
|
|
|
|
selectedId: any,
|
|
|
|
|}
|
|
|
|
| {|
|
|
|
|
// radio - preselected - ListItemJustId
|
|
|
|
...OptionsCommon,
|
|
|
|
...OptionsRadioList,
|
|
|
|
items: ListItemJustId[],
|
|
|
|
labelKey: string,
|
|
|
|
selectedId: any,
|
|
|
|
|}
|
|
|
|
| {|
|
|
|
|
// radio - preselected - ListItemBare
|
|
|
|
...OptionsCommon,
|
|
|
|
...OptionsRadioList,
|
|
|
|
items: ListItemBare[],
|
|
|
|
idKey: string,
|
|
|
|
labelKey: string,
|
|
|
|
selectedId: any,
|
|
|
|
|}
|
|
|
|
| {|
|
|
|
|
// checklist - no preselected - ListItemJustLabel
|
|
|
|
...OptionsCommon,
|
|
|
|
...OptionsCheckboxList,
|
|
|
|
items: ListItemJustLabel[],
|
|
|
|
|}
|
|
|
|
| {|
|
|
|
|
// checklist - no preselected - ListItemBare
|
|
|
|
...OptionsCommon,
|
|
|
|
...OptionsCheckboxList,
|
|
|
|
items: ListItemBare[],
|
|
|
|
labelKey: string,
|
|
|
|
|}
|
|
|
|
| {|
|
|
|
|
// checklist - preselected - ListItemFull
|
|
|
|
...OptionsCommon,
|
|
|
|
...OptionsCheckboxList,
|
|
|
|
items: ListItemFull[],
|
|
|
|
selectedIds: any[],
|
|
|
|
|}
|
|
|
|
| {|
|
|
|
|
// checklist - preselected - ListItemJustlabel
|
|
|
|
...OptionsCommon,
|
|
|
|
...OptionsCheckboxList,
|
|
|
|
items: ListItemJustLabel[],
|
|
|
|
idKey: string,
|
|
|
|
selectedIds: any,
|
|
|
|
|}
|
|
|
|
| {|
|
|
|
|
// checklist - preselected - ListItemJustId
|
|
|
|
...OptionsCommon,
|
|
|
|
...OptionsCheckboxList,
|
|
|
|
items: ListItemJustId[],
|
|
|
|
labelKey: string,
|
|
|
|
selectedIds: any,
|
|
|
|
|}
|
|
|
|
| {|
|
|
|
|
// checklist - preselected - ListItemBare
|
|
|
|
...OptionsCommon,
|
|
|
|
...OptionsCheckboxList,
|
|
|
|
items: ListItemBare[],
|
|
|
|
idKey: string,
|
|
|
|
labelKey: string,
|
|
|
|
selectedIds: any,
|
|
|
|
|};
|
2018-05-15 17:59:36 +00:00
|
|
|
|
|
|
|
type ListType =
|
|
|
|
| typeof DialogAndroid.listCheckbox
|
|
|
|
| typeof DialogAndroid.listPlain
|
|
|
|
| typeof DialogAndroid.listRadio;
|
|
|
|
|
|
|
|
type ActionType =
|
|
|
|
| typeof DialogAndroid.actionDismiss
|
|
|
|
| typeof DialogAndroid.actionNegative
|
|
|
|
| typeof DialogAndroid.actionNeutral
|
|
|
|
| typeof DialogAndroid.actionPositive
|
|
|
|
| typeof DialogAndroid.actionSelect;
|
|
|
|
|
2019-03-21 00:41:52 +00:00
|
|
|
type Options = OptionsAlert | OptionsPicker | OptionsProgress | OptionsPrompt;
|
2018-05-15 17:59:36 +00:00
|
|
|
|
|
|
|
type OptionsProgress = {|
|
2020-02-11 11:25:52 +00:00
|
|
|
contentColor?: $PropertyType<OptionsCommon, 'contentColor'>,
|
|
|
|
contentIsHtml?: $PropertyType<OptionsCommon, 'contentIsHtml'>,
|
|
|
|
linkColor?: $PropertyType<OptionsCommon, 'linkColor'>,
|
|
|
|
style?: ProgressStyle,
|
|
|
|
title?: $PropertyType<OptionsCommon, 'title'>,
|
|
|
|
titleColor?: $PropertyType<OptionsCommon, 'titleColor'>,
|
|
|
|
widgetColor?: $PropertyType<OptionsCommon, 'widgetColor'>,
|
|
|
|
|};
|
2018-05-15 17:59:36 +00:00
|
|
|
|
|
|
|
type ProgressStyle = typeof DialogAndroid.progressHorizontal;
|
|
|
|
|
|
|
|
type OptionsPrompt = {|
|
2020-02-11 11:25:52 +00:00
|
|
|
...OptionsCommon,
|
|
|
|
keyboardType?:
|
|
|
|
| 'numeric'
|
|
|
|
| 'number-pad'
|
|
|
|
| 'decimal-pad'
|
|
|
|
| 'numeric-password'
|
|
|
|
| 'email-address'
|
|
|
|
| 'password'
|
|
|
|
| 'phone-pad'
|
|
|
|
| 'url',
|
|
|
|
defaultValue?: string,
|
|
|
|
placeholder?: string,
|
|
|
|
allowEmptyInput?: boolean,
|
|
|
|
minLength?: number,
|
|
|
|
maxLength?: number,
|
|
|
|
|};
|
2015-11-01 17:28:44 +00:00
|
|
|
|
2018-05-15 17:59:36 +00:00
|
|
|
type Title = void | null | string;
|
|
|
|
type Content = void | null | string;
|
2015-11-01 17:28:44 +00:00
|
|
|
|
2018-05-15 17:59:36 +00:00
|
|
|
type NativeConfig = {|
|
2020-02-11 11:25:52 +00:00
|
|
|
...OptionsCommon,
|
|
|
|
items: string[],
|
|
|
|
widgetColor?: ColorValue,
|
|
|
|
selectedIndices?: number[],
|
|
|
|
selectedIndex?: number[],
|
|
|
|
progress?: {
|
|
|
|
indeterminate: true,
|
|
|
|
style?: 'horizontal',
|
|
|
|
},
|
|
|
|
|};
|
2015-11-01 17:28:44 +00:00
|
|
|
|
2018-05-15 17:59:36 +00:00
|
|
|
function processColors(nativeConfig: {}) {
|
2020-02-11 11:25:52 +00:00
|
|
|
for (const prop of Object.keys(nativeConfig)) {
|
|
|
|
if (prop.endsWith('Color')) {
|
|
|
|
nativeConfig[prop] = processColor(nativeConfig[prop]);
|
2015-11-01 17:28:44 +00:00
|
|
|
}
|
2020-02-11 11:25:52 +00:00
|
|
|
}
|
2018-05-15 17:59:36 +00:00
|
|
|
}
|
2015-11-01 17:28:44 +00:00
|
|
|
|
2018-05-15 17:59:36 +00:00
|
|
|
function pick(source, ...keys) {
|
2020-02-11 11:25:52 +00:00
|
|
|
const target = {};
|
|
|
|
for (const key of keys) {
|
|
|
|
if (source.hasOwnProperty(key)) {
|
|
|
|
target[key] = source[key];
|
2015-11-01 17:28:44 +00:00
|
|
|
}
|
2020-02-11 11:25:52 +00:00
|
|
|
}
|
|
|
|
return target;
|
2018-05-15 17:59:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
class DialogAndroid {
|
2020-02-11 11:25:52 +00:00
|
|
|
static listPlain = 'listPlain';
|
|
|
|
static listRadio = 'listRadio';
|
|
|
|
static listCheckbox = 'listCheckbox';
|
|
|
|
static actionDismiss = 'actionDismiss';
|
|
|
|
static actionNegative = 'actionNegative';
|
|
|
|
static actionNeutral = 'actionNeutral';
|
|
|
|
static actionPositive = 'actionPositive';
|
|
|
|
static actionSelect = 'actionSelect';
|
|
|
|
static progressHorizontal = 'progressHorizontal';
|
|
|
|
|
|
|
|
static defaults = {
|
|
|
|
positiveText: 'OK',
|
|
|
|
};
|
|
|
|
|
|
|
|
static dismiss(): void {
|
|
|
|
NativeModules.DialogAndroid.dismiss();
|
|
|
|
}
|
|
|
|
|
|
|
|
static assignDefaults(defaults: {
|
|
|
|
title?: Title,
|
|
|
|
content?: Content,
|
|
|
|
...Options,
|
|
|
|
}): void {
|
|
|
|
Object.assign(DialogAndroid.defaults, defaults);
|
|
|
|
}
|
|
|
|
|
|
|
|
static alert(
|
|
|
|
title: Title,
|
|
|
|
content: Content,
|
|
|
|
options?: OptionsAlert = {}
|
|
|
|
): Promise<
|
|
|
|
| {|
|
|
|
|
action:
|
|
|
|
| typeof DialogAndroid.actionPositive
|
|
|
|
| typeof DialogAndroid.actionNegative
|
|
|
|
| typeof DialogAndroid.actionNeutral
|
|
|
|
| typeof DialogAndroid.actionDismiss,
|
|
|
|
|}
|
|
|
|
| {|
|
|
|
|
action:
|
|
|
|
| typeof DialogAndroid.actionPositive
|
|
|
|
| typeof DialogAndroid.actionNegative
|
|
|
|
| typeof DialogAndroid.actionNeutral,
|
|
|
|
checked: boolean,
|
|
|
|
|}
|
|
|
|
> {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const nativeConfig: NativeConfig = {
|
|
|
|
...DialogAndroid.defaults,
|
|
|
|
...options,
|
|
|
|
onAny: true,
|
|
|
|
dismissListener: true,
|
|
|
|
};
|
|
|
|
if (title) nativeConfig.title = title;
|
|
|
|
if (content) nativeConfig.content = content;
|
|
|
|
|
|
|
|
processColors(nativeConfig);
|
|
|
|
|
|
|
|
NativeModules.DialogAndroid.show(
|
|
|
|
nativeConfig,
|
|
|
|
(kind: string, ...rest) => {
|
|
|
|
switch (kind) {
|
|
|
|
case 'error': {
|
|
|
|
const [error, nativeConfig] = rest;
|
|
|
|
return reject(
|
|
|
|
`DialogAndroid ${error}. nativeConfig: ${nativeConfig}`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
case 'dismissListener': {
|
|
|
|
return resolve({
|
|
|
|
action: DialogAndroid.actionDismiss,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
case 'onAny': {
|
|
|
|
const [dialogAction, checked] = rest;
|
|
|
|
switch (dialogAction) {
|
|
|
|
case 0:
|
|
|
|
return resolve({
|
|
|
|
action: DialogAndroid.actionPositive,
|
|
|
|
...getChecked(nativeConfig, checked),
|
|
|
|
});
|
|
|
|
case 1:
|
|
|
|
return resolve({
|
|
|
|
action: DialogAndroid.actionNeutral,
|
|
|
|
...getChecked(nativeConfig, checked),
|
|
|
|
});
|
|
|
|
case 2:
|
|
|
|
return resolve({
|
|
|
|
action: DialogAndroid.actionNegative,
|
|
|
|
...getChecked(nativeConfig, checked),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
default: {
|
|
|
|
return reject(`Unknown callback kind: "${kind}"`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
static showPicker(
|
|
|
|
title: Title,
|
|
|
|
content: Content,
|
|
|
|
options: OptionsPicker
|
|
|
|
): Promise<
|
|
|
|
| {|
|
|
|
|
action:
|
|
|
|
| typeof DialogAndroid.actionNegative
|
|
|
|
| typeof DialogAndroid.actionNeutral
|
|
|
|
| typeof DialogAndroid.actionDismiss,
|
|
|
|
|}
|
|
|
|
| {|
|
|
|
|
action:
|
|
|
|
| typeof DialogAndroid.actionNegative
|
|
|
|
| typeof DialogAndroid.actionNeutral,
|
|
|
|
checked: boolean,
|
|
|
|
|}
|
|
|
|
| {|
|
|
|
|
action: typeof DialogAndroid.actionSelect,
|
|
|
|
selectedItem: ListItem,
|
|
|
|
|}
|
|
|
|
| {|
|
|
|
|
action: typeof DialogAndroid.actionSelect,
|
|
|
|
selectedItem: ListItem,
|
|
|
|
checked: boolean,
|
|
|
|
|}
|
|
|
|
| {|
|
|
|
|
action: typeof DialogAndroid.actionSelect,
|
|
|
|
selectedItems: ListItem[],
|
|
|
|
|}
|
|
|
|
| {|
|
|
|
|
action: typeof DialogAndroid.actionSelect,
|
|
|
|
selectedItems: ListItem[],
|
|
|
|
checked: boolean,
|
|
|
|
|}
|
|
|
|
> {
|
|
|
|
// options is required, must defined items
|
|
|
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const {
|
|
|
|
idKey = 'id',
|
|
|
|
items,
|
|
|
|
labelKey = 'label',
|
|
|
|
type,
|
|
|
|
neutralIsClear,
|
|
|
|
selectedId,
|
|
|
|
selectedIds,
|
|
|
|
...filteredOptions
|
|
|
|
} = options;
|
|
|
|
|
|
|
|
const nativeConfig: NativeConfig = {
|
|
|
|
...DialogAndroid.defaults,
|
|
|
|
...filteredOptions,
|
|
|
|
onAny: true,
|
|
|
|
dismissListener: true,
|
|
|
|
};
|
|
|
|
if (title) nativeConfig.title = title;
|
|
|
|
if (content) nativeConfig.content = content;
|
|
|
|
|
|
|
|
if (items) {
|
|
|
|
nativeConfig.items = items.map(item => item[labelKey]);
|
|
|
|
switch (type) {
|
|
|
|
case DialogAndroid.listCheckbox: {
|
|
|
|
nativeConfig.itemsCallbackMultiChoice = true;
|
|
|
|
if (selectedIds) {
|
|
|
|
nativeConfig.selectedIndices = selectedIds.map(id =>
|
|
|
|
items.findIndex(item => item[idKey] === id)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case DialogAndroid.listRadio: {
|
|
|
|
nativeConfig.itemsCallbackSingleChoice = true;
|
|
|
|
if (selectedId !== undefined) {
|
|
|
|
nativeConfig.selectedIndex = items.findIndex(
|
|
|
|
item => item[idKey] === selectedId
|
|
|
|
);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
nativeConfig.itemsCallback = true;
|
|
|
|
}
|
|
|
|
}
|
2018-05-15 17:59:36 +00:00
|
|
|
|
2020-02-11 11:25:52 +00:00
|
|
|
if (neutralIsClear) nativeConfig.multiChoiceClearButton = true;
|
2018-05-15 17:59:36 +00:00
|
|
|
|
2020-02-11 11:25:52 +00:00
|
|
|
processColors(nativeConfig);
|
2018-05-15 17:59:36 +00:00
|
|
|
|
2020-02-11 11:25:52 +00:00
|
|
|
NativeModules.DialogAndroid.show(
|
|
|
|
nativeConfig,
|
|
|
|
(kind: string, ...rest) => {
|
|
|
|
switch (kind) {
|
|
|
|
case 'error': {
|
|
|
|
const [error, nativeConfig] = rest;
|
|
|
|
return reject(
|
|
|
|
`DialogAndroid ${error}. nativeConfig: ${nativeConfig}`
|
|
|
|
);
|
2018-05-15 17:59:36 +00:00
|
|
|
}
|
2020-02-11 11:25:52 +00:00
|
|
|
case 'itemsCallbackMultiChoice': {
|
|
|
|
const [selectedIndicesString, checked] = rest; // blank string when nothing selected
|
|
|
|
const selectedItems =
|
|
|
|
selectedIndicesString === ''
|
|
|
|
? []
|
|
|
|
: selectedIndicesString.split(',').map(index => items[index]);
|
|
|
|
|
|
|
|
return resolve({
|
|
|
|
action: DialogAndroid.actionPositive,
|
|
|
|
selectedItems,
|
|
|
|
...getChecked(nativeConfig, checked),
|
|
|
|
});
|
2018-05-15 17:59:36 +00:00
|
|
|
}
|
2020-02-11 11:25:52 +00:00
|
|
|
case 'itemsCallback':
|
|
|
|
case 'itemsCallbackSingleChoice': {
|
|
|
|
const [selectedIndex, checked] = rest;
|
|
|
|
const selectedItem = items[selectedIndex];
|
|
|
|
return resolve({
|
|
|
|
action: DialogAndroid.actionSelect,
|
|
|
|
selectedItem,
|
|
|
|
...getChecked(nativeConfig, checked),
|
|
|
|
});
|
2018-05-15 17:59:36 +00:00
|
|
|
}
|
2020-02-11 11:25:52 +00:00
|
|
|
case 'onAny': {
|
|
|
|
const [dialogAction, checked] = rest;
|
|
|
|
switch (dialogAction) {
|
|
|
|
case 0:
|
|
|
|
return resolve({
|
|
|
|
action: DialogAndroid.actionPositive,
|
|
|
|
...getChecked(nativeConfig, checked),
|
|
|
|
});
|
|
|
|
case 1:
|
|
|
|
return resolve({
|
|
|
|
action: DialogAndroid.actionNeutral,
|
|
|
|
...getChecked(nativeConfig, checked),
|
|
|
|
});
|
|
|
|
case 2:
|
|
|
|
return resolve({
|
|
|
|
action: DialogAndroid.actionNegative,
|
|
|
|
...getChecked(nativeConfig, checked),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case 'dismissListener': {
|
|
|
|
return resolve({
|
|
|
|
action: DialogAndroid.actionDismiss,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
default: {
|
|
|
|
return reject(`Unknown callback kind: "${kind}"`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
static showProgress(
|
|
|
|
content: string,
|
|
|
|
options?: OptionsProgress = {}
|
|
|
|
): Promise<{|
|
|
|
|
action: typeof DialogAndroid.actionDismiss,
|
|
|
|
|}> {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const defaults = pick(
|
|
|
|
DialogAndroid.defaults,
|
|
|
|
'contentColor',
|
|
|
|
'contentIsHtml',
|
|
|
|
'linkColor',
|
|
|
|
'title',
|
|
|
|
'widgetColor',
|
|
|
|
'titleColor'
|
|
|
|
);
|
|
|
|
|
|
|
|
const { style, ...finalOptions } = options;
|
|
|
|
|
|
|
|
const nativeConfig = {
|
|
|
|
...defaults,
|
|
|
|
progress: {
|
|
|
|
indeterminate: true,
|
|
|
|
style:
|
|
|
|
style === DialogAndroid.progressHorizontal
|
|
|
|
? 'horizontal'
|
|
|
|
: undefined,
|
|
|
|
},
|
|
|
|
cancelable: false,
|
|
|
|
...finalOptions,
|
|
|
|
dismissListener: true,
|
|
|
|
};
|
|
|
|
if (content) nativeConfig.content = content;
|
|
|
|
if (content && style !== DialogAndroid.progressHorizontal)
|
|
|
|
nativeConfig.content = ' ' + content;
|
|
|
|
processColors(nativeConfig);
|
|
|
|
|
|
|
|
NativeModules.DialogAndroid.show(
|
|
|
|
nativeConfig,
|
|
|
|
(kind: string, ...rest) => {
|
|
|
|
switch (kind) {
|
|
|
|
case 'error': {
|
|
|
|
const [error, nativeConfig] = rest;
|
|
|
|
return reject(
|
|
|
|
`DialogAndroid ${error}. nativeConfig: ${nativeConfig}`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
case 'dismissListener': {
|
|
|
|
return resolve({
|
|
|
|
action: DialogAndroid.actionDismiss,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
static prompt(
|
|
|
|
title: Title,
|
|
|
|
content: Content,
|
|
|
|
options?: OptionsPrompt = {}
|
|
|
|
): Promise<
|
|
|
|
| {|
|
|
|
|
action:
|
|
|
|
| typeof DialogAndroid.actionNegative
|
|
|
|
| typeof DialogAndroid.actionNeutral
|
|
|
|
| typeof DialogAndroid.actionDismiss,
|
|
|
|
|}
|
|
|
|
| {|
|
|
|
|
action:
|
|
|
|
| typeof DialogAndroid.actionNegative
|
|
|
|
| typeof DialogAndroid.actionNeutral,
|
|
|
|
checked: boolean,
|
|
|
|
|}
|
|
|
|
| {| action: typeof DialogAndroid.actionPositive, text: string |}
|
|
|
|
| {|
|
|
|
|
action: typeof DialogAndroid.actionPositive,
|
|
|
|
text: string,
|
|
|
|
checked: boolean,
|
|
|
|
|}
|
|
|
|
> {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const {
|
|
|
|
keyboardType,
|
|
|
|
defaultValue,
|
|
|
|
placeholder,
|
|
|
|
allowEmptyInput,
|
|
|
|
minLength,
|
|
|
|
maxLength,
|
|
|
|
...finalOptions
|
|
|
|
} = options;
|
|
|
|
|
|
|
|
const inputConfig = {};
|
|
|
|
if (defaultValue) inputConfig.prefill = defaultValue;
|
|
|
|
if (placeholder) inputConfig.hint = placeholder;
|
|
|
|
if (allowEmptyInput !== undefined)
|
|
|
|
inputConfig.allowEmptyInput = allowEmptyInput;
|
|
|
|
if (minLength) inputConfig.minLength = minLength;
|
|
|
|
if (maxLength) inputConfig.maxLength = maxLength;
|
|
|
|
if (keyboardType) inputConfig.keyboardType = keyboardType;
|
|
|
|
|
|
|
|
const nativeConfig = {
|
|
|
|
...DialogAndroid.defaults,
|
|
|
|
input: inputConfig,
|
|
|
|
...finalOptions,
|
|
|
|
onAny: true,
|
|
|
|
dismissListener: true,
|
|
|
|
};
|
|
|
|
if (title) nativeConfig.title = title;
|
|
|
|
if (content) nativeConfig.content = content;
|
|
|
|
|
|
|
|
processColors(nativeConfig);
|
|
|
|
|
|
|
|
NativeModules.DialogAndroid.show(
|
|
|
|
nativeConfig,
|
|
|
|
(kind: string, ...rest) => {
|
|
|
|
switch (kind) {
|
|
|
|
case 'error': {
|
|
|
|
const [error, nativeConfig] = rest;
|
|
|
|
return reject(
|
|
|
|
`DialogAndroid ${error}. nativeConfig: ${nativeConfig}`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
case 'onAny': {
|
|
|
|
const [dialogAction, checked] = rest;
|
|
|
|
switch (dialogAction) {
|
|
|
|
case 1:
|
|
|
|
return resolve({
|
|
|
|
action: DialogAndroid.actionNeutral,
|
|
|
|
...getChecked(nativeConfig, checked),
|
|
|
|
});
|
|
|
|
case 2:
|
|
|
|
return resolve({
|
|
|
|
action: DialogAndroid.actionNegative,
|
|
|
|
...getChecked(nativeConfig, checked),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case 'input': {
|
|
|
|
const [text, checked] = rest;
|
|
|
|
return resolve({
|
|
|
|
action: DialogAndroid.actionPositive,
|
|
|
|
text,
|
|
|
|
...getChecked(nativeConfig, checked),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
case 'dismissListener': {
|
|
|
|
return resolve({
|
|
|
|
action: DialogAndroid.actionDismiss,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
case 'cancelListener': {
|
|
|
|
// fires when input text field is there and hit back or in back to dismiss
|
|
|
|
return resolve({
|
|
|
|
action: DialogAndroid.actionDismiss,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
default: {
|
|
|
|
return reject(`Unknown callback kind: "${kind}"`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
2015-11-01 17:28:44 +00:00
|
|
|
}
|
|
|
|
|
2018-05-31 05:10:29 +00:00
|
|
|
function getChecked(nativeConfig, checked) {
|
2020-02-11 11:25:52 +00:00
|
|
|
return nativeConfig.checkboxLabel ? { checked } : {};
|
2018-05-31 05:10:29 +00:00
|
|
|
}
|
|
|
|
|
2020-02-11 11:25:52 +00:00
|
|
|
export default DialogAndroid;
|