繁体   English   中英

需要SelectBoxIt和ReactJS支持

[英]SelectBoxIt & ReactJS support needed

SelectBoxIt允许您使用华丽且功能丰富的下拉菜单替换难看且难以使用的 HTML选择框。

我实现了SelectBoxIt,而我的render(return())方法中的<select>无法识别它。 我在网上搜索后,发现Github上几乎只有一个我不理解其建议的资源,在这一点上您可以为我提供帮助。

简而言之,我得出的结论是SelectBoxIt自然不支持ReactJS。

因此,我们需要一个解决方案或技巧。

以下是他们网站上的示例代码,并在我的项目中完全使用了它:

        <!doctype html>
    <html>
    <head>
      <meta charset=utf-8>
      <title>SelectBoxIt demo</title>
      <link rel="stylesheet" href="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.2.2/css/bootstrap-combined.min.css" />
      <link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.9.2/themes/base/jquery-ui.css" />
      <link type="text/css" rel="stylesheet" href="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.css" />
      <link rel="stylesheet" href="http://gregfranko.com/jquery.selectBoxIt.js/css/jquery.selectBoxIt.css" />
    </head>
    <body>

      <select name="test">
        <option value="SelectBoxIt is:">SelectBoxIt is:</option>
        <option value="a jQuery Plugin">a jQuery Plugin</option>
        <option value="a Select Box Replacement">a Select Box Replacement</option>
        <option value="a Stateful UI Widget">a Stateful UI Widget</option>
      </select>

      <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
      <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.9.2/jquery-ui.min.js"></script>
      <script src="http://gregfranko.com/jquery.selectBoxIt.js/js/jquery.selectBoxIt.min.js"></script>
      <script>
        $(function() {

          var selectBox = $("select").selectBoxIt();

        });
      </script>
    </body>
    </html>

以下select不受影响:

<select 
                id="sel1" 
                // ref={s => { this.selectBox = s; }}
                onChange={ event => this.onSelectChange(event.target.value) }
                >
                {options}
            </select>

<select name="test">把它时,里面已经工作render(return())但在<select id="sel1" >内部render(return())没有被改变为一个SelectBoxIt形状。 请注意,我评论了ref={s => { this.selectBox = s; }} ref={s => { this.selectBox = s; }}只是为了防止显示错误。

由于这是一个jquery插件,因此您需要确保已安装它:

npm install -S jquery

然后,您需要在脚本中导入它:

import * as $ from 'jquery';

现在,在您的react组件中,在componentDidMount中应用jquery初始化。 唯一的问题是您需要使用ref查找节点。

render() {
  return <select ref={s => { this.selectBox = s; }} />
}

componentDidMount() {
  $(this.selectBox).selectBoxIt();
}

这里有一些参考。

我发现reactJS不会“捕获”被SelectBoxIt替换的select onChange ,但是会“捕获” onClick 因此,对于一个使用SelectBoxIt + ReactJS项目,当select值更改并与react onChange事件结合使用时,我从jQueryonClick触发器。

在脚本标签/index.html/中:

$("select").on('change',function(){
      $("select").trigger('click');
});

reactJS /SelectBox.js/中:

onClickHandled检查值是否更改。

此处的示例: https : //github.com/gshishkov/selectBoxIt-reactjs

我知道这不是很好的解决方案,但是它是最快且可行的解决方案。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM