Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

React.createClass is deprecated and will be removed in version 16. #39

Open
RodCardenas opened this issue Jun 14, 2017 · 6 comments
Open

Comments

@RodCardenas
Copy link

Warning shows up on console.

@RodCardenas
Copy link
Author

RodCardenas commented Jun 14, 2017

Propose to update modalFactory.

var React = require('react');
var transitionEvents = require('domkit/transitionEvents');
var appendVendorPrefix = require('domkit/appendVendorPrefix');
var PropTypes = require('prop-types');

module.exports = function(animation){

 class Factory extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            willHidden: false,
            hidden: true
        };

        this.hasHidden = this.hasHidden.bind(this);
        this.handleBackdropClick = this.handleBackdropClick.bind(this);
        this.leave = this.leave.bind(this);
        this.enter = this.enter.bind(this);
        this.show = this.show.bind(this);
        this.hide = this.hide.bind(this);
        this.toggle = this.toggle.bind(this);
        this.listenKeyboard = this.listenKeyboard.bind(this);
    };

    hasHidden() {
        return this.state.hidden;
    };

    addTransitionListener(node, handle){
        if (node) {
          var endListener = function(e) {
              if (e && e.target !== node) {
                  return;
              }
              transitionEvents.removeEndEventListener(node, endListener);
              handle();
          };
          transitionEvents.addEndEventListener(node, endListener);
        }
    };

    handleBackdropClick() {
        if (this.props.closeOnClick) {
            this.hide();
        }
    };

    render() {

        var hidden = this.hasHidden();
        if (hidden) return null;

        var willHidden = this.state.willHidden;
        var animation = this.props.animation;
        var modalStyle = animation.getModalStyle(willHidden);
        var backdropStyle = animation.getBackdropStyle(willHidden);
        var contentStyle = animation.getContentStyle(willHidden);
        var ref = animation.getRef(willHidden);
        var sharp = animation.getSharp && animation.getSharp(willHidden);

        // Apply custom style properties
        if (this.props.modalStyle) {
            var prefixedModalStyle = appendVendorPrefix(this.props.modalStyle);
            for (var style in prefixedModalStyle) {
                modalStyle[style] = prefixedModalStyle[style];
            }
        }

        if (this.props.backdropStyle) {
          var prefixedBackdropStyle = appendVendorPrefix(this.props.backdropStyle);
            for (var style in prefixedBackdropStyle) {
                backdropStyle[style] = prefixedBackdropStyle[style];
            }
        }

        if (this.props.contentStyle) {
          var prefixedContentStyle = appendVendorPrefix(this.props.contentStyle);
            for (var style in prefixedContentStyle) {
                contentStyle[style] = prefixedContentStyle[style];
            }
        }

        var backdrop = this.props.backdrop? React.createElement("div", {style: backdropStyle, onClick: this.props.closeOnClick? this.handleBackdropClick: null}): undefined;

        if(willHidden) {
            var node = this.refs[ref];
            this.addTransitionListener(node, this.leave);
        }

        return (React.createElement("span", null,
            React.createElement("div", {ref: "modal", style: modalStyle, className: this.props.className},
                sharp,
                React.createElement("div", {ref: "content", tabIndex: "-1", style: contentStyle},
                    this.props.children
                )
            ),
            backdrop
         ))
        ;
    };

    leave() {
        this.setState({
            hidden: true
        });
        this.props.onHide();
    };

    enter() {
        this.props.onShow();
    };

    show() {
        if (!this.hasHidden()) return;

        this.setState({
            willHidden: false,
            hidden: false
        });

        setTimeout(function(){
          var ref = this.props.animation.getRef();
          var node = this.refs[ref];
          this.addTransitionListener(node, this.enter);
        }.bind(this), 0);
    };

    hide() {
        if (this.hasHidden()) return;

        this.setState({
            willHidden: true
        });
    };

    toggle() {
        if (this.hasHidden())
            this.show();
        else
            this.hide();
    };

    listenKeyboard(event) {
        if (this.props.keyboard &&
                (event.key === "Escape" ||
                 event.keyCode === 27)) {
            this.hide();
        }
    };

    componentDidMount() {
        window.addEventListener("keydown", this.listenKeyboard, true);
    };

    componentWillUnmount()  {
        window.removeEventListener("keydown", this.listenKeyboard, true);
    };
};

Factory.propTypes = {
    className: PropTypes.string,
    // Close the modal when esc is pressed? Defaults to true.
    keyboard: PropTypes.bool,
    onShow: PropTypes.func,
    onHide: PropTypes.func,
    animation: PropTypes.object,
    backdrop: PropTypes.bool,
    closeOnClick: PropTypes.bool,
    modalStyle: PropTypes.object,
    backdropStyle: PropTypes.object,
    contentStyle: PropTypes.object
};

Factory.defaultProps = {
    className: "",
    onShow: function(){},
    onHide: function(){},
    animation: animation,
    keyboard: true,
    backdrop: true,
    closeOnClick: true,
    modalStyle: {},
    backdropStyle: {},
    contentStyle: {}
};

return Factory;
};

@jerairrest
Copy link

Sadly it kinda looks like PRs are not being accepted on this project. I would love to take it over 👍

@panphora
Copy link

Why not fork it and integrate all these pull requests and issues? That would be amazing.

@jerairrest
Copy link

jerairrest commented Jun 28, 2017

Works for me! https://github.com/jerairrest/boron <- is my current fork. The deprecation warnings have been fixed as well as updated to es6. Feel free to contribute :) Also, thank @RodCardenas for this code.

@michaelmika
Copy link

@jerairrest Thank you very much for your fork! boron did not work for me after updating to version 16.x of ReactJs. This should be the main project, or boron should start to update.

@Maxim-Mazurok
Copy link

I've created pull request #49 to add React 16 support to this project.
It seems like maintainer is active on github, I can see commits from him recently on his profile page, so I hope that he will merge changes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants