duuliy

看了antd源码后...

2019-6-25

  看了部分源码,原来antd很多方法功能都是rc 组件库提供的 比如rc-form rc-table,自己也尝试在rc库上面二次封装storybook,简直不要太方便

学习到:
1.cls 巧用,
2.组件巧妙createContext context传数据,
3.static 直接用拿子组件
4.高度统一的less
5.createDOMForm (来自rc-form)巧妙依赖注入props
6.form的几种提交方式
7.className组件层级添加
8. 标签原生事件直接方法不处理返回方式(巧用)
9.storybook 发布自己框架
10.弹窗以及气泡定位body响应位置 避免overflow:hidden ,是定位坐标吗?
11.react 的render,cloneElement等操作dom方法
12.16.3新生命周期getDerivedStateFromProps 等用法
13.modal创建在root之外的节点方法:react.createPortal
14.dangerouslySetInnerHTML :它的 prop 值( 一个对象而不是字符串 )应该被用来表明净化后的数据。

1.form表单:
getFieldProps默认是由onchange触发的(隐式传递),把原生事件的onchange覆盖了

2.为啥有些设置className,有些起作用在根标签上有些不起作用?
antd封装的组件上的className传递到组件内部,然后手动添加到一些组件上面,可能是最底层的也可能是最外层的。所有有时间因为层级关系就没起作用。

3.form 表单是功能最麻烦的
http://react-component.github.io/form/ rc-form
虽然antd引用rc库,rc库实现的原理是:
eg:
getFieldsValue 可通过Input组件的 双向绑定拿到的val 或者直接 $(“input[name=’firstname’]”).val(); js粗暴的拿到。
其他方法请举一反三。。

6.无刷新页面提交表单
表单可实现无刷新页面提交,无需页面跳转,如下,通过一个隐藏的iframe实现,form表单的target设置为iframe的name名称,
form提交目标位当前页面iframe则不会刷新页面

<form action="/url.do" method="post" target="targetIfr">
<input type="text" name="name"/>
</form>   
<iframe name="targetIfr" style="display:none"></iframe> 

通过type=submit提交
一般表单提交通过type=submit实现,input type=”submit”,浏览器显示为button按钮,通过点击这个按钮提交表单数据跳转到/url.do

<form action="/url.do" method="post">
   <input type="text" name="name"/>
   <input type="submit" value="提交">
</form>

js提交form表单
js事件触发表单提交,通过button、链接等触发事件,js调用submit()方法提交表单数据,jquery通过submit()方法

<form id="form" action="/url.do" method="post">
   <input type="text" name="name"/>
</form>

js: document.getElementById(“form”).submit();
jquery: $(“#form”).submit();
ajax异步提交表单数据
采用ajax异步方式,通过js获取form中所有input、select等组件的值,将这些值组成Json格式,通过异步的方式与服务器端进行交互,
一般将表单数据传送给服务器端,服务器端处理数据并返回结果信息等

<form id="form"  method="post">
   <input type="text" name="name" id="name"/>
</form>
  var params = &#123;"name", $("#name").val()&#125;
 $.ajax(&#123;
      type: "POST",
      url: "/url.do",
      data: params,
      dataType : "json",
      success: function(respMsg)&#123;
      &#125;
   &#125;);

页面无跳转
如果通过form表单提交请求服务端去下载文件,这时当前页面不会发生跳转,服务端返回void,通过response 去写文件数据,
页面会显示下载文件。

<form action="/url.do" method="post">
   <input type="text" name="name"/>
   <input type="submit" value="提交">
</form>

@RequestMapping(value = "/url")
    public void exportFile(HttpServletRequest req, HttpServletResponse response, String rptId)
            throws Exception &#123;
        OutputStream out = null;
        try &#123;
            String rptName = "file";
            String fileName = new String((rptName + excelAble.getFileSuffix()).getBytes("GBK"),
                    "8859_1");
            response.reset();
            response.setContentType("application/octec-stream");
            response.setHeader("Content-disposition", "attachment; filename=" + fileName);
            out = response.getOutputStream();
            excelAble.exportFile(out);
        &#125; catch (Exception e) &#123;
            logger.error(e);
        &#125; finally &#123;
            if (out != null) &#123;
                out.close();
            &#125;
        &#125;
    &#125;

form表单上传文件
使用form表单进行上传文件需要为form添加enctype=”multipart/form-data” 属性,除此之外还需要将表单的提交方法改成post,
如下 method=”post”, input type的类型需要设置为file

 <form action="/url.do" enctype="multipart/form-data" method="post">
     <input type="file" name="name"/>
     <input type="submit" value="提交">
   </form>