Repository: chentsulin/sweetalert-react Branch: master Commit: 8e214bce947f Files: 87 Total size: 56.8 KB Directory structure: gitextract_ihku2qdz/ ├── .babelrc ├── .editorconfig ├── .eslintignore ├── .eslintrc.json ├── .gitattributes ├── .gitignore ├── .travis.yml ├── CHANGELOG.md ├── LICENSE ├── README.md ├── __mocks__/ │ └── sweetalert.js ├── examples/ │ ├── cancel/ │ │ ├── .babelrc │ │ ├── components/ │ │ │ └── App.js │ │ ├── index.html │ │ ├── index.js │ │ ├── package.json │ │ ├── server.js │ │ └── webpack.config.js │ ├── complex/ │ │ ├── .babelrc │ │ ├── components/ │ │ │ └── App.js │ │ ├── index.html │ │ ├── index.js │ │ ├── package.json │ │ ├── server.js │ │ └── webpack.config.js │ ├── component-as-body/ │ │ ├── .babelrc │ │ ├── components/ │ │ │ ├── App.js │ │ │ └── HelloWorld.js │ │ ├── index.html │ │ ├── index.js │ │ ├── package.json │ │ ├── server.js │ │ └── webpack.config.js │ ├── esc/ │ │ ├── .babelrc │ │ ├── components/ │ │ │ └── App.js │ │ ├── index.html │ │ ├── index.js │ │ ├── package.json │ │ ├── server.js │ │ └── webpack.config.js │ ├── history-change/ │ │ ├── .babelrc │ │ ├── components/ │ │ │ └── App.js │ │ ├── index.html │ │ ├── index.js │ │ ├── package.json │ │ ├── server.js │ │ └── webpack.config.js │ ├── input/ │ │ ├── .babelrc │ │ ├── components/ │ │ │ └── App.js │ │ ├── index.html │ │ ├── index.js │ │ ├── package.json │ │ ├── server.js │ │ └── webpack.config.js │ ├── outside-click/ │ │ ├── .babelrc │ │ ├── components/ │ │ │ └── App.js │ │ ├── index.html │ │ ├── index.js │ │ ├── package.json │ │ ├── server.js │ │ └── webpack.config.js │ ├── show-input-error/ │ │ ├── .babelrc │ │ ├── components/ │ │ │ └── App.js │ │ ├── index.html │ │ ├── index.js │ │ ├── package.json │ │ ├── server.js │ │ └── webpack.config.js │ └── simple/ │ ├── .babelrc │ ├── components/ │ │ └── App.js │ ├── index.html │ ├── index.js │ ├── package.json │ ├── server.js │ └── webpack.config.js ├── package.json ├── src/ │ ├── SweetAlert.js │ ├── __tests__/ │ │ ├── SweetAlert-test.js │ │ └── index-test.js │ ├── index.js │ └── utils/ │ ├── __mocks__/ │ │ └── isDOMEquals.js │ ├── __tests__/ │ │ ├── isDOMEquals-test.js │ │ └── outsideTargetHandlerFactory-test.js │ ├── isDOMEquals.js │ └── outsideTargetHandlerFactory.js ├── webpack.config.development.js └── webpack.config.production.js ================================================ FILE CONTENTS ================================================ ================================================ FILE: .babelrc ================================================ { "presets": ["latest", "stage-2", "react"] } ================================================ FILE: .editorconfig ================================================ root = true [*] indent_style = space end_of_line = lf charset = utf-8 trim_trailing_whitespace = true insert_final_newline = true indent_size = 2 [*.md] trim_trailing_whitespace = false ================================================ FILE: .eslintignore ================================================ node_modules dist lib ================================================ FILE: .eslintrc.json ================================================ { "parser": "babel-eslint", "extends": "airbnb", "env": { "browser": true, "node": true, "jest": true, "jasmine": true }, "rules": { "arrow-parens": ["error", "as-needed"], "class-methods-use-this": "off", "comma-dangle": "off", "consistent-return": "off", "no-underscore-dangle": "off", "import/no-extraneous-dependencies": ["error", { "devDependencies": [ "**/__tests__/**.js", "**/webpack.config.js", "webpack.config.*.js" ], "optionalDependencies": false }], "react/jsx-filename-extension": "off", "react/require-default-props": "off" } } ================================================ FILE: .gitattributes ================================================ * text=auto ================================================ FILE: .gitignore ================================================ .DS_Store npm-debug.log node_modules coverage lib dist ================================================ FILE: .travis.yml ================================================ sudo: false language: node_js node_js: - '7' - '6' - '4' script: "npm run test:cov" after_script: "npm install coveralls@2 && cat ./coverage/lcov.info | coveralls" ================================================ FILE: CHANGELOG.md ================================================ 0.4.11 / 2017-12-12 =================== - [new] Support React v16 0.4.10 / 2017-04-24 ================== - [deps] Removes prototype reference which raises warnings in react 15.5 [#71](https://github.com/chentsulin/sweetalert-react/pull/71). 0.4.9 / 2017-02-13 ================== - [fix] clicked comfirm button with empty string should trigger onConfirm instead of onCancel. [#56](https://github.com/chentsulin/sweetalert-react/pull/56). 0.4.8 / 2017-02-05 ================== - [fix] `umd` build 0.4.7 / 2017-01-19 ================== - [fix] allow `animation` prop with `pop`, `slide-from-top`, `slide-from-bottom` value. [#50](https://github.com/chentsulin/sweetalert-react/pull/50). 0.4.6 / 2016-12-05 ================== - [fix] Don't break server rendering [#44](https://github.com/chentsulin/sweetalert-react/pull/44). 0.4.5 / 2016-11-09 ================== - [deps] update dependencies - [fix] Added `inputValue` to `ALLOWS_KEYS` [#33](https://github.com/chentsulin/sweetalert-react/pull/33). ================================================ FILE: LICENSE ================================================ The MIT License (MIT) Copyright (c) 2015-present C.T. Lin (github.com/chentsulin/sweetalert-react) Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ================================================ FILE: README.md ================================================ # sweetalert-react [![NPM version][npm-image]][npm-url] [![Dependency Status][david_img]][david_site] > Declarative SweetAlert in React ## Introduction `sweetalert-react` is a wrapped `sweetalert` implementation with declarative React style component APIs. There is a `show` prop available for toggling alert component's visibility. And `onConfirm`, `onCancel`, `onClose`, `onEscapeKey`, `onOutsideClick` props allow you have a fine grained control over alert component events. ## Install ``` $ npm install sweetalert-react ``` ## Usage ```js import React, { Component } from 'react'; import SweetAlert from 'sweetalert-react'; // ... render() { return (
this.setState({ show: false })} />
); } ``` You should import `sweetalert.css` from CDN, file, node_modules(sweetalert/dist/sweetalert.css) or wherever method to include this CSS file. Checkout the full examples [here](./examples). ## Removed Options - **timer**: You should use `setTimeout` and pass `show` as false. - **closeOnConfirm**: You should pass `show` as false via `onConfirm`. - **closeOnCancel**: You should pass `show` as false via `onCancel`. - **allowEscapeKey**: You should pass `show` as false via `onEscapeKey`. - **allowOutsideClick**: You should pass `show` as false via `onOutsideClick`. All other options can be passed as props, see them in [Configuare Section in sweetalert document](http://t4t5.github.io/sweetalert/) ## FAQ ### Q: My alert didn't close when 'go back' or 'go forward' in browser You can listen to history changes and set `show: false` when it is mounted. See the full example [here](https://github.com/chentsulin/sweetalert-react/blob/master/examples/history-change/components/App.js). ### Q: Can I use react component to render HTML as the alert body? Sure, you can achieve this with `ReactDOMServer.renderToStaticMarkup`: ```js import { renderToStaticMarkup } from 'react-dom/server'; )} onConfirm={() => this.setState({ show: false })} /> ``` See the full example [here](https://github.com/chentsulin/sweetalert-react/blob/master/examples/component-as-body/components/App.js). Thanks @ArkadyB for discovering the approach in [issue #53](https://github.com/chentsulin/sweetalert-react/issues/53). ## Relevant Projects - [react-redux-sweetalert](https://github.com/chentsulin/react-redux-sweetalert) ## License MIT © [C.T. Lin](https://github.com/chentsulin/sweetalert-react) [npm-image]: https://badge.fury.io/js/sweetalert-react.svg [npm-url]: https://npmjs.org/package/sweetalert-react [travis-image]: https://travis-ci.org/chentsulin/sweetalert-react.svg [travis-url]: https://travis-ci.org/chentsulin/sweetalert-react [coveralls-image]: https://coveralls.io/repos/chentsulin/sweetalert-react/badge.svg?branch=master&service=github [coveralls-url]: https://coveralls.io/r/chentsulin/sweetalert-react?branch=master [david_img]: https://david-dm.org/chentsulin/sweetalert-react.svg [david_site]: https://david-dm.org/chentsulin/sweetalert-react ================================================ FILE: __mocks__/sweetalert.js ================================================ const sweetalert = jest.genMockFromModule('sweetalert'); module.exports = sweetalert; ================================================ FILE: examples/cancel/.babelrc ================================================ { "presets": ["latest", "stage-2", "react"] } ================================================ FILE: examples/cancel/components/App.js ================================================ import React, { Component } from 'react'; import SweetAlert from 'sweetalert-react'; // eslint-disable-line import/no-extraneous-dependencies import 'sweetalert/dist/sweetalert.css'; class App extends Component { constructor(props, context) { super(props, context); this.state = { show: false, }; } render() { return (
{ console.log('confirm'); // eslint-disable-line no-console this.setState({ show: false }); }} onCancel={() => { console.log('cancel'); // eslint-disable-line no-console this.setState({ show: false }); }} onClose={() => console.log('close')} // eslint-disable-line no-console />
); } } export default App; ================================================ FILE: examples/cancel/index.html ================================================ sweetalert-react-example
================================================ FILE: examples/cancel/index.js ================================================ import React from 'react'; import { render } from 'react-dom'; import App from './components/App'; render( , document.getElementById('root') ); ================================================ FILE: examples/cancel/package.json ================================================ { "name": "sweetalert-react-example", "version": "1.0.0", "description": "sweetalert-react-example-description", "private": true, "main": "server.js", "scripts": { "start": "node server.js" }, "repository": { "type": "git", "url": "https://github.com/chentsulin/sweetalert-react.git" }, "keywords": [ "sweetalert-react" ], "license": "MIT", "bugs": { "url": "https://github.com/chentsulin/sweetalert-react/issues" }, "homepage": "https://github.com/chentsulin/sweetalert-react", "dependencies": { "react": "^15.0.1", "react-dom": "^15.0.1", "sweetalert": "^1.1.3" }, "devDependencies": { "babel-core": "^6.7.6", "babel-loader": "^6.2.4", "babel-preset-latest": "^6.22.0", "babel-preset-react": "^6.5.0", "babel-preset-stage-2": "^6.22.0", "css-loader": "^0.23.1", "node-libs-browser": "^1.0.0", "react-hot-loader": "^1.3.0", "style-loader": "^0.13.1", "webpack": "^1.12.15", "webpack-dev-server": "^1.14.1" } } ================================================ FILE: examples/cancel/server.js ================================================ /* eslint-disable import/no-extraneous-dependencies */ const webpack = require('webpack'); const WebpackDevServer = require('webpack-dev-server'); const config = require('./webpack.config'); new WebpackDevServer(webpack(config), { publicPath: config.output.publicPath, hot: true, historyApiFallback: true, stats: { colors: true, }, }).listen(3000, 'localhost', err => { if (err) { console.log(err); // eslint-disable-line no-console } console.log('Listening at localhost:3000'); // eslint-disable-line no-console }); ================================================ FILE: examples/cancel/webpack.config.js ================================================ const path = require('path'); const webpack = require('webpack'); module.exports = { devtool: 'eval', entry: [ 'webpack-dev-server/client?http://localhost:3000', 'webpack/hot/only-dev-server', './index', ], output: { path: path.join(__dirname, 'dist'), filename: 'bundle.js', publicPath: '/static/', }, plugins: [ new webpack.HotModuleReplacementPlugin(), new webpack.NoErrorsPlugin(), ], resolve: { alias: { 'sweetalert-react': path.join(__dirname, '..', '..', 'src', 'SweetAlert.js'), }, extensions: ['', '.js'], }, module: { loaders: [{ test: /\.js$/, loaders: ['react-hot', 'babel'], exclude: /node_modules/, include: __dirname, }, { test: /\.js$/, loaders: ['babel'], include: path.join(__dirname, '..', '..', 'src'), }, { test: /\.css$/, loaders: ['style-loader', 'css-loader'], }], }, }; ================================================ FILE: examples/complex/.babelrc ================================================ { "presets": ["latest", "stage-2", "react"] } ================================================ FILE: examples/complex/components/App.js ================================================ import React, { Component } from 'react'; import SweetAlert from 'sweetalert-react'; // eslint-disable-line import/no-extraneous-dependencies import 'sweetalert/dist/sweetalert.css'; class App extends Component { constructor(props, context) { super(props, context); this.state = { show: false, }; } render() { return (
{ console.log('confirm'); // eslint-disable-line no-console this.setState({ show: false }); }} onCancel={() => { console.log('cancel'); // eslint-disable-line no-console this.setState({ show: false }); }} onEscapeKey={() => this.setState({ show: false })} onOutsideClick={() => this.setState({ show: false })} />
); } } export default App; ================================================ FILE: examples/complex/index.html ================================================ sweetalert-react-example
================================================ FILE: examples/complex/index.js ================================================ import React from 'react'; import { render } from 'react-dom'; import App from './components/App'; render( , document.getElementById('root') ); ================================================ FILE: examples/complex/package.json ================================================ { "name": "sweetalert-react-example", "version": "1.0.0", "description": "sweetalert-react-example-description", "private": true, "main": "server.js", "scripts": { "start": "node server.js" }, "repository": { "type": "git", "url": "https://github.com/chentsulin/sweetalert-react.git" }, "keywords": [ "sweetalert-react" ], "license": "MIT", "bugs": { "url": "https://github.com/chentsulin/sweetalert-react/issues" }, "homepage": "https://github.com/chentsulin/sweetalert-react", "dependencies": { "react": "^15.0.1", "react-dom": "^15.0.1", "sweetalert": "^1.1.3" }, "devDependencies": { "babel-core": "^6.7.6", "babel-loader": "^6.2.4", "babel-preset-latest": "^6.22.0", "babel-preset-react": "^6.5.0", "babel-preset-stage-2": "^6.22.0", "css-loader": "^0.23.1", "node-libs-browser": "^1.0.0", "react-hot-loader": "^1.3.0", "style-loader": "^0.13.1", "webpack": "^1.12.15", "webpack-dev-server": "^1.14.1" } } ================================================ FILE: examples/complex/server.js ================================================ /* eslint-disable import/no-extraneous-dependencies */ const webpack = require('webpack'); const WebpackDevServer = require('webpack-dev-server'); const config = require('./webpack.config'); new WebpackDevServer(webpack(config), { publicPath: config.output.publicPath, hot: true, historyApiFallback: true, stats: { colors: true, }, }).listen(3000, 'localhost', err => { if (err) { console.log(err); // eslint-disable-line no-console } console.log('Listening at localhost:3000'); // eslint-disable-line no-console }); ================================================ FILE: examples/complex/webpack.config.js ================================================ const path = require('path'); const webpack = require('webpack'); module.exports = { devtool: 'eval', entry: [ 'webpack-dev-server/client?http://localhost:3000', 'webpack/hot/only-dev-server', './index', ], output: { path: path.join(__dirname, 'dist'), filename: 'bundle.js', publicPath: '/static/', }, plugins: [ new webpack.HotModuleReplacementPlugin(), new webpack.NoErrorsPlugin(), ], resolve: { alias: { 'sweetalert-react': path.join(__dirname, '..', '..', 'src', 'SweetAlert.js'), }, extensions: ['', '.js'], }, module: { loaders: [{ test: /\.js$/, loaders: ['react-hot', 'babel'], exclude: /node_modules/, include: __dirname, }, { test: /\.js$/, loaders: ['babel'], include: path.join(__dirname, '..', '..', 'src'), }, { test: /\.css$/, loaders: ['style-loader', 'css-loader'], }], }, }; ================================================ FILE: examples/component-as-body/.babelrc ================================================ { "presets": ["latest", "stage-2", "react"] } ================================================ FILE: examples/component-as-body/components/App.js ================================================ import React, { Component } from 'react'; import { renderToStaticMarkup } from 'react-dom/server'; import SweetAlert from 'sweetalert-react'; // eslint-disable-line import/no-extraneous-dependencies import 'sweetalert/dist/sweetalert.css'; import HelloWorld from './HelloWorld'; class App extends Component { constructor(props, context) { super(props, context); this.state = { show: false, }; } render() { return (
)} onConfirm={() => this.setState({ show: false })} />
); } } export default App; ================================================ FILE: examples/component-as-body/components/HelloWorld.js ================================================ import React from 'react'; const HelloWorld = () =>

SweetAlert in React

; export default HelloWorld; ================================================ FILE: examples/component-as-body/index.html ================================================ sweetalert-react-example
================================================ FILE: examples/component-as-body/index.js ================================================ import React from 'react'; import { render } from 'react-dom'; import App from './components/App'; render( , document.getElementById('root') ); ================================================ FILE: examples/component-as-body/package.json ================================================ { "name": "sweetalert-react-example", "version": "1.0.0", "description": "sweetalert-react-example-description", "private": true, "main": "server.js", "scripts": { "start": "node server.js" }, "repository": { "type": "git", "url": "https://github.com/chentsulin/sweetalert-react.git" }, "keywords": [ "sweetalert-react" ], "license": "MIT", "bugs": { "url": "https://github.com/chentsulin/sweetalert-react/issues" }, "homepage": "https://github.com/chentsulin/sweetalert-react", "dependencies": { "react": "^15.0.1", "react-dom": "^15.0.1", "sweetalert": "^1.1.3" }, "devDependencies": { "babel-core": "^6.7.6", "babel-loader": "^6.2.4", "babel-preset-latest": "^6.22.0", "babel-preset-react": "^6.5.0", "babel-preset-stage-2": "^6.22.0", "css-loader": "^0.23.1", "node-libs-browser": "^1.0.0", "react-hot-loader": "^1.3.0", "style-loader": "^0.13.1", "webpack": "^1.12.15", "webpack-dev-server": "^1.14.1" } } ================================================ FILE: examples/component-as-body/server.js ================================================ /* eslint-disable import/no-extraneous-dependencies */ const webpack = require('webpack'); const WebpackDevServer = require('webpack-dev-server'); const config = require('./webpack.config'); new WebpackDevServer(webpack(config), { publicPath: config.output.publicPath, hot: true, historyApiFallback: true, stats: { colors: true, }, }).listen(3000, 'localhost', err => { if (err) { console.log(err); // eslint-disable-line no-console } console.log('Listening at localhost:3000'); // eslint-disable-line no-console }); ================================================ FILE: examples/component-as-body/webpack.config.js ================================================ const path = require('path'); const webpack = require('webpack'); module.exports = { devtool: 'eval', entry: [ 'webpack-dev-server/client?http://localhost:3000', 'webpack/hot/only-dev-server', './index', ], output: { path: path.join(__dirname, 'dist'), filename: 'bundle.js', publicPath: '/static/', }, plugins: [ new webpack.HotModuleReplacementPlugin(), new webpack.NoErrorsPlugin(), ], resolve: { alias: { 'sweetalert-react': path.join(__dirname, '..', '..', 'src', 'SweetAlert.js'), }, extensions: ['', '.js'], }, module: { loaders: [{ test: /\.js$/, loaders: ['react-hot', 'babel'], exclude: /node_modules/, include: __dirname, }, { test: /\.js$/, loaders: ['babel'], include: path.join(__dirname, '..', '..', 'src'), }, { test: /\.css$/, loaders: ['style-loader', 'css-loader'], }], }, }; ================================================ FILE: examples/esc/.babelrc ================================================ { "presets": ["latest", "stage-2", "react"] } ================================================ FILE: examples/esc/components/App.js ================================================ import React, { Component } from 'react'; import SweetAlert from 'sweetalert-react'; // eslint-disable-line import/no-extraneous-dependencies import 'sweetalert/dist/sweetalert.css'; class App extends Component { constructor(props, context) { super(props, context); this.state = { show: false, }; } render() { return (
this.setState({ show: false })} onEscapeKey={() => this.setState({ show: false })} />
); } } export default App; ================================================ FILE: examples/esc/index.html ================================================ sweetalert-react-example
================================================ FILE: examples/esc/index.js ================================================ import React from 'react'; import { render } from 'react-dom'; import App from './components/App'; render( , document.getElementById('root') ); ================================================ FILE: examples/esc/package.json ================================================ { "name": "sweetalert-react-example", "version": "1.0.0", "description": "sweetalert-react-example-description", "private": true, "main": "server.js", "scripts": { "start": "node server.js" }, "repository": { "type": "git", "url": "https://github.com/chentsulin/sweetalert-react.git" }, "keywords": [ "sweetalert-react" ], "license": "MIT", "bugs": { "url": "https://github.com/chentsulin/sweetalert-react/issues" }, "homepage": "https://github.com/chentsulin/sweetalert-react", "dependencies": { "react": "^15.0.1", "react-dom": "^15.0.1", "sweetalert": "^1.1.3" }, "devDependencies": { "babel-core": "^6.7.6", "babel-loader": "^6.2.4", "babel-preset-latest": "^6.22.0", "babel-preset-react": "^6.5.0", "babel-preset-stage-2": "^6.22.0", "css-loader": "^0.23.1", "node-libs-browser": "^1.0.0", "react-hot-loader": "^1.3.0", "style-loader": "^0.13.1", "webpack": "^1.12.15", "webpack-dev-server": "^1.14.1" } } ================================================ FILE: examples/esc/server.js ================================================ /* eslint-disable import/no-extraneous-dependencies */ const webpack = require('webpack'); const WebpackDevServer = require('webpack-dev-server'); const config = require('./webpack.config'); new WebpackDevServer(webpack(config), { publicPath: config.output.publicPath, hot: true, historyApiFallback: true, stats: { colors: true, }, }).listen(3000, 'localhost', err => { if (err) { console.log(err); // eslint-disable-line no-console } console.log('Listening at localhost:3000'); // eslint-disable-line no-console }); ================================================ FILE: examples/esc/webpack.config.js ================================================ const path = require('path'); const webpack = require('webpack'); module.exports = { devtool: 'eval', entry: [ 'webpack-dev-server/client?http://localhost:3000', 'webpack/hot/only-dev-server', './index', ], output: { path: path.join(__dirname, 'dist'), filename: 'bundle.js', publicPath: '/static/', }, plugins: [ new webpack.HotModuleReplacementPlugin(), new webpack.NoErrorsPlugin(), ], resolve: { alias: { 'sweetalert-react': path.join(__dirname, '..', '..', 'src', 'SweetAlert.js'), }, extensions: ['', '.js'], }, module: { loaders: [{ test: /\.js$/, loaders: ['react-hot', 'babel'], exclude: /node_modules/, include: __dirname, }, { test: /\.js$/, loaders: ['babel'], include: path.join(__dirname, '..', '..', 'src'), }, { test: /\.css$/, loaders: ['style-loader', 'css-loader'], }], }, }; ================================================ FILE: examples/history-change/.babelrc ================================================ { "presets": ["latest", "stage-2", "react"] } ================================================ FILE: examples/history-change/components/App.js ================================================ import React, { Component } from 'react'; import shortid from 'shortid'; import SweetAlert from 'sweetalert-react'; // eslint-disable-line import/no-extraneous-dependencies import 'sweetalert/dist/sweetalert.css'; class App extends Component { constructor(props, context) { super(props, context); this.state = { show: false, }; } componentDidMount() { window.addEventListener('popstate', this.hiddenAlert); } componentWillUnmount() { window.removeEventListener('popstate', this.hiddenAlert); } hiddenAlert = () => { this.setState({ show: false }); }; pushNextState = () => { history.pushState(null, 'next', shortid.generate()); }; render() { return (
); } } export default App; ================================================ FILE: examples/history-change/index.html ================================================ sweetalert-react-example
================================================ FILE: examples/history-change/index.js ================================================ import React from 'react'; import { render } from 'react-dom'; import App from './components/App'; render( , document.getElementById('root') ); ================================================ FILE: examples/history-change/package.json ================================================ { "name": "sweetalert-react-example", "version": "1.0.0", "description": "sweetalert-react-example-description", "private": true, "main": "server.js", "scripts": { "start": "node server.js" }, "repository": { "type": "git", "url": "https://github.com/chentsulin/sweetalert-react.git" }, "keywords": [ "sweetalert-react" ], "license": "MIT", "bugs": { "url": "https://github.com/chentsulin/sweetalert-react/issues" }, "homepage": "https://github.com/chentsulin/sweetalert-react", "dependencies": { "react": "^15.0.1", "react-dom": "^15.0.1", "shortid": "^2.2.6", "sweetalert": "^1.1.3" }, "devDependencies": { "babel-core": "^6.7.6", "babel-loader": "^6.2.4", "babel-preset-latest": "^6.22.0", "babel-preset-react": "^6.5.0", "babel-preset-stage-2": "^6.22.0", "css-loader": "^0.23.1", "node-libs-browser": "^1.0.0", "react-hot-loader": "^1.3.0", "style-loader": "^0.13.1", "webpack": "^1.12.15", "webpack-dev-server": "^1.14.1" } } ================================================ FILE: examples/history-change/server.js ================================================ /* eslint-disable import/no-extraneous-dependencies */ const webpack = require('webpack'); const WebpackDevServer = require('webpack-dev-server'); const config = require('./webpack.config'); new WebpackDevServer(webpack(config), { publicPath: config.output.publicPath, hot: true, historyApiFallback: true, stats: { colors: true, }, }).listen(3000, 'localhost', err => { if (err) { console.log(err); // eslint-disable-line no-console } console.log('Listening at localhost:3000'); // eslint-disable-line no-console }); ================================================ FILE: examples/history-change/webpack.config.js ================================================ const path = require('path'); const webpack = require('webpack'); module.exports = { devtool: 'eval', entry: [ 'webpack-dev-server/client?http://localhost:3000', 'webpack/hot/only-dev-server', './index', ], output: { path: path.join(__dirname, 'dist'), filename: 'bundle.js', publicPath: '/static/', }, plugins: [ new webpack.HotModuleReplacementPlugin(), new webpack.NoErrorsPlugin(), ], resolve: { alias: { 'sweetalert-react': path.join(__dirname, '..', '..', 'src', 'SweetAlert.js'), }, extensions: ['', '.js'], }, module: { loaders: [{ test: /\.js$/, loaders: ['react-hot', 'babel'], exclude: /node_modules/, include: __dirname, }, { test: /\.js$/, loaders: ['babel'], include: path.join(__dirname, '..', '..', 'src'), }, { test: /\.css$/, loaders: ['style-loader', 'css-loader'], }], }, }; ================================================ FILE: examples/input/.babelrc ================================================ { "presets": ["latest", "stage-2", "react"] } ================================================ FILE: examples/input/components/App.js ================================================ import React, { Component } from 'react'; import SweetAlert from 'sweetalert-react'; // eslint-disable-line import/no-extraneous-dependencies import 'sweetalert/dist/sweetalert.css'; class App extends Component { constructor(props, context) { super(props, context); this.state = { show: false, }; } render() { return (
{ console.log(inputValue); // eslint-disable-line no-console this.setState({ show: false }); }} />
); } } export default App; ================================================ FILE: examples/input/index.html ================================================ sweetalert-react-example
================================================ FILE: examples/input/index.js ================================================ import React from 'react'; import { render } from 'react-dom'; import App from './components/App'; render( , document.getElementById('root') ); ================================================ FILE: examples/input/package.json ================================================ { "name": "sweetalert-react-example", "version": "1.0.0", "description": "sweetalert-react-example-description", "private": true, "main": "server.js", "scripts": { "start": "node server.js" }, "repository": { "type": "git", "url": "https://github.com/chentsulin/sweetalert-react.git" }, "keywords": [ "sweetalert-react" ], "license": "MIT", "bugs": { "url": "https://github.com/chentsulin/sweetalert-react/issues" }, "homepage": "https://github.com/chentsulin/sweetalert-react", "dependencies": { "react": "^15.0.1", "react-dom": "^15.0.1", "sweetalert": "^1.1.3" }, "devDependencies": { "babel-core": "^6.7.6", "babel-loader": "^6.2.4", "babel-preset-latest": "^6.22.0", "babel-preset-react": "^6.5.0", "babel-preset-stage-2": "^6.22.0", "css-loader": "^0.23.1", "node-libs-browser": "^1.0.0", "react-hot-loader": "^1.3.0", "style-loader": "^0.13.1", "webpack": "^1.12.15", "webpack-dev-server": "^1.14.1" } } ================================================ FILE: examples/input/server.js ================================================ /* eslint-disable import/no-extraneous-dependencies */ const webpack = require('webpack'); const WebpackDevServer = require('webpack-dev-server'); const config = require('./webpack.config'); new WebpackDevServer(webpack(config), { publicPath: config.output.publicPath, hot: true, historyApiFallback: true, stats: { colors: true, }, }).listen(3000, 'localhost', err => { if (err) { console.log(err); // eslint-disable-line no-console } console.log('Listening at localhost:3000'); // eslint-disable-line no-console }); ================================================ FILE: examples/input/webpack.config.js ================================================ const path = require('path'); const webpack = require('webpack'); module.exports = { devtool: 'eval', entry: [ 'webpack-dev-server/client?http://localhost:3000', 'webpack/hot/only-dev-server', './index', ], output: { path: path.join(__dirname, 'dist'), filename: 'bundle.js', publicPath: '/static/', }, plugins: [ new webpack.HotModuleReplacementPlugin(), new webpack.NoErrorsPlugin(), ], resolve: { alias: { 'sweetalert-react': path.join(__dirname, '..', '..', 'src', 'SweetAlert.js'), }, extensions: ['', '.js'], }, module: { loaders: [{ test: /\.js$/, loaders: ['react-hot', 'babel'], exclude: /node_modules/, include: __dirname, }, { test: /\.js$/, loaders: ['babel'], include: path.join(__dirname, '..', '..', 'src'), }, { test: /\.css$/, loaders: ['style-loader', 'css-loader'], }], }, }; ================================================ FILE: examples/outside-click/.babelrc ================================================ { "presets": ["latest", "stage-2", "react"] } ================================================ FILE: examples/outside-click/components/App.js ================================================ import React, { Component } from 'react'; import SweetAlert from 'sweetalert-react'; // eslint-disable-line import/no-extraneous-dependencies import 'sweetalert/dist/sweetalert.css'; class App extends Component { constructor(props, context) { super(props, context); this.state = { show: false, }; } render() { return (
this.setState({ show: false })} onOutsideClick={() => this.setState({ show: false })} />
); } } export default App; ================================================ FILE: examples/outside-click/index.html ================================================ sweetalert-react-example
================================================ FILE: examples/outside-click/index.js ================================================ import React from 'react'; import { render } from 'react-dom'; import App from './components/App'; render( , document.getElementById('root') ); ================================================ FILE: examples/outside-click/package.json ================================================ { "name": "sweetalert-react-example", "version": "1.0.0", "description": "sweetalert-react-example-description", "private": true, "main": "server.js", "scripts": { "start": "node server.js" }, "repository": { "type": "git", "url": "https://github.com/chentsulin/sweetalert-react.git" }, "keywords": [ "sweetalert-react" ], "license": "MIT", "bugs": { "url": "https://github.com/chentsulin/sweetalert-react/issues" }, "homepage": "https://github.com/chentsulin/sweetalert-react", "dependencies": { "react": "^15.0.1", "react-dom": "^15.0.1", "sweetalert": "^1.1.3" }, "devDependencies": { "babel-core": "^6.7.6", "babel-loader": "^6.2.4", "babel-preset-latest": "^6.22.0", "babel-preset-react": "^6.5.0", "babel-preset-stage-2": "^6.22.0", "css-loader": "^0.23.1", "node-libs-browser": "^1.0.0", "react-hot-loader": "^1.3.0", "style-loader": "^0.13.1", "webpack": "^1.12.15", "webpack-dev-server": "^1.14.1" } } ================================================ FILE: examples/outside-click/server.js ================================================ /* eslint-disable import/no-extraneous-dependencies */ const webpack = require('webpack'); const WebpackDevServer = require('webpack-dev-server'); const config = require('./webpack.config'); new WebpackDevServer(webpack(config), { publicPath: config.output.publicPath, hot: true, historyApiFallback: true, stats: { colors: true, }, }).listen(3000, 'localhost', err => { if (err) { console.log(err); // eslint-disable-line no-console } console.log('Listening at localhost:3000'); // eslint-disable-line no-console }); ================================================ FILE: examples/outside-click/webpack.config.js ================================================ const path = require('path'); const webpack = require('webpack'); module.exports = { devtool: 'eval', entry: [ 'webpack-dev-server/client?http://localhost:3000', 'webpack/hot/only-dev-server', './index', ], output: { path: path.join(__dirname, 'dist'), filename: 'bundle.js', publicPath: '/static/', }, plugins: [ new webpack.HotModuleReplacementPlugin(), new webpack.NoErrorsPlugin(), ], resolve: { alias: { 'sweetalert-react': path.join(__dirname, '..', '..', 'src', 'SweetAlert.js'), }, extensions: ['', '.js'], }, module: { loaders: [{ test: /\.js$/, loaders: ['react-hot', 'babel'], exclude: /node_modules/, include: __dirname, }, { test: /\.js$/, loaders: ['babel'], include: path.join(__dirname, '..', '..', 'src'), }, { test: /\.css$/, loaders: ['style-loader', 'css-loader'], }], }, }; ================================================ FILE: examples/show-input-error/.babelrc ================================================ { "presets": ["latest", "stage-2", "react"] } ================================================ FILE: examples/show-input-error/components/App.js ================================================ import React, { Component } from 'react'; import swal from 'sweetalert'; import SweetAlert from 'sweetalert-react'; // eslint-disable-line import/no-extraneous-dependencies import 'sweetalert/dist/sweetalert.css'; class App extends Component { constructor(props, context) { super(props, context); this.state = { show: false, }; } render() { return (
{ if (inputValue === '') { swal.showInputError('You need to write something!'); return; } this.setState({ show: false }); }} />
); } } export default App; ================================================ FILE: examples/show-input-error/index.html ================================================ sweetalert-react-example
================================================ FILE: examples/show-input-error/index.js ================================================ import React from 'react'; import { render } from 'react-dom'; import App from './components/App'; render( , document.getElementById('root') ); ================================================ FILE: examples/show-input-error/package.json ================================================ { "name": "sweetalert-react-example", "version": "1.0.0", "description": "sweetalert-react-example-description", "private": true, "main": "server.js", "scripts": { "start": "node server.js" }, "repository": { "type": "git", "url": "https://github.com/chentsulin/sweetalert-react.git" }, "keywords": [ "sweetalert-react" ], "license": "MIT", "bugs": { "url": "https://github.com/chentsulin/sweetalert-react/issues" }, "homepage": "https://github.com/chentsulin/sweetalert-react", "dependencies": { "react": "^15.0.1", "react-dom": "^15.0.1", "sweetalert": "^1.1.3" }, "devDependencies": { "babel-core": "^6.7.6", "babel-loader": "^6.2.4", "babel-preset-latest": "^6.22.0", "babel-preset-react": "^6.5.0", "babel-preset-stage-2": "^6.22.0", "css-loader": "^0.23.1", "node-libs-browser": "^1.0.0", "react-hot-loader": "^1.3.0", "style-loader": "^0.13.1", "webpack": "^1.12.15", "webpack-dev-server": "^1.14.1" } } ================================================ FILE: examples/show-input-error/server.js ================================================ /* eslint-disable import/no-extraneous-dependencies */ const webpack = require('webpack'); const WebpackDevServer = require('webpack-dev-server'); const config = require('./webpack.config'); new WebpackDevServer(webpack(config), { publicPath: config.output.publicPath, hot: true, historyApiFallback: true, stats: { colors: true, }, }).listen(3000, 'localhost', err => { if (err) { console.log(err); // eslint-disable-line no-console } console.log('Listening at localhost:3000'); // eslint-disable-line no-console }); ================================================ FILE: examples/show-input-error/webpack.config.js ================================================ const path = require('path'); const webpack = require('webpack'); module.exports = { devtool: 'eval', entry: [ 'webpack-dev-server/client?http://localhost:3000', 'webpack/hot/only-dev-server', './index', ], output: { path: path.join(__dirname, 'dist'), filename: 'bundle.js', publicPath: '/static/', }, plugins: [ new webpack.HotModuleReplacementPlugin(), new webpack.NoErrorsPlugin(), ], resolve: { alias: { 'sweetalert-react': path.join(__dirname, '..', '..', 'src', 'SweetAlert.js'), }, extensions: ['', '.js'], }, module: { loaders: [{ test: /\.js$/, loaders: ['react-hot', 'babel'], exclude: /node_modules/, include: __dirname, }, { test: /\.js$/, loaders: ['babel'], include: path.join(__dirname, '..', '..', 'src'), }, { test: /\.css$/, loaders: ['style-loader', 'css-loader'], }], }, }; ================================================ FILE: examples/simple/.babelrc ================================================ { "presets": ["latest", "stage-2", "react"] } ================================================ FILE: examples/simple/components/App.js ================================================ import React, { Component } from 'react'; import SweetAlert from 'sweetalert-react'; // eslint-disable-line import/no-extraneous-dependencies import 'sweetalert/dist/sweetalert.css'; class App extends Component { constructor(props, context) { super(props, context); this.state = { show: false, }; } render() { return (
this.setState({ show: false })} />
); } } export default App; ================================================ FILE: examples/simple/index.html ================================================ sweetalert-react-example
================================================ FILE: examples/simple/index.js ================================================ import React from 'react'; import { render } from 'react-dom'; import App from './components/App'; render( , document.getElementById('root') ); ================================================ FILE: examples/simple/package.json ================================================ { "name": "sweetalert-react-example", "version": "1.0.0", "description": "sweetalert-react-example-description", "private": true, "main": "server.js", "scripts": { "start": "node server.js" }, "repository": { "type": "git", "url": "https://github.com/chentsulin/sweetalert-react.git" }, "keywords": [ "sweetalert-react" ], "license": "MIT", "bugs": { "url": "https://github.com/chentsulin/sweetalert-react/issues" }, "homepage": "https://github.com/chentsulin/sweetalert-react", "dependencies": { "react": "^15.0.1", "react-dom": "^15.0.1", "sweetalert": "^1.1.3" }, "devDependencies": { "babel-core": "^6.7.6", "babel-loader": "^6.2.4", "babel-preset-latest": "^6.22.0", "babel-preset-react": "^6.5.0", "babel-preset-stage-2": "^6.22.0", "css-loader": "^0.23.1", "node-libs-browser": "^1.0.0", "react-hot-loader": "^1.3.0", "style-loader": "^0.13.1", "webpack": "^1.12.15", "webpack-dev-server": "^1.14.1" } } ================================================ FILE: examples/simple/server.js ================================================ /* eslint-disable import/no-extraneous-dependencies */ const webpack = require('webpack'); const WebpackDevServer = require('webpack-dev-server'); const config = require('./webpack.config'); new WebpackDevServer(webpack(config), { publicPath: config.output.publicPath, hot: true, historyApiFallback: true, stats: { colors: true, }, }).listen(3000, 'localhost', err => { if (err) { console.log(err); // eslint-disable-line no-console } console.log('Listening at localhost:3000'); // eslint-disable-line no-console }); ================================================ FILE: examples/simple/webpack.config.js ================================================ const path = require('path'); const webpack = require('webpack'); module.exports = { devtool: 'eval', entry: [ 'webpack-dev-server/client?http://localhost:3000', 'webpack/hot/only-dev-server', './index', ], output: { path: path.join(__dirname, 'dist'), filename: 'bundle.js', publicPath: '/static/', }, plugins: [ new webpack.HotModuleReplacementPlugin(), new webpack.NoErrorsPlugin(), ], resolve: { alias: { 'sweetalert-react': path.join(__dirname, '..', '..', 'src', 'SweetAlert.js'), }, extensions: ['', '.js'], }, module: { loaders: [{ test: /\.js$/, loaders: ['react-hot', 'babel'], exclude: /node_modules/, include: __dirname, }, { test: /\.js$/, loaders: ['babel'], include: path.join(__dirname, '..', '..', 'src'), }, { test: /\.css$/, loaders: ['style-loader', 'css-loader'], }], }, }; ================================================ FILE: package.json ================================================ { "name": "sweetalert-react", "version": "0.4.11", "description": "Declarative SweetAlert in React", "main": "lib/index.js", "license": "MIT", "repository": "chentsulin/sweetalert-react", "scripts": { "clean": "rimraf lib dist coverage", "lint": "eslint .", "test": "jest", "test:watch": "npm test -- --watch", "test:cov": "npm test -- --coverage", "check": "npm run lint && npm run test", "build:lib": "babel src --out-dir lib --ignore __tests__", "build:umd": "webpack src/index.js dist/sweetalert-react.js --config webpack.config.development.js", "build:umd:min": "webpack src/index.js dist/sweetalert-react.min.js --config webpack.config.production.js", "build": "npm run clean && npm run build:lib && npm run build:umd && npm run build:umd:min", "preversion": "npm run clean && npm run check", "version": "npm run build", "postversion": "git push && git push --tags && npm run clean", "prepublish": "npm run clean && npm run build" }, "author": { "name": "C.T. Lin", "email": "chentsulin@gmail.com", "url": "github.com/chentsulin/sweetalert-react" }, "engines": { "node": ">=0.10.0" }, "files": [ "lib/" ], "keywords": [ "alert", "sweetalert", "react", "component", "react-component" ], "devDependencies": { "babel-cli": "^6.18.0", "babel-eslint": "^7.1.1", "babel-jest": "^19.0.0", "babel-loader": "^6.2.10", "babel-polyfill": "^6.20.0", "babel-preset-latest": "^6.16.0", "babel-preset-react": "^6.16.0", "babel-preset-stage-2": "^6.18.0", "enzyme": "^2.7.0", "eslint": "^3.13.1", "eslint-config-airbnb": "^14.0.0", "eslint-plugin-import": "^2.2.0", "eslint-plugin-jsx-a11y": "^3.0.2", "eslint-plugin-react": "^6.9.0", "jest-cli": "^19.0.2", "react": "^15.4.2", "react-addons-test-utils": "^15.4.2", "react-dom": "^15.4.2", "rimraf": "^2.5.4", "webpack": "^1.14.0" }, "dependencies": { "lodash.pick": "^4.4.0", "mousetrap": "^1.6.0", "prop-types": "^15.0.0", "sweetalert": "^1.1.3", "warning": "^3.0.0" }, "peerDependencies": { "react": "^0.14.0 || ^15.0.0-0 || ^16.0.0-0" }, "jest": { "verbose": true, "collectCoverageFrom": [ "src/**/*.js" ], "testPathIgnorePatterns": [ "/lib" ] } } ================================================ FILE: src/SweetAlert.js ================================================ import { Component } from 'react'; import PropTypes from 'prop-types'; import swal from 'sweetalert'; import pick from 'lodash.pick'; import mousetrap from 'mousetrap'; import warning from 'warning'; import outsideTargetHandlerFactory from './utils/outsideTargetHandlerFactory'; const ALLOWS_KEYS = [ 'title', 'text', 'type', 'customClass', 'showCancelButton', 'showConfirmButton', 'confirmButtonText', 'confirmButtonColor', 'cancelButtonText', 'imageUrl', 'imageSize', 'html', 'animation', 'inputType', 'inputValue', 'inputPlaceholder', 'showLoaderOnConfirm', ]; const REMOVED_KEYS = [ 'timer', 'closeOnConfirm', 'closeOnCancel', 'allowOutsideClick', 'allowEscapeKey', ]; const OVERWRITE_PROPS = { closeOnConfirm: false, closeOnCancel: false, allowOutsideClick: false, allowEscapeKey: false, }; // reference: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input const ALLOWS_INPUT_TYPES = [ 'button', 'checkbox', 'color', 'date', 'datetime', 'datetime-local', 'email', 'file', 'hidden', 'image', 'month', 'number', 'password', 'radio', 'range', 'reset', 'search', 'submit', 'tel', 'text', 'time', 'url', 'week', ]; function warningRemoved(props) { REMOVED_KEYS.forEach(key => { warning( props[key] === undefined, '%s has been removed from sweetalert-react, pass `show` props and use event hook instead.', `\`${key}\`` ); }); } export default class SweetAlert extends Component { /* eslint-disable react/no-unused-prop-types */ static propTypes = { // sweetalert option title: PropTypes.string.isRequired, text: PropTypes.string, type: PropTypes.oneOf(['warning', 'error', 'success', 'info', 'input']), customClass: PropTypes.string, showCancelButton: PropTypes.bool, showConfirmButton: PropTypes.bool, confirmButtonText: PropTypes.string, confirmButtonColor: PropTypes.string, cancelButtonText: PropTypes.string, imageUrl: PropTypes.string, imageSize(props, propName) { if (!/^[1-9]\d*x[1-9]\d*/.test(props[propName])) { return new Error('imageSize should have the format like this: "80x80"'); } }, html: PropTypes.bool, animation: PropTypes.oneOfType([ PropTypes.bool, PropTypes.oneOf(['pop', 'slide-from-top', 'slide-from-bottom']), ]), inputType: PropTypes.oneOf(ALLOWS_INPUT_TYPES), inputPlaceholder: PropTypes.string, inputValue: PropTypes.string, showLoaderOnConfirm: PropTypes.bool, // custom option show: PropTypes.bool, onConfirm: PropTypes.func, onCancel: PropTypes.func, onClose: PropTypes.func, onEscapeKey: PropTypes.func, onOutsideClick: PropTypes.func, }; /* eslint-enable react/no-unused-prop-types */ static defaultProps = { // sweetalert option text: null, type: null, customClass: null, showCancelButton: false, showConfirmButton: true, confirmButtonText: 'OK', confirmButtonColor: '#aedef4', cancelButtonText: 'Cancel', imageUrl: null, imageSize: '80x80', html: false, animation: true, inputType: 'text', inputPlaceholder: null, inputValue: null, showLoaderOnConfirm: false, // custom option show: false, }; constructor(props, context) { super(props, context); this._show = false; } componentDidMount() { this.setupWithProps(this.props); if (this.props.onOutsideClick) { this.registerOutsideClickHandler(this.props.onOutsideClick); } } componentWillReceiveProps(props) { this.setupWithProps(props); const oldOutsideClickHandler = this.props.onOutsideClick; const newOutsideClickHandler = props.onOutsideClick; if (oldOutsideClickHandler !== newOutsideClickHandler) { if (oldOutsideClickHandler && newOutsideClickHandler) { this.unregisterOutsideClickHandler(); this.registerOutsideClickHandler(newOutsideClickHandler); } else if (oldOutsideClickHandler && !newOutsideClickHandler) { this.unregisterOutsideClickHandler(); } else if ((!oldOutsideClickHandler) && newOutsideClickHandler) { this.registerOutsideClickHandler(newOutsideClickHandler); } } } componentWillUnmount() { this.unregisterOutsideClickHandler(); this.unbindEscapeKey(); } setupWithProps(props) { warningRemoved(props); const { show, onConfirm, onCancel, onClose, onEscapeKey } = props; if (show) { swal({ ...pick(props, ALLOWS_KEYS), ...OVERWRITE_PROPS, }, isConfirm => this.handleClick(isConfirm, onConfirm, onCancel)); this._show = true; if (onEscapeKey) this.bindEscapeKey(onEscapeKey); } else { this.handleClose(onClose); } } registerOutsideClickHandler(handler) { this._outsideClickHandler = outsideTargetHandlerFactory( document.getElementsByClassName('sweet-alert')[0], handler ); this.enableOutsideClick(); } unregisterOutsideClickHandler() { this.disableOutsideClick(); this._outsideClickHandler = null; } enableOutsideClick() { const fn = this._outsideClickHandler; if (fn) { document.addEventListener('mousedown', fn); document.addEventListener('touchstart', fn); } } disableOutsideClick() { const fn = this._outsideClickHandler; if (fn) { document.removeEventListener('mousedown', fn); document.removeEventListener('touchstart', fn); } } bindEscapeKey(onEscapeKey) { mousetrap.bind('esc', onEscapeKey); } unbindEscapeKey() { mousetrap.unbind('esc'); } handleClick(isConfirm, onConfirm, onCancel) { if (isConfirm !== false) { if (onConfirm) onConfirm(isConfirm); } else { if (onCancel) onCancel(); // eslint-disable-line no-lonely-if } } handleClose(onClose) { if (this._show) { swal.close(); this.unbindEscapeKey(); if (onClose) onClose(); this._show = false; } } render() { return null; } } ================================================ FILE: src/__tests__/SweetAlert-test.js ================================================ /* eslint-disable no-console */ import React from 'react'; import { mount } from 'enzyme'; import swal from 'sweetalert'; import SweetAlert from '../SweetAlert'; jest.mock('sweetalert'); let _error; beforeEach(() => { _error = console.error; console.error = jest.fn(); }); afterEach(() => { console.error = _error; _error = null; swal.mockClear(); }); describe('propTypes', () => { it('should return error when imageSize invalid', () => { expect(SweetAlert.propTypes.imageSize({ imageSize: '8080' }, 'imageSize')) .toBeInstanceOf(Error); }); it('should not return error when imageSize valid', () => { expect(SweetAlert.propTypes.imageSize({ imageSize: '80x80' }, 'imageSize')) .not.toBeInstanceOf(Error); }); it('should warning when title is not passed down to props', () => { mount(); const message = console.error.mock.calls[0][0]; expect(message).toMatch( /Failed prop type: The prop `title` is marked as required in `SweetAlert`/ ); }); }); describe('allow ALLOWS_KEYS as props', () => { it('should not log error when props pass down', () => { mount( ); expect(console.error).not.toBeCalled(); }); }); describe('warning REMOVED_KEYS', () => { it('should warning when REMOVED_KEYS:timer is passed down to props', () => { mount(); expect(console.error).toBeCalledWith( 'Warning: `timer` has been removed from sweetalert-react, ' + 'pass `show` props and use event hook instead.' ); }); it('should warning when REMOVED_KEYS:timer is passed down to props', () => { mount(); expect(console.error).toBeCalledWith( 'Warning: `timer` has been removed from sweetalert-react, ' + 'pass `show` props and use event hook instead.' ); }); it('should warning when REMOVED_KEYS:closeOnConfirm is passed down to props', () => { mount(); expect(console.error).toBeCalledWith( 'Warning: `closeOnConfirm` has been removed from sweetalert-react, ' + 'pass `show` props and use event hook instead.' ); }); it('should warning when REMOVED_KEYS:closeOnCancel is passed down to props', () => { mount(); expect(console.error).toBeCalledWith( 'Warning: `closeOnCancel` has been removed from sweetalert-react, ' + 'pass `show` props and use event hook instead.' ); }); it('should warning when REMOVED_KEYS:allowOutsideClick is passed down to props', () => { mount(); expect(console.error).toBeCalledWith( 'Warning: `allowOutsideClick` has been removed from sweetalert-react, ' + 'pass `show` props and use event hook instead.' ); }); it('should warning when REMOVED_KEYS:allowEscapeKey is passed down to props', () => { mount(); expect(console.error).toBeCalledWith( 'Warning: `allowEscapeKey` has been removed from sweetalert-react, ' + 'pass `show` props and use event hook instead.' ); }); }); describe('should show prop works', () => { it('should call sweetalert when show is true', async () => { mount( ); expect(swal).toBeCalled(); }); it('should call sweetalert when show is false', async () => { mount( ); expect(swal).not.toBeCalled(); }); }); describe('`onConfirm`', () => { xit('should works', () => { const callback = jest.fn(); mount( ); }); }); describe('Cancel', () => { // body... }); describe('Outside click', () => { // body... }); describe('ESC', () => { // body... }); ================================================ FILE: src/__tests__/index-test.js ================================================ import React from 'react'; import { render } from 'react-dom'; import { renderToString } from 'react-dom/server'; import SweetAlert from '../'; it('should not break render', () => { const node = document.createElement('div'); expect(() => { render( , node ); }).not.toThrow(); }); it('should not break renderToString', () => { global.window = undefined; expect(() => renderToString( )).not.toThrow(); }); ================================================ FILE: src/index.js ================================================ if (typeof window === 'undefined') { module.exports = () => null; } else { module.exports = require('./SweetAlert'); // eslint-disable-line global-require } ================================================ FILE: src/utils/__mocks__/isDOMEquals.js ================================================ const isDOMEquals = jest.genMockFromModule('../isDOMEquals'); module.exports = isDOMEquals; ================================================ FILE: src/utils/__tests__/isDOMEquals-test.js ================================================ import isDOMEquals from '../isDOMEquals'; describe('isDOMEquals', () => { it('should be true if dom equals', () => { const div = document.createElement('div'); expect(isDOMEquals(div, div)).toEqual(true); }); it('should be false if dom does not equal', () => { const div1 = document.createElement('div'); const div2 = document.createElement('div'); expect(isDOMEquals(div1, div2)).toEqual(false); }); }); ================================================ FILE: src/utils/__tests__/outsideTargetHandlerFactory-test.js ================================================ import outsideTargetHandlerFactory from '../outsideTargetHandlerFactory'; jest.mock('../isDOMEquals'); const isDOMEquals = require('../isDOMEquals').default; describe('outsideTargetHandlerFactory', () => { let targetNode; let eventHandler; let handler; beforeEach(() => { targetNode = document.createElement('div'); eventHandler = jest.fn(); handler = outsideTargetHandlerFactory(targetNode, eventHandler); }); it('handler should call stopPropagation', () => { const event = {}; event.target = { parentNode: null }; event.stopPropagation = jest.fn(); handler(event); expect(event.stopPropagation).toBeCalled(); }); it('eventHandler should be called when no parentNode', () => { const event = {}; event.target = { parentNode: null }; event.stopPropagation = () => {}; handler(event); expect(eventHandler).toBeCalled(); }); it('eventHandler should not be called when isDOMEquals return true', () => { isDOMEquals.mockReturnValue(true); const event = {}; event.target = { parentNode: document.createElement('div') }; event.stopPropagation = () => {}; handler(event); expect(eventHandler.mock.calls.length).toEqual(0); }); it('eventHandler should not be called when isDOMEquals return false', () => { isDOMEquals.mockReturnValueOnce(false); const event = {}; event.target = { parentNode: document.createElement('div') }; event.stopPropagation = () => {}; handler(event); expect(eventHandler).toBeCalled(); }); }); ================================================ FILE: src/utils/isDOMEquals.js ================================================ /** * Determinate whether dom1 and dom2 is the same dom or not. * * @param {HTMLElement} dom1 * @param {HTMLElement} dom2 * @return {Boolean} */ export default function isDOMEquals(dom1, dom2) { return dom1 === dom2; } ================================================ FILE: src/utils/outsideTargetHandlerFactory.js ================================================ import isDOMEquals from './isDOMEquals'; /** * * @param {HTMLElement} targetNode * @param {HTMLElement} eventHandler * @return {Boolean} */ export default function outsideTargetHandlerFactory(targetNode, eventHandler) { return evt => { evt.stopPropagation(); let current = evt.target; let found = false; while (current.parentNode) { found = isDOMEquals(current, targetNode); if (found) return; current = current.parentNode; } eventHandler(evt); }; } ================================================ FILE: webpack.config.development.js ================================================ const webpack = require('webpack'); module.exports = { module: { loaders: [ { test: /\.js$/, loaders: ['babel-loader'], exclude: /node_modules/ }, ], }, output: { library: 'ReactSweetAlert', libraryTarget: 'umd', }, resolve: { extensions: ['', '.js'], }, plugins: [ new webpack.optimize.OccurenceOrderPlugin(), new webpack.DefinePlugin({ 'process.env.NODE_ENV': JSON.stringify('development'), }), ], }; ================================================ FILE: webpack.config.production.js ================================================ const webpack = require('webpack'); module.exports = { module: { loaders: [ { test: /\.js$/, loaders: ['babel-loader'], exclude: /node_modules/ }, ], }, output: { library: 'ReactSweetAlert', libraryTarget: 'umd', }, resolve: { extensions: ['', '.js'], }, plugins: [ new webpack.optimize.OccurenceOrderPlugin(), new webpack.DefinePlugin({ 'process.env.NODE_ENV': JSON.stringify('production'), }), new webpack.optimize.UglifyJsPlugin({ compressor: { screw_ie8: true, warnings: false, }, }), ], };