Skip to content

Commit

Permalink
use document.activeElement, copy select value
Browse files Browse the repository at this point in the history
  • Loading branch information
SteffenDE committed May 3, 2024
1 parent 7218d2e commit 719ea4a
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 38 deletions.
7 changes: 2 additions & 5 deletions assets/js/phoenix_live_view/dom_patch.js
Original file line number Diff line number Diff line change
Expand Up @@ -411,11 +411,8 @@ export default class DOMPatch {
if(!(fromEl instanceof HTMLSelectElement) || fromEl.multiple){ return false }
if(fromEl.options.length !== toEl.options.length){ return true }

let fromSelected = fromEl.selectedOptions[0]
let toSelected = toEl.selectedOptions[0]
if(fromSelected && fromSelected.hasAttribute("selected")){
toSelected.setAttribute("selected", fromSelected.getAttribute("selected"))
}
// keep the current value
toEl.value = fromEl.value

// in general we have to be very careful with using isEqualNode as it does not a reliable
// DOM tree equality check, but for selection attributes and options it works fine
Expand Down
22 changes: 1 addition & 21 deletions assets/js/phoenix_live_view/live_socket.js
Original file line number Diff line number Diff line change
Expand Up @@ -476,25 +476,8 @@ export default class LiveSocket {
}
}

setActiveElement(target){
if(this.activeElement === target){ return }
this.activeElement = target
let cancel = () => {
if(target === this.activeElement){ this.activeElement = null }
target.removeEventListener("mouseup", this)
target.removeEventListener("touchend", this)
}
target.addEventListener("mouseup", cancel)
target.addEventListener("touchend", cancel)
}

getActiveElement(){
if(document.activeElement === document.body){
return this.activeElement || document.activeElement
} else {
// document.activeElement can be null in Internet Explorer 11
return document.activeElement || document.body
}
return document.activeElement
}

dropActiveElement(view){
Expand Down Expand Up @@ -877,9 +860,6 @@ export default class LiveSocket {
this.debounce(input, e, type, () => {
this.withinOwners(dispatcher, view => {
DOM.putPrivate(input, PHX_HAS_FOCUSED, true)
if(!DOM.isTextualInput(input)){
this.setActiveElement(input)
}
JS.exec("change", phxEvent, view, input, ["push", {_target: e.target.name, dispatcher: dispatcher}])
})
})
Expand Down
13 changes: 1 addition & 12 deletions assets/test/live_socket_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,17 +167,6 @@ describe("LiveSocket", () => {
expect(liveSocket.getActiveElement()).toEqual(input)
})

test("setActiveElement and getActiveElement", async () => {
let liveSocket = new LiveSocket("/live", Socket)

let input = document.querySelector("input")

// .activeElement
liveSocket.setActiveElement(input)
expect(liveSocket.activeElement).toEqual(input)
expect(liveSocket.getActiveElement()).toEqual(input)
})

test("blurActiveElement", async () => {
let liveSocket = new LiveSocket("/live", Socket)

Expand Down Expand Up @@ -215,7 +204,7 @@ describe("LiveSocket", () => {
liveSocket.connect()

let input = document.querySelector("input")
liveSocket.setActiveElement(input)
input.focus()
liveSocket.blurActiveElement()
expect(liveSocket.prevActive).toEqual(input)

Expand Down

0 comments on commit 719ea4a

Please sign in to comment.