gov-gui
Version:
Gov UI Component Library Typscript Build
184 lines (183 loc) • 6.64 kB
JavaScript
import { h } from "@stencil/core";
import { getGlobalPropsClasses } from "../../global/global-styles-helper";
import { getAnimationClasses } from "../../global/animation-helpers";
export class GovChoiceChips {
constructor() {
this.animationDelay = '2s';
this.allClasses = '';
// Handle chip click and update the selectedValue
this.handleChipClick = (selectedName, assignedElements) => {
// Update the selected value based on the clicked chip's name
this.selectedValue = selectedName;
// Unselect all other chips that do not match the selected chip
assignedElements.forEach((el) => {
const chip = el;
const chipName = chip.getAttribute('name');
// Update the selected attribute based on the selectedValue
chip.setAttribute('selected', chipName === this.selectedValue ? 'true' : 'false');
});
};
}
//watching for any change in animations to trigger them
watchAnimations() {
this.provideClass();
}
watchAnimationsDelay() {
this.provideClass();
}
watchAnimationsSpeed() {
this.provideClass();
}
// inject the animations and styles on component load
componentWillLoad() {
const animationClasses = getAnimationClasses({
animation: this.animation,
animationDelay: this.animationDelay,
animationSpeed: this.animationSpeed
});
this.allClasses = getGlobalPropsClasses({
classes: ' ' + animationClasses,
});
}
componentDidLoad() {
setTimeout(() => {
this.provideClass();
}, 100);
}
//Called on change of any animation related property to trigger change
provideClass() {
const animationClasses = getAnimationClasses({
animation: this.animation,
animationDelay: this.animationDelay,
animationSpeed: this.animationSpeed
});
this.allClasses = getGlobalPropsClasses({
classes: ' ' + animationClasses,
});
}
render() {
return (h("div", { key: '1415369cb3244344eec142b8cb7759653d4f902e', class: this.allClasses }, h("div", { key: '2d7c8b6c6d8af2a62d14d4428dab18a8c27530df', class: `chips-container`, role: "radiogroup" }, h("slot", { key: 'aa3dad2013f6add8836dca17e356f46761903309', onSlotchange: (event) => {
const assignedElements = event.target.assignedElements();
// Add click event listeners to each chip
assignedElements.forEach((el) => {
const chip = el;
const chipName = chip.getAttribute('name');
// Set selected state based on parent's selectedValue
chip.setAttribute('selected', chipName === this.selectedValue ? 'true' : 'false');
// Add click event listener to update selected value when chip is clicked
chip.addEventListener('click', () => this.handleChipClick(chipName, assignedElements));
});
} }))));
}
static get is() { return "gov-choice-chips"; }
static get encapsulation() { return "shadow"; }
static get originalStyleUrls() {
return {
"$": ["gov-choice-chips.css"]
};
}
static get styleUrls() {
return {
"$": ["gov-choice-chips.css"]
};
}
static get properties() {
return {
"name": {
"type": "string",
"mutable": false,
"complexType": {
"original": "string",
"resolved": "string",
"references": {}
},
"required": false,
"optional": false,
"docs": {
"tags": [],
"text": ""
},
"getter": false,
"setter": false,
"attribute": "name",
"reflect": false
},
"animation": {
"type": "string",
"mutable": false,
"complexType": {
"original": "string",
"resolved": "string",
"references": {}
},
"required": false,
"optional": true,
"docs": {
"tags": [],
"text": ""
},
"getter": false,
"setter": false,
"attribute": "animation",
"reflect": false
},
"animationDelay": {
"type": "string",
"mutable": false,
"complexType": {
"original": "'2s' | '3s' | '4s' | '5s'",
"resolved": "\"2s\" | \"3s\" | \"4s\" | \"5s\"",
"references": {}
},
"required": false,
"optional": true,
"docs": {
"tags": [],
"text": ""
},
"getter": false,
"setter": false,
"attribute": "animation-delay",
"reflect": false,
"defaultValue": "'2s'"
},
"animationSpeed": {
"type": "string",
"mutable": false,
"complexType": {
"original": "'slow' | 'slower' | 'fast' | 'faster'",
"resolved": "\"fast\" | \"faster\" | \"slow\" | \"slower\"",
"references": {}
},
"required": false,
"optional": true,
"docs": {
"tags": [],
"text": ""
},
"getter": false,
"setter": false,
"attribute": "animation-speed",
"reflect": false
}
};
}
static get states() {
return {
"selectedValue": {}
};
}
static get watchers() {
return [{
"propName": "animation",
"methodName": "watchAnimations"
}, {
"propName": "animationDelay",
"methodName": "watchAnimationsDelay"
}, {
"propName": "animationSpeed",
"methodName": "watchAnimationsSpeed"
}];
}
}
//# sourceMappingURL=gov-choice-chips.js.map