Skip to content

Commit 4539c78

Browse files
authored
Merge pull request #4455 from Laravel-Backpack/js-fields-subfields-parent
JS fields - Added `parent` on subfields instead of `subfieldHolder`
2 parents 473efe4 + c712c93 commit 4539c78

File tree

1 file changed

+16
-17
lines changed

1 file changed

+16
-17
lines changed

src/resources/views/crud/inc/form_fields_script.blade.php

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class CrudField {
1111
this.name = name;
1212
1313
// get the input/textarea/select that has that field name
14-
this.$input = $('input[name="'+this.name+'"], textarea[name="'+this.name+'"], select[name="'+this.name+'"], select[name="'+this.name+'[]"]').first();
14+
this.$input = $(`input[name="${this.name}"], textarea[name="${this.name}"], select[name="${this.name}"], select[name="${this.name}[]"]`).first();
1515
1616
// get the field wraper
1717
this.wrapper = this.inputWrapper;
@@ -47,7 +47,7 @@ class CrudField {
4747
// otherwise, try to find the input using other selectors
4848
if (this.$input.length === 0) {
4949
// try searching for the field with the corresponding bp-field-name
50-
input = this.wrapper.find('input[bp-field-name="'+this.name+'"], textarea[bp-field-name="'+this.name+'"], select[bp-field-name="'+this.name+'"], select[bp-field-name="'+this.name+'[]"]').first();
50+
input = this.wrapper.find(`input[bp-field-name="${this.name}"], textarea[bp-field-name="${this.name}"], select[bp-field-name="${this.name}"], select[bp-field-name="${this.name}[]"]`).first();
5151
5252
// if not input found yet, just get the first input in that wrapper
5353
if (input.length === 0) {
@@ -78,11 +78,8 @@ class CrudField {
7878
const fieldChanged = (event, values) => bindedClosure(this, event, values);
7979
8080
if(this.isSubfield) {
81-
window.crud.subfieldsCallbacks = window.crud.subfieldsCallbacks ?? [];
82-
window.crud.subfieldsCallbacks[this.subfieldHolder] = window.crud.subfieldsCallbacks[this.subfieldHolder] ?? [];
83-
if(!window.crud.subfieldsCallbacks[this.subfieldHolder].some( callbacks => callbacks['fieldName'] === this.name )) {
84-
window.crud.subfieldsCallbacks[this.subfieldHolder].push({closure: closure, field: this});
85-
}
81+
window.crud.subfieldsCallbacks[this.parent.name] ??= [];
82+
window.crud.subfieldsCallbacks[this.parent.name].push({ closure, field: this });
8683
return this;
8784
}
8885
@@ -96,15 +93,12 @@ class CrudField {
9693
9794
change() {
9895
if(this.isSubfield) {
99-
if(typeof window.crud.subfieldsCallbacks[this.subfieldHolder].length !== 'undefined') {
100-
window.crud.subfieldsCallbacks[this.subfieldHolder].forEach(item => {
101-
item.triggerChange = true;
102-
});
103-
}
104-
return this;
96+
window.crud.subfieldsCallbacks[this.parent.name]?.forEach(callback => callback.triggerChange = true);
97+
} else {
98+
this.$input.trigger(`change`);
10599
}
106100
107-
this.$input.trigger(`change`);
101+
return this;
108102
}
109103
110104
show(value = true) {
@@ -149,10 +143,12 @@ class CrudField {
149143
subfield(name, rowNumber = false) {
150144
let subfield = new CrudField(this.name);
151145
subfield.name = name;
146+
152147
if(!rowNumber) {
153148
subfield.isSubfield = true;
154-
subfield.subfieldHolder = this.name;
155-
}else{
149+
subfield.subfieldHolder = this.name; // deprecated
150+
subfield.parent = this;
151+
} else {
156152
subfield.rowNumber = rowNumber;
157153
subfield.wrapper = $(`[data-repeatable-identifier="${this.name}"][data-row-number="${rowNumber}"]`).find(`[bp-field-wrapper][bp-field-name$="${name}"]`);
158154
subfield.$input = subfield.wrapper.find(`[data-repeatable-input-name$="${name}"][bp-field-main-input]`);
@@ -174,10 +170,13 @@ class CrudField {
174170
window.crud = {
175171
...window.crud,
176172
173+
// Subfields callbacks holder
174+
subfieldsCallbacks: [],
175+
177176
// Create a field from a given name
178177
field: name => new CrudField(name),
179178
180179
// Create all fields from a given name list
181180
fields: names => names.map(window.crud.field),
182181
};
183-
</script>
182+
</script>

0 commit comments

Comments
 (0)