Socket
Socket
Sign inDemoInstall

@mxjs/router-modal

Package Overview
Dependencies
Maintainers
1
Versions
26
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@mxjs/router-modal - npm Package Compare versions

Comparing version 0.2.16 to 0.3.0

21

CHANGELOG.md

@@ -0,1 +1,22 @@

# [0.3.0](https://github.com/miaoxing/miaoxing/compare/@mxjs/router-modal@0.2.16...@mxjs/router-modal@0.3.0) (2024-05-01)
### Features
* 更新 `react-router` 到 v6 ([9115eb5](https://github.com/miaoxing/miaoxing/commit/9115eb56195a095f592dd9e402b25f7e22365b62))
* **router-modal:** 更新组件为函数式 ([ee17b5d](https://github.com/miaoxing/miaoxing/commit/ee17b5d44f811382428db4670bbca4ffc6724101))
### BREAKING CHANGES
* 更新 `react-router` 到 v6
### Dependencies
* **@mxjs/router:** upgrade from `0.2.16` to `0.3.0`
## [0.2.16](https://github.com/miaoxing/miaoxing/compare/@mxjs/router-modal@0.2.15...@mxjs/router-modal@0.2.16) (2024-03-31)

@@ -2,0 +23,0 @@

75

ModalEvent.js

@@ -1,48 +0,53 @@

import React from 'react';
import {withRouter} from 'react-router';
import { useEffect } from 'react';
import PropTypes from 'prop-types';
import { history } from '@mxjs/app';
export default @withRouter
class ModalEvent extends React.Component {
static defaultProps = {
onEnter: null,
onExit: null,
};
const ModalEvent = ({ children, onEnter, onExit }) => {
let isModal = false;
static propTypes = {
history: PropTypes.object,
children: PropTypes.node,
};
useEffect(() => {
const handleEvent = (location, action) => {
const nextIsModal = location.state && location.state.modal;
isModal;
componentDidMount() {
this.unlisten = this.props.history.listen((location, action) => {
const isModal = location.state && location.state.modal;
if (this.isModal && !isModal) {
this.handleEvent('onExit', location, action);
if (isModal && !nextIsModal) {
callEvent(onExit, location, action);
}
if (!this.isModal && isModal) {
this.handleEvent('onEnter', location, action);
if (!isModal && nextIsModal) {
callEvent(onEnter, location, action);
}
this.isModal = isModal;
isModal = nextIsModal;
};
const unlisten = history.listen((location, action) => {
handleEvent(location, action);
});
}
componentWillUnmount() {
this.unlisten();
}
return () => {
unlisten();
};
}, [history, onEnter, onExit]);
handleEvent(event, location, action) {
if (this.props[event]) {
this.props[event](location, action);
const callEvent = (event, location, action) => {
if (event) {
event(location, action);
}
}
};
render() {
return this.props.children || '';
}
}
return children || '';
};
ModalEvent.defaultProps = {
onEnter: null,
onExit: null,
};
ModalEvent.propTypes = {
history: PropTypes.object.isRequired,
children: PropTypes.node,
onEnter: PropTypes.func,
onExit: PropTypes.func,
};
export default ModalEvent;

@@ -1,75 +0,70 @@

import React from 'react';
import {Switch, withRouter} from 'react-router';
import { useEffect, useState } from 'react';
import { Switch, useLocation } from 'react-router';
import ModalView from './ModalView';
import PropTypes from 'prop-types';
import ModalContext from '@mxjs/router/ModalContext';
import { history } from '@mxjs/app';
export {ModalContext};
export { ModalContext };
/**
* @link https://reacttraining.com/react-router/web/example/modal-gallery
*/
export default @withRouter
class ModalSwitch extends React.Component {
static propTypes = {
location: PropTypes.object,
history: PropTypes.object,
children: PropTypes.node,
};
const ModalSwitch = ({ children }) => {
const location = useLocation();
const [length, setLength] = useState(1);
const [previousLocation, setPreviousLocation] = useState(location);
length = 1;
previousLocation = this.props.location;
useEffect(() => {
const handleUpdate = (nextLocation) => {
if (history.action !== 'POP' && (!nextLocation.state || !nextLocation.state.modal)) {
setPreviousLocation(location);
}
UNSAFE_componentWillUpdate(nextProps) {
let {location} = this.props;
const modal = location.state && location.state.modal;
const nextModal = nextLocation.state && nextLocation.state.modal;
// set previousLocation if props.location is not modal
if (
nextProps.history.action !== 'POP' &&
(!location.state || !location.state.modal)
) {
this.previousLocation = this.props.location;
}
if (modal && nextModal) {
if (history.action === 'PUSH') {
// modal 中下一页
setLength(length + 1);
} else if (history.action === 'POP') {
// modal 中上一页
setLength(length - 1);
}
}
const modal = location.state && location.state.modal;
const nextModal = nextProps.location.state && nextProps.location.state.modal;
if (modal && nextModal) {
if (nextProps.history.action === 'PUSH') {
// modal 中下一页
this.length++;
} else if (nextProps.history.action === 'POP') {
// modal 中上一页
this.length--;
if (!nextModal) {
setLength(1);
}
}
};
if (!nextModal) {
// 退出 modal
this.length = 1;
}
}
history.listen(handleUpdate);
isModal() {
let {location} = this.props;
return location.state &&
location.state.modal &&
this.previousLocation !== location; // not initial render
}
return () => {
history.listen(handleUpdate);
};
}, [location, history, length]);
render() {
const isModal = this.isModal();
const isModal = () => {
return location.state && location.state.modal && previousLocation !== location;
};
return (
<ModalContext.Provider value={{isModal: isModal}}>
<Switch location={isModal ? this.previousLocation : this.props.location}>
{this.props.children}
</Switch>
{isModal && <ModalView length={this.length}>
<Switch>
{this.props.children}
</Switch>
</ModalView>}
</ModalContext.Provider>
);
}
}
return (
<ModalContext.Provider value={{ isModal: isModal() }}>
<Switch location={isModal() ? previousLocation : location}>
{children}
</Switch>
{isModal() && (
<ModalView length={length}>
<Switch>{children}</Switch>
</ModalView>
)}
</ModalContext.Provider>
);
};
ModalSwitch.propTypes = {
location: PropTypes.object,
history: PropTypes.object,
children: PropTypes.node,
};
export default ModalSwitch;

@@ -1,44 +0,36 @@

import React from 'react';
import {Modal} from 'react-bootstrap';
import {withRouter} from 'react-router';
import { useState } from 'react';
import { Modal } from 'react-bootstrap';
import PropTypes from 'prop-types';
export default @withRouter
class ModalView extends React.Component {
static defaultProps = {
length: 1,
};
const ModalView = ({ history, length = 1, children }) => {
const [show, setShow] = useState(true);
static propTypes = {
history: PropTypes.object,
length: PropTypes.number,
children: PropTypes.node,
const handleHide = () => {
setShow(false);
};
state = {
show: true,
const handleExited = () => {
history.go(-length);
};
handleHide = () => {
this.setState({show: false});
};
return (
<Modal
show={show}
onHide={handleHide}
onExited={handleExited}
className="modal-right"
>
<Modal.Body className="page-content">
{children}
</Modal.Body>
</Modal>
);
};
handleExited = () => {
this.props.history.go(-this.props.length);
};
ModalView.propTypes = {
history: PropTypes.object,
length: PropTypes.number,
children: PropTypes.node,
};
render() {
return (
<Modal
show={this.state.show}
onHide={this.handleHide}
onExited={this.handleExited}
className="modal-right"
>
<Modal.Body className="page-content">
{this.props.children}
</Modal.Body>
</Modal>
);
}
}
export default ModalView;
{
"name": "@mxjs/router-modal",
"version": "0.2.16",
"version": "0.3.0",
"main": "index.js",

@@ -13,5 +13,5 @@ "license": "MIT",

"prop-types": "^15.7.2",
"react-router": "^5.2.0",
"react-router-dom": "^5.2.0",
"@mxjs/router": "^0.2.16"
"react-router": "^6",
"react-router-dom": "^6",
"@mxjs/router": "^0.3.0"
},

@@ -18,0 +18,0 @@ "devDependencies": {

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc