djorg83 / react-bootstrap-sweetalert

A React implementation of SweetAlert
https://djorg83.github.io/react-bootstrap-sweetalert/
MIT License
252 stars 55 forks source link
alert bootstrap confirm javascript react sweetalert typescript

react-bootstrap-sweetalert

NPM version Downloads David GitHub issues license GitHub stars

openbase Openbase Dashboard

NPM

SweetAlert for React/Bootstrap

An awesome replacement for JavaScript's alert.


Support

If you think I've done a good job, consider showing your support by buying me a beer. Cheers! :beers:

Buy me a beer


Demo & Examples

See the demo with examples of common use cases and a live editor.

:alien: TAKE ME TO YOUR DEMO :alien:

Demo GIF

Getting Started

Installation

$ npm i react-bootstrap-sweetalert

or

$ yarn add react-bootstrap-sweetalert

Import

const SweetAlert = require('react-bootstrap-sweetalert');

or

import SweetAlert from 'react-bootstrap-sweetalert';

Recommended Usage

It is recommended that you keep an alert in your state, and remove it when the onConfirm or onCancel callbacks are invoked.

You can have stackable alerts by keeping an array of alerts in your data store, and always providing the first alert in the array as the visible alert. When an alert is closed, remove it from the store.

See examples/redux for a working example of how to implement stackable alerts with a Redux store.


Tip: Receiving an input value

If you're using input type, the value of the input will be sent to the onConfirm callback as the first argument.

onConfirm={(response) => this.onRecieveInput(response)}

Custom Forms / Using Render Props

If you want to build an alert that re-renders based on external state changes, or simply want to build a custom form, then you will find the render props pattern to be your best option.

<SweetAlert
 title={"Uses render props"}
 onConfirm={this.onConfirm}
 onCancel={this.onCancel}
 dependencies={[this.state.firstName, this.state.lastName]}
>
  {(renderProps: SweetAlertRenderProps) => (
    <form>
      Your name is: {this.state.firstName} {this.state.lastName}
      <hr/>
      <input
        type={'text'}
        ref={renderProps.setAutoFocusInputRef}
        className="form-control"
        value={this.state.firstName}
        onKeyDown={renderProps.onEnterKeyDownConfirm}
        onChange={(e) => this.setState({ firstName: e.target.value })}
        placeholder={'First name'}
      />
      <br />
      <input
        type={'text'}
        className="form-control"
        value={this.state.lastName}
        onKeyDown={renderProps.onEnterKeyDownConfirm}
        onChange={(e) => this.setState({ lastName: e.target.value })}
        placeholder={'Last name'}
      />
      <hr/>
    </form>
  )}
</SweetAlert>

Changes in version 5.2

For more see CHANGE_LOG.md

Props

Buttons
Input
Hooks
Styling
Animations

props.title

The text to display for the title. JSX/ReactNode allowed.

NOTE

  • If props.type === 'controlled' then props.onConfirm will receive props.dependencies as its first argument.
  • If props.type === 'input' then props.onConfirm will receive props.dependencies as its first argument.

    props.btnSize

    The type of alert to display.

  • Type: 'lg'|'sm'|'xs'
  • Default: 'lg'
  • Allowed values: 'lg', 'sm', 'xs'

    props.confirmBtnText

    Content of confirm button, or JSX/ReactNode.

  • Type: ReactNode|string
  • Default: 'OK'

    props.confirmBtnBsStyle

    Bootstrap style of confirm button.

  • Type: 'default'|'primary'|'link'|'info'|'success'|'warning'|'danger'|'secondary'|'outline-{variant}'
  • Default: 'primary'

    props.confirmBtnCssClass

    CSS class added to confirm button.

  • Type: string
  • Default: ''

    props.confirmBtnStyle

    Inline style added to confirm button.

  • Type: CSSProperties
  • Default: {}

    props.cancelBtnText

    Content of cancel button, or JSX/ReactNode.

  • Type: ReactNode|string
  • Default: 'Cancel'

    props.cancelBtnBsStyle

    Text of cancel button, or JSX/ReactNode.

  • Type: string
  • Default: 'link'
  • Recommended values: 'default'|'primary'|'link'|'info'|'success'|'warning'|'danger'|'secondary'|'outline-{variant}'

    props.cancelBtnCssClass

    CSS class added to cancel button.

  • Type: string
  • Default: ''

    props.cancelBtnStyle

    Inline style added to cancel button.

  • Type: CSSProperties
  • Default: {}

    props.showCloseButton

    If set to true, then an X close button will be shown in the top right of the alert.

NOTE: You must also implement props.onCancel in order for this props to work. This is because visibility of the component is controlled externally through either props.show or by removing the <SweetAlert /> in your render method.

Example

const [firstName, setFirstName] = useState('');
const [lastName, setLastName] = useState('');

<SweetAlert dependencies={[firstName, lastName]}>
  <div>
    <h4>Hello {{firstName}} {{lastName}}</h4>
    <input value={firstName} onChange={(e) => setFirstName(e.target.value)} />
    <input value={lastName} onChange={(e) => setLastName(e.target.value)} />
  </div>
</SweetAlert>

props.focusConfirmBtn

If true the Confirm button will receive focus automatically. NOTE: Does not apply when props.type is 'input'

Related projects

Development

$ yarn demo && open http://localhost:3000