首先,此https://github.com/taylorSando/om-material-ui不适用于最新的React / Material UI。 我认为,主要原因是在控制台中发出此警告:
Warning: Something is calling a React component directly. Use a factory or JSX instead. See: https://fb.me/react-legacyfactory
我还试图创建组件"手动":
(ns om-test.core
(:require [om.core :as om :include-macros true]
[om-tools.dom :as dom :include-macros true]
[om-tools.core :refer-macros [defcomponent]]
[om-material-ui.core :as mui :include-macros true]))
(enable-console-print!)
(defonce app-state (atom {:text "Hello Chestnut!"}))
(defn main []
(om/root
(fn [app owner]
(reify
om/IRender
(render [_]
(dom/div (dom/element js/MaterialUI.Paper {} "Hello")
(mui/paper {} "Hello"))
)))
app-state
{:target (. js/document (getElementById "app"))}))
因此,这两种方法都会产生相同的警告。
React显然有一些变化。它建议以编程方式创建组件:
var React = require('react');
var MyComponent = React.createFactory(require('MyComponent'));
function render() {
return MyComponent({ foo: 'bar' });
}
那么如何在Om渲染函数中创建Material UI组件,或者更好我如何在Om渲染函数中创建React组件呢?
按材质用户界面我的意思是https://github.com/callemall/material-ui
我的依赖
:dependencies [[org.clojure/clojure "1.6.0"]
[org.clojure/clojurescript "0.0-3058" :scope "provided"]
[ring "1.3.2"]
[ring/ring-defaults "0.1.4"]
[compojure "1.3.2"]
[enlive "1.1.6"]
[org.omcljs/om "0.9.0"]
[environ "1.0.0"]
[http-kit "2.1.19"]
[prismatic/om-tools "0.3.11"]
[om-material-ui "0.1.1" :exclusions [org.clojure/clojurescript
org.clojure/clojure]]]
答案 0 :(得分:7)
好吧,我最终想通了。
material.js
。同样,不需要包含CSS。现在 Om 的代码:
(ns material-ui-test.core
(:require [om.core :as om :include-macros true]
[om.dom :as dom :include-macros true]))
(enable-console-print!)
(defonce app-state (atom {:text "Hello Chestnut!"}))
(def ^:dynamic *mui-theme*
(.getCurrentTheme (js/MaterialUI.Styles.ThemeManager.)))
(defn main []
(om/root
(fn [app owner]
(reify
om/IRender
(render [_]
(let [ctor (js/React.createFactory
(js/React.createClass
#js
{:getDisplayName (fn [] "muiroot-context")
:childContextTypes #js {:muiTheme js/React.PropTypes.object}
:getChildContext (fn [] #js {:muiTheme *mui-theme*})
:render (fn []
(dom/div nil
(dom/h1 nil (:text app))
(js/React.createElement js/MaterialUI.Slider)))}))]
(ctor. nil)))))
app-state
{:target (. js/document (getElementById "app"))}))
如果您仅使用(js/React.createElement js/MaterialUI.Slider)
而没有使用:getChildContext
等,则会抛出错误:
Uncaught TypeError: Cannot read property 'component' of undefined
这是因为当前的MaterialUI工作原理。阅读"用法"部分在这里:http://material-ui.com/#/customization/themes
试剂的代码更优雅。但是我在这里使用了命名空间
[material-ui.core :as ui :include-macros true]
从此示例项目中复制粘贴:https://github.com/tuhlmann/reagent-material
(def ^:dynamic *mui-theme*
(.getCurrentTheme (js/MaterialUI.Styles.ThemeManager.)))
(defn main-panel []
(let [active-panel (rf/subscribe [:active-panel])]
(r/create-class
{:display-name "Main Panel"
:child-context-types
#js {:muiTheme js/React.PropTypes.object}
:get-child-context
(fn [this]
#js {:muiTheme *mui-theme*})
:reagent-render
(fn []
[ui/Slider {:name "slide1"}])})))
编辑:我发布了库,大大简化了整个过程。
图书馆:https://github.com/madvas/cljs-react-material-ui
示例应用:https://github.com/madvas/cljs-react-material-ui-example
答案 1 :(得分:0)
我没有使用Material UI,而是React Widgets。这是我需要为om写的wrapper:
(defn dropdown-list
[data owner {:keys [val-key menu-key id-key label-key props]}]
(reify
om/IRender
(render [_]
(let [menu (-get data menu-key)]
(js/React.createElement js/ReactWidgets.DropdownList
(-> {:defaultValue (-> (find-by-key menu id-key (-get data val-key))
(-get label-key))
:data (mapv #(-get % label-key) menu)
:onChange (fn [new-val]
(let [new-id (-> (find-by-key menu label- key new-val)
(-get id-key))]
(om/update! data val-key new-id)))}
(merge props)
clj->js))))))
因此,一般情况下,您需要获取React类(js/ReactWidgets.DropdownList
)并在js/Readt.createElement
上传递道具时调用render
。