From 88ea270de0376085d2a9ccc03ad4483fd9eb33e6 Mon Sep 17 00:00:00 2001 From: Noitidart Date: Fri, 23 Mar 2018 10:56:41 -0700 Subject: [PATCH] added android options of onDismiss and onAny - closes https://github.com/shimohq/react-native-prompt-android/issues/34 --- README.md | 37 ++++++++++++++++++++++------------- index.android.js | 50 +++++++++++++++++++++++++++++++++++++++--------- 2 files changed, 65 insertions(+), 22 deletions(-) diff --git a/README.md b/README.md index 81d25a1..5d0156a 100644 --- a/README.md +++ b/README.md @@ -109,18 +109,20 @@ TODO: ### Options The third argument is an object. It can have any of these keys: -| Key | Description | Type | Default | -|---------------------|---------------------------------------------------------------------------|---------|---------------------------------------------------------------------| -| type | Text input type: `'numeric', 'secure-text', 'phone-pad', 'email-address'` | string | 'default' | -| cancelable | Android only. If tapping outside of the alert box should cause dismiss. | boolean | true | -| defaultValue | Default input value | string | | -| placeholder | String in input that will be rendered when empty. | string | | -| style | `'default', 'shimo', 'cust'` | string | 'default' | -| disableFullscreenUI | When in landscape mode, don't use fullscreen | boolean | false | -| highlightColor | Color of text selection | string | ![Color](#https://facebook.github.io/react-native/docs/colors.html) | -| placeholderColor | Color of the placeholder in input field | string | ![Color](#https://facebook.github.io/react-native/docs/colors.html) | -| color | Color of the text in input field | string | ![Color](#https://facebook.github.io/react-native/docs/colors.html) | -| buttonColor | Color of the buttons | string | ![Color](#https://facebook.github.io/react-native/docs/colors.html) | +| Key | Description | Type | Default | +|---------------------|---------------------------------------------------------------------------|----------------------|---------------------------------------------------------------------| +| type | Text input type: `'numeric', 'secure-text', 'phone-pad', 'email-address'` | string | 'default' | +| cancelable | Android only. If tapping outside of the alert box should cause dismiss. | boolean | true | +| defaultValue | Default input value | string | | +| placeholder | String in input that will be rendered when empty. | string | | +| style | `'default', 'shimo', 'cust'` | string | 'default' | +| disableFullscreenUI | When in landscape mode, don't use fullscreen | boolean | false | +| highlightColor | Color of text selection | string | ![Color](#https://facebook.github.io/react-native/docs/colors.html) | +| placeholderColor | Color of the placeholder in input field | string | ![Color](#https://facebook.github.io/react-native/docs/colors.html) | +| color | Color of the text in input field | string | ![Color](#https://facebook.github.io/react-native/docs/colors.html) | +| buttonColor | Color of the buttons | string | ![Color](#https://facebook.github.io/react-native/docs/colors.html) | +| onDismiss | Callback triggered when prompt is dismissed | () => void | | +| onAny | Callback triggered when any action happens | PromptAction => void | | ##### "cust" Style (change underline, cursor, and handle color) If you set this style, you can adjust the color of the "underline", "cursor", and "handles" of the input field. The default custom color is a reddish color of "#F34336". You can change this by going to `./node_modules/react-native-prompt-android/android/src/main/res/values/colors.xml` and changing the value of the `custUnderlineAndCursorAndHandleColor` field. @@ -143,7 +145,16 @@ If you set this style, you can adjust the color of the "underline", "cursor", an color: '#212121', buttonColor: '#000000', defaultValue: memo, - style: 'cust' + style: 'cust', + onDismiss: () => console.log('prompt was dismissed') + onAny: action => { + switch(action) { + case prompt.dismissedAction: return console.log('onAny says dismissed'); + case prompt.positiveAction: return console.log('onAny says positive button clicked'); + case prompt.negativeAction: return console.log('onAny says negative button clicked'); + case prompt.neutralAction: return console.log('onAny says neutral button clicked'); + } + } } ) diff --git a/index.android.js b/index.android.js index 34294d1..18124b7 100644 --- a/index.android.js +++ b/index.android.js @@ -3,7 +3,6 @@ import processColor from 'react-native/Libraries/StyleSheet/processColor'; const PromptAndroid = NativeModules.PromptAndroid; - export type PromptType = $Enum<{ /** * Default alert with no inputs @@ -46,6 +45,12 @@ export type PromptStyle = $Enum<{ 'cust': string }>; +export type PromptAction = + | 'dismissedAction' + | 'positiveAction' + | 'negativeAction' + | 'neutralAction'; + type Options = { disableFullscreenUI?: boolean; cancelable?: boolean; @@ -57,6 +62,8 @@ type Options = { highlightColor?: string; color?: string; buttonColor?: string; + onAny?: PromptAction => void, + onDismiss: () => void }; /** @@ -76,6 +83,10 @@ type ButtonsArray = Array<{ onPress?: () => void, }>; +prompt.dismissedAction = 'dismissedAction'; +prompt.positiveAction = 'positiveAction'; +prompt.negativeAction = 'negativeAction'; +prompt.neutralAction = 'neutralAction'; export default function prompt( title: ?string, message?: ?string, @@ -139,15 +150,36 @@ export default function prompt( PromptAndroid.promptWithArgs( config, (action, buttonKey, input) => { - if (action !== PromptAndroid.buttonClicked) { - return; + if (action === PromptAndroid.dismissed) { + options.onDismiss && options.onDismiss(); + } else if (action === PromptAndroid.buttonClicked) { + switch (buttonKey) { + case PromptAndroid.buttonNeutral: + buttonNeutral.onPress && buttonNeutral.onPress(input); + break; + case PromptAndroid.buttonNegative: + buttonNegative.onPress && buttonNegative.onPress(); + break; + case PromptAndroid.buttonPositive: + buttonPositive.onPress && buttonPositive.onPress(input); + break; + // no default + } } - if (buttonKey === PromptAndroid.buttonNeutral) { - buttonNeutral.onPress && buttonNeutral.onPress(input); - } else if (buttonKey === PromptAndroid.buttonNegative) { - buttonNegative.onPress && buttonNegative.onPress(); - } else if (buttonKey === PromptAndroid.buttonPositive) { - buttonPositive.onPress && buttonPositive.onPress(input); + + if (options.onAny) { + let actionText; + if (action === PromptAndroid.buttonClicked) { + switch (buttonKey) { + case PromptAndroid.buttonNeutral: actionText = prompt.neutralAction; break; + case PromptAndroid.buttonPositive: actionText = prompt.positiveAction; break; + case PromptAndroid.buttonNegative: actionText = prompt.negativeAction; break; + } + } else if (action === PromptAndroid.dismissed) { + actionText = prompt.dismissedAction; + } + + options.onAny(actionText); } } );