Skip to content

Commit 60575b3

Browse files
committed
fix df-require/disable/show/hide is target is UploadField
1 parent 2de8f1c commit 60575b3

File tree

1 file changed

+12
-9
lines changed

1 file changed

+12
-9
lines changed

client/django-formset/DjangoFormset.ts

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,16 @@ if (style.sheet instanceof CSSStyleSheet) {
3535
}, document.body]]);
3636
}
3737

38+
function isTruthy(expression: any) : boolean {
39+
// since empty arrays evaluate to true, implement separately to examine for existance
40+
// when using the file upload widget. Check https://262.ecma-international.org/5.1/#sec-11.9.3
41+
// for details.
42+
if (Array.isArray(expression))
43+
return expression.length !== 0 && expression.every(v => isTruthy(v));
44+
if (isPlainObject(expression))
45+
return !isEmpty(expression);
46+
return Boolean(expression);
47+
}
3848

3949
class BoundValue {
4050
public readonly value: FieldValue;
@@ -265,13 +275,6 @@ class FieldGroup {
265275
}
266276

267277
private evalVisibility(attribute: string, visible: boolean): Function|null {
268-
function isTruthy(expression: any) : boolean {
269-
// since empty arrays evaluate to true, implement separately to examine for existance
270-
// when using the file upload widget. Check https://262.ecma-international.org/5.1/#sec-11.9.3
271-
// for details.
272-
return Array.isArray(expression) ? expression.length !== 0 : Boolean(expression);
273-
}
274-
275278
const attrValue = this.fieldElements[0]?.getAttribute(attribute);
276279
if (!isString(attrValue))
277280
return null;
@@ -296,7 +299,7 @@ class FieldGroup {
296299
try {
297300
const evalExpression = new Function('return ' + parse(attrValue, {startRule: 'OperabilityExpression'}));
298301
return () => {
299-
const disable = evalExpression.call(this);
302+
const disable = isTruthy(evalExpression.call(this));
300303
this.fieldElements.forEach((elem, index) => elem.disabled = disable || this.initialDisabled[index]);
301304
}
302305
} catch (error) {
@@ -311,7 +314,7 @@ class FieldGroup {
311314
try {
312315
const evalExpression = new Function('return ' + parse(attrValue, {startRule: 'OperabilityExpression'}));
313316
return () => {
314-
const require = evalExpression.call(this);
317+
const require = isTruthy(evalExpression.call(this));
315318
this.fieldElements.forEach((elem, index) => elem.required = require && this.initialRequired[index]);
316319
this.form.checkValidity();
317320
}

0 commit comments

Comments
 (0)