Skip to content

Commit

Permalink
added android options of onDismiss and onAny - closes shimohq#34
Browse files Browse the repository at this point in the history
  • Loading branch information
Noitidart committed Mar 23, 2018
1 parent e724872 commit 88ea270
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 22 deletions.
37 changes: 24 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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');
}
}
}
)

Expand Down
50 changes: 41 additions & 9 deletions index.android.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -46,6 +45,12 @@ export type PromptStyle = $Enum<{
'cust': string
}>;

export type PromptAction =
| 'dismissedAction'
| 'positiveAction'
| 'negativeAction'
| 'neutralAction';

type Options = {
disableFullscreenUI?: boolean;
cancelable?: boolean;
Expand All @@ -57,6 +62,8 @@ type Options = {
highlightColor?: string;
color?: string;
buttonColor?: string;
onAny?: PromptAction => void,
onDismiss: () => void
};

/**
Expand All @@ -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,
Expand Down Expand Up @@ -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);
}
}
);
Expand Down

0 comments on commit 88ea270

Please sign in to comment.