Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Super-select: update on dependent field #45

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions app/javascript/controllers/dependable_controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { Controller } from "@hotwired/stimulus"

export default class extends Controller {
static values = {
dependentsSelector: String
}

updateDependents(event) {
if (!this.hasDependents) { return false }

this.dependents.forEach((dependent) => {
dependent.dispatchEvent(new CustomEvent(`${this.identifier}:updated`, { detail: { event: event }, bubbles: true, cancelable: false }))
})
}

get hasDependents() {
return (this.dependents.length > 0)
}

get dependents() {
if (!this.dependentsSelectorValue) { return [] }
return document.querySelectorAll(this.dependentsSelectorValue)
}
}
15 changes: 14 additions & 1 deletion app/javascript/controllers/fields/super_select_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ export default class extends Controller {
acceptsNew: Boolean,
enableSearch: Boolean,
searchUrl: String,
// searchUrlPattern: String,
// queryStringMappings: Object,
// searchUrlParams: Object
}

// will be reissued as native dom events name prepended with '$' e.g. '$change', '$select2:closing', etc
Expand Down Expand Up @@ -84,6 +87,16 @@ export default class extends Controller {
// revert to original markup, remove any event listeners
$(this.pluginMainEl).select2('destroy');
}

updateSearchUrlValue(event) {
const dependentOnField = event?.detail?.event?.target
if (!dependentOnField) { return }

const fieldName = dependentOnField.name
const fieldValue = dependentOnField.value

// this.updateUrlParamsFrom(fieldName, fieldValue)
}

initReissuePluginEventsAsNativeEvents() {
this.constructor.jQueryEventsToReissue.forEach((eventName) => {
Expand All @@ -99,7 +112,7 @@ export default class extends Controller {

dispatchNativeEvent(event) {
const nativeEventName = '$' + event.type // e.g. '$change.select2'
this.element.dispatchEvent(new CustomEvent(nativeEventName, { detail: { event: event }, bubbles: true, cancelable: false }))
this.pluginMainEl.dispatchEvent(new CustomEvent(nativeEventName, { detail: { event: event }, bubbles: true, cancelable: false }))
}

// https://stackoverflow.com/questions/29290389/select2-add-image-icon-to-option-dynamically
Expand Down
3 changes: 3 additions & 0 deletions app/javascript/controllers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import FileFieldController from './fields/file_field_controller'
import PasswordController from './fields/password_controller'
import PhoneController from './fields/phone_controller'
import SuperSelectController from './fields/super_select_controller'
import DependableController from './dependable_controller'

export const controllerDefinitions = [
[ButtonToggleController, 'fields/button_toggle_controller.js'],
Expand All @@ -20,6 +21,7 @@ export const controllerDefinitions = [
[PasswordController, 'fields/password_controller.js'],
[PhoneController, 'fields/phone_controller.js'],
[SuperSelectController, 'fields/super_select_controller.js'],
[DependableController, 'dependable_controller.js'],
].map(function(d) {
const key = d[1]
const controller = d[0]
Expand All @@ -39,4 +41,5 @@ export {
PasswordController,
PhoneController,
SuperSelectController,
DependableController,
}
6 changes: 3 additions & 3 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1012,9 +1012,9 @@
integrity sha512-wa/zupVG0eWxRYJjC1IiPBdt3Lruv0RqGN+/DTMmUWUyMAEB27KXmVY6a8YpUVTM7QwVuaLNGW4EqDgrS2upXQ==

"@hotwired/stimulus@^3.0.1":
version "3.0.1"
resolved "https://registry.yarnpkg.com/@hotwired/stimulus/-/stimulus-3.0.1.tgz#141f15645acaa3b133b7c247cad58ae252ffae85"
integrity sha512-oHsJhgY2cip+K2ED7vKUNd2P+BEswVhrCYcJ802DSsblJFv7mPFVk3cQKvm2vHgHeDVdnj7oOKrBbzp1u8D+KA==
version "3.2.1"
resolved "https://registry.yarnpkg.com/@hotwired/stimulus/-/stimulus-3.2.1.tgz#e3de23623b0c52c247aba4cd5d530d257008676b"
integrity sha512-HGlzDcf9vv/EQrMJ5ZG6VWNs8Z/xMN+1o2OhV1gKiSG6CqZt5MCBB1gRg5ILiN3U0jEAxuDTNPRfBcnZBDmupQ==

"@nodelib/fs.scandir@2.1.5":
version "2.1.5"
Expand Down