我将Material UI的Autocomplete
与Reagent(ClojureScript)结合使用时遇到问题。元素呈现良好,但是当我尝试单击它时,出现以下异常:
Uncaught TypeError: Cannot read property 'focus' of null
at handleClick (useAutocomplete.js:938)
at HTMLUnknownElement.callCallback (react-dom.development.js:189)
at Object.invokeGuardedCallbackImpl (react-dom.development.js:238)
at invokeGuardedCallback (react-dom.development.js:293)
at invokeGuardedCallbackAndCatchFirstError (react-dom.development.js:307)
at executeDispatch (react-dom.development.js:390)
at executeDispatchesAndReleaseTopLevel (react-dom.development.js:412)
at forEachAccumulated (react-dom.development.js:3260)
at runEventsInBatch (react-dom.development.js:3305)
at handleTopLevel (react-dom.development.js:3515)
useAutocomplete.js:322 Uncaught TypeError: Cannot read property 'removeAttribute' of null
at eval (useAutocomplete.js:322)
at eval (useEventCallback.js:26)
at eval (useAutocomplete.js:433)
at eval (useEventCallback.js:26)
at eval (useAutocomplete.js:463)
at eval (useAutocomplete.js:528)
at commitHookEffectListMount (react-dom.development.js:19765)
at commitPassiveHookEffects (react-dom.development.js:19803)
at HTMLUnknownElement.callCallback (react-dom.development.js:189)
at Object.invokeGuardedCallbackImpl (react-dom.development.js:238)
在JS调试器中,我看到inputRef.current
为null(这是调用focus
和removeAttribute
的地方。(奇怪的是,唯一inputRef
的地方是通过调用useRef(null)
来设置文件中的值,这导致inputRef.current
为空。)
在我的代码中,我定义了“自动完成”字段,如下所示:
(ns my-product-redacted.views.atoms
(:require [reagent.core :as r]
["@material-ui/lab/Autocomplete" :default Autocomplete]
;; other requires
))
(def autocomplete-field (r/adapt-react-class Autocomplete))
然后,在React组件中,其用法如下:
[a/autocomplete-field {:render-input (fn [js-params]
(let [clj-params (js->clj js-params)
params {:label label
:width width
:select select?
:Input-label-props {:shrink true}
:Select-props {:native true}}
all-params (into clj-params params)]
(js/console.log (clj->js all-params))
(r/as-element [a/text-field all-params])))
:options (when select? (cons {:value "" :label ""} options))
:get-option-label (fn [option] (or (get (js->clj option) "label") ""))
:default-value (when (not select?) value-override)
:value (when select? value)
:disabled disabled?
:on-focus #(re-frame/dispatch [::forms/on-focus path])
:on-blur #(re-frame/dispatch [::forms/on-blur path])
:on-change #(re-frame/dispatch (conj on-change (-> % .-target .-value)))})]
(这里,a/text-field
也以与a/autocomplete-field
相同的名称空间定义,并且以类似的方式。)
JS控制台日志(来自(js/console.log (clj->js params))
调用)显示inputProps.ref.current
设置为null。但是,InputProps.ref
不为null。即便如此,我还是尝试将与InputProps.ref
传递给inputProps.ref.current
的相同函数进行手动关联,但这没什么区别。
我还尝试了https://github.com/mui-org/material-ui/issues/21245中建议的解决方法(尽管该问题与Gestalt库有关,而不与Reagent有关,它表明引用转发可能存在问题)。但是,将text-field
包裹到div中,并从ref
提取InputProps.ref
也没什么区别。
有什么建议吗?
答案 0 :(得分:4)
在渲染输入js参数上调用js->clj
会破坏它们。
我在示例存储库here中对实质性UI自动完成组件进行了快速演示。
但这主要来自官方试剂文档/示例here:
(defn autocomplete-example []
[:> mui/Grid
{:item true}
[:> Autocomplete {:options ["foo" "bar" "foobar"]
:style {:width 300}
;; Note that the function parameter is a JS Object!
;; Autocomplete expects the renderInput value to be function
;; returning React elements, not a component!
;; So reactify-component won't work here.
:render-input (fn [^js params]
;; Don't call js->clj because that would recursively
;; convert all JS objects (e.g. React ref objects)
;; to Cljs maps, which breaks them, even when converted back to JS.
;; Best thing is to use r/create-element and
;; pass the JS params to it.
;; If necessary, use JS interop to modify params.
(set! (.-variant params) "outlined")
(set! (.-label params) "Autocomplete")
(r/create-element mui/TextField params))}]])