# IFrame 嵌套页面

标准 iframe 组件，用于嵌入其它页面

* 嵌入页面 [iframe](https://developer.mozilla.org/zh-CN/docs/Web/HTML/Element/iframe)
* 跨页面通信 [postMessage](https://developer.mozilla.org/zh-CN/docs/Web/API/Window/postMessage)

```js
import { IFrame } from 'amos-framework';
```

## 案例演示

### 基本使用

---demo
```js
import { IFrame } from 'amos-framework';

ReactDOM.render((
  <IFrame
    url="https://www.baidu.com/"
    width={850}
    height={650}
    id="iframeDemo"
    className="iframe-demo"
    display="initial"
    position="relative"
    allowFullScreen
    scrolling="auto"
  />
), _react_runner_);
```
---demoend

### 启用加载动画&页面间通信

---demo
```js
import { IFrame, NestedSpin, Spin } from 'amos-framework';

class Demo extends Component {
  constructor(props) {
    super(props);
    this.state = {
      loading: true
    };
  }

  handleMessage = (data) => {
    console.log(data);
    if (data.loaded){
      this.setState({
        loading: false
      });
    }
  }

  handleCreated = (ifrDoc, ifr) => {
    this.ifrDoc = ifrDoc;
    this.ifr = ifr;
    // 主窗口向iframe窗口发送信息
    // this.ifr.contentWindow..postMessage('from main window', '*');;
  }

  handleLoaded = () => {
    const me = this;
    const loading = me.ifrDoc.getElementById('startload');
    loading && loading.remove();
  }

  render() {
    return (
      <NestedSpin
        spinning={this.state.loading}
        interceptClassName
        indicator={<Spin type="scale" color="#345fa6" />}
      >
        <IFrame
          url="/demo/test.html"
          width={600}
          height={400}
          id="iframeDemo2"
          className="iframe-demo"
          display="initial"
          position="relative"
          allowFullScreen
          scrolling="auto"
          onMessage={this.handleMessage}
          onCreated={this.handleCreated}
          onLoaded={this.handleLoaded}
        />
      </NestedSpin>
    );
  }
}

ReactDOM.render(<Demo />, _react_runner_);
```
---demoend

- test.html

```html
<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="renderer" content="webkit">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>测试</title>

</head>

<body>
  <div id="startload"
    style="height:100%;width:100%;background:#fefefe;position:fixed;z-index:9999;text-align:center;padding-top:200px;left:0;top:0">
    <img src="./toip-loading.gif" width="100px">
  </div>
  <script>
    // 判断当前页面是否在 iframe 中
    if (top.location !== location || self != top || (self.frameElement && self.frameElement.tagName == "IFRAME")) {
      document.getElementById("startload").remove();
    }
  </script>
  <div id="app"></div>
  <script>
    window.onload = function () {
      if (document.getElementById("startload")){
        document.getElementById("startload").style.display = "none";
      }
      // 嵌入的方式
      else {
        // 注意，此处给说发消息，则需要采用谁，top 代表父窗口， 如果用 window 的话，就是自身了
        // 模拟延迟
        setTimeout(() => {
          top.postMessage({ type: 'af-iframe', loaded: true }, '*');
        }, 3000);
      }
    }
  </script>
</body>

</html>

```

## props

| params | type | default | description |
|--------- |-------- |--------- |-------- |
| url | String | - | 页面地址,设置应被加载到框架中的文档的 URL |
| id | String | - | html attr id |
| className | String | - | 自定义样式名 |
| width | `String or number` | `100%` | 定义 iframe 的宽度。 |
| height | String | `100%` | 规定 iframe 的高度。 |
| position | String | `absolute` | 定位属性 |
| display | String | `block` | display 属性 |
| name | String | '' | 规定 iframe 的名称，设置框架的名称 |
| allowFullScreen | bool | false | display 属性 |
| target | String | `_parent` | target 属性 |
| frameBorder | number | 0 | 设置是否显示框架周围的边框 |
| scrolling | string | `no` | 设置框架是否可拥有滚动条。 可选值 `yes, no, auto` |
| onCreated | `func: (ifrDocumemt, ifr) => {}` | - | 组件加载完成之后的回调 |
| onMessage | `func: (data) => {}` | - | 接收消息，仅接收 `type='af-iframe'` 类型数据 |
| onLoaded | `func: (evt, ifr)` | - | iframe 页面 onLoad 回调 |
