0.1.20 • Published 3 years ago

react-native-customisable-alert v0.1.20

Weekly downloads
76
License
MIT
Repository
github
Last release
3 years ago

Installation

To install the latest version of react-native-customisable-alert you only need to run:

npm install --save react-native-customisable-alert

or

yarn add react-native-customisable-alert

Try it out

You can find the examples above on src/examples

Basic Usage

This component was meant to be used globally, so you only need to import it once in your App.js:

import React from "react";
import { View } from "react-native";
import CustomisableAlert from "react-native-customisable-alert";

export default class App extends React.Component {
    render() {
        return (
            <View style={{ flex: 1 }}>
                ... all your stuff
                <CustomisableAlert
                    titleStyle={{
                        fontSize: 18,
                        fontWeight: "bold"
                    }}
                />
            </View>
        );
    }
}

After that all you need is to call showAlert or closeAlert methods from anywhere in your app.

Show some alert

To fire an alert just call showAlert(obj) as below:

import React from "react";
import { View, Button } from "react-native";

import { showAlert } from "react-native-customisable-alert";

class OtherScreen extends React.Component {
  render() {
    return (
      <View style={{ flex: 1 }}>
        <Button
          onPress={() => {
            showAlert({
              title="Are you sure?"
              message: "All your files will be deleted!",
              alertType: 'warning',
              onPress: () => console.log('files deleted!'),
            });
          }}
        />
      </View>
    );
  }
}

When you call showAlert you should pass some attributes to customize your alert:

PropertyTypeDescription
titlestringThe title of your alert
messagestringThe message of your alert
btnLabelstringButton label for the one button alert ('error' or success' alertTypes), default='Ok'
leftBtnLabelstringLeft button label for two buttons alerts ('warning' alertTypes), default='Cancel'
customAlertReact.ComponentDefine a custom alert (this will replace the whole thing!)
customIconReact.ComponentSet a custom icon for your alert. If you want no icon in your alert use customIcon:'none'
alertType'error', 'success', 'warning', 'custom'Define the type of the alert
onPress(): voidButton pressed callback (one button alerts or right button in a two buttons alerts)
onDismiss(): voidClose alert button callback (left button in a two buttons alerts)
animationInAnimationOverhides global animation for an entrance animation
animationOutAnimationOverhides global animation for an exit animation
dismissablebooleanOverhides global dismissable behavior. If true it closes the alert when touch outside, default false
modalPropsModalPropsOverhides global modal props from react-native-modal

All alerts are set to close automatically when any button pressed, but for custom and warning alertTypes you need close it programatically as the example below:

import React from "react";
import { View, Button } from "react-native";

import { showAlert, closeAlert } from "react-native-customisable-alert";

class OtherScreen extends React.Component {
    render() {
        return (
            <View style={{ flex: 1 }}>
                <Button
                    onPress={() => {
                        showAlert({
                            customAlert: (
                                <View>
                                    <Text>Could you tell us your name?</Text>
                                    <TextInput />
                                    <View>
                                        <Button
                                            title="Cancel"
                                            onPress={() => closeAlert()}
                                        />
                                        <Button
                                            title="Send"
                                            onPress={() => {}}
                                        />
                                    </View>
                                </View>
                            )
                        });
                    }}
                />
            </View>
        );
    }
}

Global Props

You can set a default style for all your alerts with the following:

PropertyTypeDescription
defaultTitlestringSet a default title for all alerts you call, default='Title'
backdropStyleViewStyleStyle for the alert backdrop
alertContainerStyleViewStyleStyle for the alert container
titleStyleTextStyleStyle for the title
textStyleTextStyleStyle for the message
btnStyleTouchableOpacityPropsStyle for all buttons or one button alert type
btnLeftStyleTouchableOpacityPropsStyle for the left button ('warning' alertTypes), overrides btnStyle
btnRightStyleTouchableOpacityPropsStyle for the right button ('warning' alertTypes), overrides btnStyle
btnLabelStyleTextStyleStyle for the label of the buttons
btnLeftLabelStyleTextStyleStyle for the left label button
btnRightLabelStyleTextStyleStyle for the right label button
defaultLeftBtnLabelstringSet a default label for the left button, default='Cancel'
defaultRightBtnLabelstringSet a default label for the right button, default='Ok'
defaultType'error' | 'success' | 'warning' | 'custom'Set a default type for your alert, default='error'
animationInAnimationSet a default entrance animation for your alert
animationOutAnimationSet a default exit animation for your alert
dismissablebooleanIf true alert auto dismiss when touch outside, default=false
defaultWarningIconReact.ComponentSet a default icon for you warning messages
defaultSuccessIconReact.ComponentSet a default icon for you success messages
defaultErrorIconReact.ComponentSet a default icon for you error messages
modalPropsModalPropsSet a global modal props from react-native-modal

License

MIT

0.1.20

3 years ago

0.1.19

3 years ago

0.1.18

3 years ago

0.1.14

3 years ago

0.1.17

3 years ago

0.1.13

4 years ago

0.1.11

4 years ago

0.1.12

4 years ago

0.1.10

4 years ago

0.1.9

4 years ago

0.1.8

4 years ago

0.1.7

4 years ago

0.1.6

4 years ago

0.1.5

4 years ago

0.1.2

4 years ago

0.1.1

4 years ago

0.1.4

4 years ago

0.1.3

4 years ago

0.1.0

4 years ago

0.0.3

4 years ago

0.0.2

4 years ago

0.0.1

4 years ago