Current section
Files
Jump to
Current section
Files
priv/static/live_select.min.js
export default{LiveSelect:{textInput(){return this.el.querySelector("input[type=text]")},maybeStyleClearButton(){const t=this.el.querySelector("button[phx-click=clear]");t&&(this.textInput().style.position="relative",t.style.position="absolute",t.style.top="0px",t.style.bottom="0px",t.style.right="5px")},attachDomEventHandlers(){this.textInput().onkeydown=e=>{e.code==="Enter"&&e.preventDefault(),this.pushEventTo(this.el,"keydown",{key:e.code})};const t=this.el.querySelector("ul");t&&(t.onmousedown=e=>{e.target.dataset.idx&&(this.textInput().blur(),this.pushEventTo(this.el,"option_click",{idx:e.target.dataset.idx}))})},setInputValue(t,{focus:e}){this.textInput().value=t,e&&this.textInput().focus()},inputEvent(t,e){const l=e==="single"?"input[class=single-mode]":t.length===0?"input[name=live_select_empty_selection]":"input[type=hidden]";this.el.querySelector(l).dispatchEvent(new Event("input",{bubbles:!0}))},mounted(){this.maybeStyleClearButton(),this.handleEvent("change",({payload:t,target:e})=>{this.el.id===t.id&&(e?this.pushEventTo(e,"live_select_change",t):this.pushEvent("live_select_change",t))}),this.handleEvent("select",({id:t,selection:e,mode:l,focus:i,input_event:n})=>{if(this.el.id===t){if(l==="single"){const s=e.length>0?e[0].label:null;this.setInputValue(s,{focus:i,blur})}else this.setInputValue(null,{focus:i,blur});n&&this.inputEvent(e,l)}}),this.attachDomEventHandlers()},updated(){this.maybeStyleClearButton(),this.attachDomEventHandlers()}}};