🐛 Fix differences between on-option-enter and on-option-click

This commit is contained in:
Eva Marco
2026-02-26 12:55:51 +01:00
parent f770585f68
commit f73d2fe0dd
2 changed files with 36 additions and 23 deletions

View File

@@ -137,10 +137,15 @@
(mf/deps value resolve-stream name)
(fn [id]
(let [input-node (mf/ref-val ref)
final-val (tp/select-option-by-id id options-ref input-node value)]
(when final-val
(fm/on-input-change form name final-val true)
(rx/push! resolve-stream final-val))
{:keys [value cursor]} (tp/select-option-by-id id options-ref input-node value)]
(when value
(fm/on-input-change form name value true)
(rx/push! resolve-stream value)
(js/setTimeout
(fn []
(set! (.-selectionStart input-node) cursor)
(set! (.-selectionEnd input-node) cursor))
0))
(reset! filter-term* "")
(reset! is-open* false))))
@@ -179,19 +184,21 @@
(let [input-node (mf/ref-val ref)
node (dom/get-current-target event)
id (dom/get-data node "id")
final-val (tp/select-option-by-id id options-ref input-node value)]
{:keys [value cursor]} (tp/select-option-by-id id options-ref input-node value)]
(reset! filter-term* "")
(dom/focus! input-node)
(when final-val
(when value
(reset! is-open* false)
(fm/on-input-change form name final-val true)
(rx/push! resolve-stream final-val)
(fm/on-input-change form name value true)
(rx/push! resolve-stream value)
(let [new-cursor (+ (str/index-of final-val "}") 1)]
(set! (.-selectionStart input-node) new-cursor)
(set! (.-selectionEnd input-node) new-cursor))))))
(js/setTimeout
(fn []
(set! (.-selectionStart input-node) cursor)
(set! (.-selectionEnd input-node) cursor))
0)))))
hint*
(mf/use-state {})

View File

@@ -55,18 +55,25 @@
(defn replace-active-token
"Replaces the token at the cursor with `{new-name}`.
If no valid token is active, inserts the new token at the cursor position."
Returns {:value :cursor} with the updated value and new cursor position."
[value cursor new-name]
(if-let [{:keys [start end]}
(find-active-token-range value cursor)]
(let [new-token (str "{" new-name "}")]
(if-let [{:keys [start end]}
(find-active-token-range value cursor)]
(str (subs value 0 start)
"{" new-name "}"
(subs value end))
(let [new-value (str (subs value 0 start)
new-token
(subs value end))
new-cursor (+ start (count new-token))]
{:value new-value
:cursor new-cursor})
(str (subs value 0 cursor)
"{" new-name "}"
(subs value cursor))))
(let [new-value (str (subs value 0 cursor)
new-token
(subs value cursor))
new-cursor (+ cursor (count new-token))]
{:value new-value
:cursor new-cursor}))))
(defn active-token [value input-node]
(let [cursor (dom/selection-start input-node)]
@@ -87,6 +94,5 @@
options (if (delay? options) @options options)
option (get-option options id)
name (:name option)
final-val (replace-active-token value cursor name)]
final-val))
name (:name option)]
(replace-active-token value cursor name)))