Skip to content

Commit

Permalink
fix(Modal): when platform=cross, modal will change style from android…
Browse files Browse the repository at this point in the history
… to ios before close in android

close #1433
  • Loading branch information
paranoidjk committed Jun 9, 2017
1 parent efde632 commit 2c79a05
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 10 deletions.
29 changes: 23 additions & 6 deletions components/modal/Modal.web.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ import assign from 'object-assign';
import Touchable from 'rc-touchable';
import ModalProps from './PropsType';

function checkIfAndroid(platform) {
return platform === 'android' ||
(platform === 'cross' && typeof window !== 'undefined' && !!navigator.userAgent.match(/Android/i));
}

export default class Modal extends React.Component<ModalProps, any> {
static defaultProps = {
prefixCls: 'am-modal',
Expand All @@ -18,12 +23,18 @@ export default class Modal extends React.Component<ModalProps, any> {
operation: false,
platform: 'cross',
};

constructor(props) {
super(props);
this.state = {
// in ssr, just set isAndroid false
// since modal normally won't show at first render, componentDidMount will do double check
isAndroid: checkIfAndroid(props.platform),
};
}
isInModal(e) {
if (!/\biPhone\b|\biPod\b/i.test(navigator.userAgent)) {
return;
}

// fix touch to scroll background page on iOS
const prefixCls = this.props.prefixCls;
const pNode = (node => {
Expand Down Expand Up @@ -69,7 +80,14 @@ export default class Modal extends React.Component<ModalProps, any> {
</Touchable>
);
}

componentDidMount() {
const isAndroid = checkIfAndroid(this.props.platform);
if (isAndroid !== this.state.isAndroid) {
this.setState({
isAndroid,
});
}
}
render() {
const {
prefixCls,
Expand All @@ -82,11 +100,10 @@ export default class Modal extends React.Component<ModalProps, any> {
footer = [],
closable,
operation,
platform,
} = this.props;

const isAndroid = platform === 'android' ||
(platform === 'cross' && this.props.visible && !!navigator.userAgent.match(/Android/i));
const { isAndroid } = this.state;

const wrapCls = classNames({
[className as string]: !!className,
[`${prefixCls}-transparent`]: transparent,
Expand Down
4 changes: 3 additions & 1 deletion components/modal/alert.web.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ export default function a(...args) {

function close() {
ReactDOM.unmountComponentAtNode(div);
div.parentNode.removeChild(div);
if (div && div.parentNode) {
div.parentNode.removeChild(div);
}
}

const footer = actions.map((button) => {
Expand Down
3 changes: 2 additions & 1 deletion components/modal/demo/alert.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ const showAlert = () => {
]);
setTimeout(() => {
// 可以调用close方法以在外部close
console.log('auto close');
alertInstance.close();
}, 1000);
}, 2000);
};

const App = () => (
Expand Down
4 changes: 3 additions & 1 deletion components/modal/operation.web.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ export default function a(...args) {

function close() {
ReactDOM.unmountComponentAtNode(div);
div.parentNode.removeChild(div);
if (div && div.parentNode) {
div.parentNode.removeChild(div);
}
}

const footer = actions.map((button) => {
Expand Down
4 changes: 3 additions & 1 deletion components/modal/prompt.web.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,9 @@ export default function a(...args) {

function close() {
ReactDOM.unmountComponentAtNode(div);
div.parentNode.removeChild(div);
if (div && div.parentNode) {
div.parentNode.removeChild(div);
}
}

function getArgs(func) {
Expand Down

2 comments on commit 2c79a05

@paranoidjk
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cc @cncolder this bug was introduce by ssr change, you can review this commit when you got time.

@cncolder
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's better than before. Fully works in ssr mode now.

test('Modal', t => t.truthy(render(<Modal visible />)))
...
✔ antd-mobile › Modal
...
47 tests passed

Please sign in to comment.