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

Rewrite and expand input trigger functionality #1163

Draft
wants to merge 29 commits into
base: develop
Choose a base branch
from

Conversation

bennothommo
Copy link
Member

@bennothommo bennothommo commented Jul 15, 2024

Workshop theme PR: wintercms/wn-workshop-theme#3
Original docs: https://wintercms.com/docs/v1.2/ui/script/input-trigger

This PR rewrites and expands upon the Input trigger functionality present in Winter CMS, and makes it available as an optional extra to themes in the front-end.

Whilst still remaining fully backwards-compatible with the current Trigger functionality included in the Storm UI library, this expands on the trigger functionality significantly.

Multiple triggers supported

An element may now have multiple triggers, by adding a specific name in the middle of the data-trigger attributes. For example, a button could have an enable or disable trigger depending on certain exclusive conditions:

<button
    id="save"
    data-trigger-enable=".terms-and-conditions"
    data-trigger-enable-condition="checked"
    data-trigger-enable-action="enable"
    data-trigger-disable=".privacy-policy"
    data-trigger-disable-condition="unchecked"
    data-trigger-disable-action="disable"
>Save</button>

In the example above, the name enable and disable have been used to differentiate the triggers, but you may use any name you wish, as long as it has a trigger element, a condition and a target.

Naming changes and new trigger parameters

A new trigger parameter - trigger-priority - is available to order triggers if you use more than one trigger on the same element. Priorities are ordered ascendingly, with higher priority triggers being fired after lower priority triggers.

The following trigger parameters have been renamed to be shorter, but the original names will still be compatible.

  • The trigger-closest-parent parameter has been renamed to trigger-parent
  • The trigger-condition parameter has been renamed to trigger-when
  • The trigger-action parameter has been renamed to trigger-do

New conditions and parameters

In addition to the checked, unchecked and value conditions, additional conditions have been made available. Conditions also take into account multiple inputs.

  • empty - triggered when the selected input(s) are empty/unchecked.
  • oneOf:value, value... - triggered when the selected input(s) values match one of the given values.
  • allOf:value, value... - triggered when the selected input(s) values match all of the given values.
  • focus - triggered when the selected input(s) are focused on.
  • blur - triggered when the selected input(s) lose focus.
  • attr:attributeName, attributeValue - triggered when the selected input(s) have a specific attribue name and value.
  • class:className - triggered when the selected input(s) have a specific class name.

Parameters can be provided in the format condition:param1, param2 similar to Laravel's validation rules. If a comma needs to be used within a parameter, the parameter can be quoted to prevent that comma from being used to split the parameters.

The checked condition has an optional parameter which can be a number, indicating that at least that number of checkboxes must be checked.

New actions

In addition to the show, hide, enable, disable and empty actions, additional actions have been made available, similar in format to the conditions, to run on the trigger element.

  • value:newValue - sets the value of the trigger element to the new value.
  • valueOf:selector, newValue - sets the value of a child element(s) of a trigger element that matches the selector.
  • check - checks a checkbox/radio trigger element.
  • unchceck - unchecks a checkbox/radio trigger element.
  • class:className, unmetClassName - defines a class to add to the trigger element when the condition is met. Optionally, can also provide a class when the condition is unmet.
  • classOf:selector, className, unmetClassName - defines a class to add to a child element of the trigger element when the condition is met. Optionally, can also provide a class when the condition is unmet.
  • attr:attributeName, attributeValue, unmetAttributeValue - sets the given attribute name with the given value on the trigger element when the condition is met. Optionally, can also provide an unmet value.
  • attrOf:selector, attributeName, attributeValue, unmetAttributeValue - sets the given attribute name with the given value on a child element of the trigger element when the condition is met. Optionally, can also provide an unmet value.
  • style:styleName, styleValue, unmetStyleValue - sets the given styling attribute with the given value on the trigger element when the condition is met. Optionally, can also provide an unmet value.
  • styleOf:selector, styleName, styleValue, unmetStyleValue - sets the given styling attribute with the given value on a child element of the trigger element when the condition is met. Optionally, can also provide an unmet value.

For the value, class, attr and style actions, the value of the target element can be inserted into the parameters. In order to prevent this from being abused, the value will be HTML encoded.

For the value action, if this is targeted on a non-input type element, it will change the inner text of that element.

Multiple conditions and actions

Both conditions and actions support multiple commands, by using a pipe (|) symbol between commands, again similar to Laravel. Conditions will always been treated as AND-type conditions - all conditions need to pass for the trigger to occur.

<input type="checkbox" value="red">
<input type="checkbox" value="orange">
<input type="checkbox" value="yellow">
<input type="checkbox" value="green">
<input type="checkbox" value="blue">
<input type="checkbox" value="indigo">
<input type="checkbox" value="violet">

<div
    class="message"
    data-trigger="input[type=checkbox]"
    data-trigger-when="checked:3|value:green"
    data-trigger-do="hide"
>
    You must select 3 colours, and one of them must be green.
</div>

@bennothommo bennothommo added enhancement PRs that implement a new feature or substantial change Status: In Progress labels Jul 15, 2024
@bennothommo bennothommo added this to the 1.2.7 milestone Jul 15, 2024
@mjauvin
Copy link
Member

mjauvin commented Jul 15, 2024

Niiice!

@mjauvin

This comment has been minimized.

@LukeTowers LukeTowers modified the milestones: 1.2.7, 1.3.0 Jul 16, 2024
@bennothommo bennothommo marked this pull request as draft October 11, 2024 00:25
Since multiple conditions are considered "AND" conditions, we will chain the matched elements from the previous rule and only consider these in the following rule. This narrows the scope of successful rulesets and allows rules to work more congruently together.

For example, previously, if you used a "checked" condition with a "within" condition, if the "checked" condition passed, the "within" condition would pass if any target element (even those not checked) successfully passed the "within" condition. If you wanted the "within" condition to only consider the elements that were checked, it couldn't do so. Now that the chained elements are passed through, this will work.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement PRs that implement a new feature or substantial change
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants