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

Add case for all non-empty values in toggle-attribute #79

Merged
merged 6 commits into from
Oct 11, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 29 additions & 1 deletion src/toggle-attribute/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,17 +64,44 @@ import '@travelopia/web-components/dist/toggle-attribute/style.css';
<option value="">Select</option>
<option value="First">First</option>
<option value="Second">Second</option>
<option value="Third">Third</option>
<option value="Fourth">Fourth</option>
<option value="Fifth">Fifth</option>
<option value="All">All</option>
</select>
</tp-toggle-attribute>

<div class="toggle-target-variable" data-toggle-value="First,All">
Toggled First
</div>
<div class="toggle-target-variable" data-toggle-value-non-empty>
junaidbhura marked this conversation as resolved.
Show resolved Hide resolved
<!-- add the data-toggle-value-non-empty attribute to toggle on with all non empty values. -->
This will be toggled for all non empty values.
</div>
<div class="toggle-target-variable" data-toggle-value="Second,All">
Toggled Second
</div>

<!-- Toggle for non empty values. -->
<p>Toggle with non empty values</p>

<!-- Select value -->
<tp-toggle-attribute target=".toggle-target-non-empty" value-non-empty>
abhishekxix marked this conversation as resolved.
Show resolved Hide resolved
<select>
<option value="">Select</option>
<option value="First">First</option>
<option value="Second">Second</option>
<option value="Third">Third</option>
<option value="Fourth">Fourth</option>
<option value="Fifth">Fifth</option>
<option value="All">All</option>
</select>
</tp-toggle-attribute>

<div class="toggle-target-non-empty">
Toggle for all non empty values.
</div>

<p>Button with click event</p>
<tp-toggle-attribute event="click" target=".button-target">
<button>Toggle using class</button>
Expand Down Expand Up @@ -104,9 +131,10 @@ import '@travelopia/web-components/dist/toggle-attribute/style.css';
| target | Yes | <selector or the target> | This is required if group is not mentioned |
| attribute | No | <attribute key> | The attribute to toggle. Default: `toggled` |
| attribute-value | No | <attribute value> | The attribute value when its. Default: `yes` |
| values | No | <comma separated values to match> | If this is specified, these comma separated values are matched with the value of the trigger. If they match, the target(s) is/are toggled. Same goes for having a `data-toggle-value` attribute on a target. |
| value | No | <comma separated values to match> | If this is specified, these comma separated values are matched with the value of the trigger. If they match, the target(s) is/are toggled. Same goes for having a `data-toggle-value` attribute on a target. |
| trigger | No | <selector of the trigger> | If this is not specified, the direct child is treated as the trigger. If it is mentioned, it looks for this selector within the context |
| closest-ancestor | No | <selector of the closest ancestor> | Default: `body`. If this is specified, the target is searched for within this selector, not on `body`. |
| value-non-empty | No | <none> | A boolean attribute that signifies whether or not the targets should be toggled for all non empty values on the trigger.

## Events

Expand Down
29 changes: 28 additions & 1 deletion src/toggle-attribute/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -66,17 +66,44 @@
<option value="">Select</option>
<option value="First">First</option>
<option value="Second">Second</option>
<option value="Third">Third</option>
<option value="Fourth">Fourth</option>
<option value="Fifth">Fifth</option>
<option value="All">All</option>
</select>
</tp-toggle-attribute>

<div class="toggle-target-variable" data-toggle-value="First,All">
Toggled First
</div>
<div class="toggle-target-variable" data-toggle-value-non-empty>
<!-- add the data-toggle-value-non-empty attribute to toggle on with all non empty values. -->
This will be toggled for all non empty values.
</div>
<div class="toggle-target-variable" data-toggle-value="Second,All">
Toggled Second
</div>

<!-- Toggle for non empty values. -->
<p>Toggle with non empty values</p>

<!-- Select value -->
<tp-toggle-attribute target=".toggle-target-non-empty" value-non-empty>
<select>
<option value="">Select</option>
<option value="First">First</option>
<option value="Second">Second</option>
<option value="Third">Third</option>
<option value="Fourth">Fourth</option>
<option value="Fifth">Fifth</option>
<option value="All">All</option>
</select>
</tp-toggle-attribute>

