我在使用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
为null。)
在我的代码中,我将自动完成字段定义如下:
(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,但它表明有可能存在ref转发问题)。但是,将text-field
包装在从 InputProps.ref
中取出的div中也没有任何影响。
有什么建议吗?