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

[3.x] ShowWhen refactor #1152

Merged
merged 17 commits into from
Aug 9, 2024
Merged
56 changes: 30 additions & 26 deletions resources/js/Components/FormBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import request from '../Request/Core.js'
import {dispatchEvents as de} from '../Support/DispatchEvents.js'
import {
getInputs,
isValidateShow,
showWhenChange,
showWhenVisibilityChange,
} from '../Support/ShowWhen.js'
Expand Down Expand Up @@ -85,37 +84,44 @@ export default (name = '', initData = {}, reactive = {}) => ({
}
})

if (t.whenFields !== undefined) {
this.$nextTick(async function () {
let formId = t.$id('form')
if (formId === undefined) {
formId = t.$el.getAttribute('id')
}
this.whenFieldsInit()
},
whenFieldsInit(){
const t = this

await t.$nextTick()
if (! t.whenFields.length) {
return
}

const inputs = t.getInputs(formId)
this.$nextTick(async function () {
let formId = t.$id('form')
if (formId === undefined) {
formId = t.$el.getAttribute('id')
}

const showWhenConditions = {}
await t.$nextTick()

t.whenFields.forEach(field => {
if (
const inputs = t.getInputs(formId)

const showWhenFields = {}

t.whenFields.forEach(field => {
if (
inputs[field.changeField] === undefined ||
inputs[field.changeField].value === undefined
) {
return
}
if (showWhenConditions[field.showField] === undefined) {
showWhenConditions[field.showField] = []
}
showWhenConditions[field.showField].push(field)
})

for (let key in showWhenConditions) {
t.showWhenVisibilityChange(showWhenConditions[key], key, inputs, formId)
) {
return
}
if (showWhenFields[field.showField] === undefined) {
showWhenFields[field.showField] = []
}
showWhenFields[field.showField].push(field)
})
}

for (let key in showWhenFields) {
t.showWhenVisibilityChange(showWhenFields[key], key, inputs, formId)
}
})
},
precognition() {
const form = this.$el
Expand Down Expand Up @@ -276,8 +282,6 @@ export default (name = '', initData = {}, reactive = {}) => ({

showWhenVisibilityChange,

isValidateShow,

getInputs,
})

Expand Down
6 changes: 6 additions & 0 deletions resources/js/Components/TableBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@ export default (

this.table.querySelector('tbody').appendChild(this.lastRow.cloneNode(true))

const form = this.table.closest('from[data-component]')
if(form) {
const formName = form.getAttribute('data-component')
this.$dispatch('show_when_refresh:' + formName)
}

if (!force && this.reindex) {
this.resolveReindex()
}
Expand Down
4 changes: 2 additions & 2 deletions resources/js/Support/Forms.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {inputFieldName, inputGeValue} from './ShowWhen.js'
import {inputFieldName, inputGetValue} from './ShowWhen.js'

export function filterAttributeStartsWith(data, startsWith) {
const filtered = {}
Expand Down Expand Up @@ -97,7 +97,7 @@ export function crudFormQuery(formElements = null) {
!name.startsWith('_') &&
!name.startsWith('hidden_')
) {
values[inputFieldName(name)] = inputGeValue(element)
values[inputFieldName(name)] = inputGetValue(element)
}
})

Expand Down
Loading