<div class="toggle-target-non-empty">
Toggle for all non empty values.
</div>

<p>Button with click event</p>
<tp-toggle-attribute event="click" target=".button-target">
<button>Toggle using class</button>
Expand All @@ -100,4 +127,4 @@
</main>
</body>

</html>
</html>
54 changes: 40 additions & 14 deletions src/toggle-attribute/tp-toggle-attribute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export class TPToggleAttributeElement extends HTMLElement {
update(): void {
// Get trigger elements.
const triggerSelector: string = this.getAttribute( 'trigger' ) ?? ':scope > *';
const triggers: NodeListOf<HTMLElement> | null = this.querySelectorAll( triggerSelector );
const triggers: NodeListOf<HTMLElement> = this.querySelectorAll( triggerSelector );

// Exit the function if no triggers are found.
if ( ! triggers ) {
Expand Down Expand Up @@ -61,7 +61,7 @@ export class TPToggleAttributeElement extends HTMLElement {
// Check if trigger has a value, example: form inputs.
if ( value || '' === value ) {
// Check if we have a value.
if ( this.hasAttribute( 'value' ) ) {
if ( this.hasAttribute( 'value' ) || this.hasAttribute( 'value-non-empty' ) ) {
this.toggleByValueAttribute( value );
} else {
this.toggleByTargetDataValue( value );
Expand All @@ -78,22 +78,38 @@ export class TPToggleAttributeElement extends HTMLElement {
* @param {string} value Trigger's value.
*/
toggleByValueAttribute( value: string = '' ): void {
// Get value to listen for.
const values: string[] = ( this.getAttribute( 'value' ) ?? '' ).split( ',' );

// Get the target elements.
const targetElements = this.getTargetElements();

// Check if we can continue
if ( ! ( values.length && targetElements ) ) {
if ( ! targetElements ) {
// We can't.
return;
}

// Initialize values.
let values: string[] = [];

// Get value to listen for.
const valuesAttribute = this.getAttribute( 'value' );
const nonEmptyValuesAttribute = this.hasAttribute( 'value-non-empty' );

// Can we proceed?
if ( ! valuesAttribute && ! nonEmptyValuesAttribute ) {
// Nope.
return;
}

// Do we have the values attribute?
if ( valuesAttribute ) {
// Yes, split it.
values = valuesAttribute.split( ',' );
}

// Toggle the target elements.
targetElements.forEach( ( target ) => {
// Toggle the target's attribute if the target and trigger have the same value.
if ( values.includes( value ) ) {
if ( ( values.length && values.includes( value ) ) || ( nonEmptyValuesAttribute && value ) ) {
this.toggleTargetAttribute( target, 'on' );
} else {
this.toggleTargetAttribute( target, 'off' );
Expand All @@ -118,17 +134,27 @@ export class TPToggleAttributeElement extends HTMLElement {

// Toggle the target elements.
targetElements.forEach( ( target: HTMLElement ): void => {
// Get values.
const values: string[] = ( target.getAttribute( 'data-toggle-value' ) ?? '' ).split( ',' );
// Get values and split them. Set an empty array otherwise.
const valuesAttribute = target.getAttribute( 'data-toggle-value' );
const nonEmptyValuesAttribute = target.hasAttribute( 'data-toggle-value-non-empty' );

// Check if we can continue
if ( ! values.length ) {
// We can't.
// Can we proceed?
if ( ! valuesAttribute && ! nonEmptyValuesAttribute ) {
// Nope, bail.
return;
}

// Toggle on element attribute if it matches value.
if ( values.includes( value ) ) {
// Initialize values.
let values: string[] = [];

// Null check.
if ( valuesAttribute ) {
// Assign the values.
values = valuesAttribute.split( ',' );
}

// Toggle on element attribute if it matches value or it does not have a data-toggle-value attribute in which case it will match with all non empty values.
if ( ( values.length && values.includes( value ) ) || ( nonEmptyValuesAttribute && value ) ) {
this.toggleTargetAttribute( target, 'on' );
} else {
this.toggleTargetAttribute( target, 'off' );
Expand Down
Loading