Skip to content

Commit e8687e1

Browse files
committed
add extra visibility conditions logic. Allow value * which accepts all non empty values. Add option for unequals check.
1 parent fad9bda commit e8687e1

File tree

3 files changed

+44
-5
lines changed

3 files changed

+44
-5
lines changed

layouts/shortcodes/dynamic_form_engine.html

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,12 @@ <h3>{{.title}}</h3>
2828
{{ $visibilityCondition := "" }}
2929
{{ range .visible_when_or }}
3030
{{ range .visible_when_and }}
31+
{{ $compareSign := "==" }}
32+
{{ if .compareSign }}
33+
{{ $compareSign = .compareSign }}
34+
{{ end }}
3135
{{ $visibilityCondition = printf "%s" $visibilityCondition | printf "%s%s" .value | printf "%s" }}
32-
{{ $visibilityCondition = printf "%s" $visibilityCondition | printf "%s%s" "=" | printf "%s" }}
36+
{{ $visibilityCondition = printf "%s" $visibilityCondition | printf "%s%s" $compareSign | printf "%s" }}
3337
{{ $visibilityCondition = printf "%s" $visibilityCondition | printf "%s%s" .identifier | printf "%s" }}
3438
{{ $visibilityCondition = printf "%s" $visibilityCondition | printf "%s%s" "&&" | printf "%s" }}
3539
{{ end }}
@@ -166,8 +170,12 @@ <h6 class="d-inline ml-1" >{{.title}}</h6>
166170
<script>
167171
document.addEventListener("DOMContentLoaded", function (event) {
168172

173+
var valuesAreEqual = function (inputValue, value) {
174+
return inputValue === value || (value === '*' && inputValue);
175+
}
176+
169177
var calculateVisibleFields = function () {
170-
// this is what the data attribute looks like: data-show-when="||&&data1=medium&&data2=medium||&&data1=low"
178+
// this is what the data attribute looks like: data-show-when=="||&&data1==medium&&data2=medium||&&data1==low"
171179
// This field should be hidden when no input is selected, and shown if either data 1 and data 2 are medium or data 1 is low.
172180
$('.score-card__question').each(function () {
173181
const showWhen = $(this).attr('data-show-when');
@@ -183,14 +191,26 @@ <h6 class="d-inline ml-1" >{{.title}}</h6>
183191
if (andCondition.indexOf('=') === -1) {
184192
return;
185193
}
186-
const [name, value] = andCondition.split('=');
194+
// And condition can be either a==b or a!=b
195+
const andCondSplitted = andCondition.split('=');
196+
let name = andCondSplitted[0];
197+
let value = andCondSplitted[andCondSplitted.length - 1];
198+
const checkForEquals = andCondSplitted.length > 2;
199+
const checkForUnequal = !checkForEquals;
200+
if (!checkForEquals) {
201+
// clean up name when name is a!
202+
name = name.substring(0, name.length - 1);
203+
}
204+
187205
// Check for radio button
188206
let input = document.querySelector(`.score-card__question:not(.d-none) input[name=${name}]:checked`);
189207
// Check for checkbox
190208
if (!input) {
191209
input = document.querySelector(`.score-card__question:not(.d-none) input[id=${name}]:checked`);
192210
}
193-
if (!input || input.value !== value) {
211+
if (!input
212+
|| (checkForEquals && !valuesAreEqual(input.value, value))
213+
|| (checkForUnequal && valuesAreEqual(input.value, value))) {
194214
andShow = false;
195215
}
196216
});

tina/collections/shared/page/building_blocks.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,25 @@ const building_blocks: TinaField[] = [
355355
name: "value",
356356
label: "Value",
357357
required: true,
358+
description:
359+
"The value of the option, or * when all values are allowed",
360+
},
361+
{
362+
type: "string",
363+
name: "compareSign",
364+
label: "Should be",
365+
options: [
366+
{
367+
value: "==",
368+
label: "Equal to",
369+
},
370+
{
371+
value: "!=",
372+
label: "Unequal to",
373+
},
374+
],
375+
description:
376+
"Allows equal or unequal comparison, empty or default is equal",
358377
},
359378
],
360379
},

tina/tina-lock.json

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)