-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[PBNTR-415] Adding Star Rating form controls (#3600)
**What does this PR do?** Adding Star Rating form controls **How to test?** Steps to confirm the desired behavior: 1. Go to the Form kit page 2. Scroll down to the last input (Star Rating kit) #### Checklist: - [x] **LABELS** Add a label: `enhancement`, `bug`, `improvement`, `new kit`, `deprecated`, or `breaking`. See [Changelog & Labels](https://github.com/powerhome/playbook/wiki/Changelog-&-Labels) for details. - [x] **DEPLOY** I have added the `milano` label to show I'm ready for a review. - [ ] **TESTS** I have added test coverage to my code.
- Loading branch information
1 parent
53b648c
commit 4b051ec
Showing
4 changed files
with
100 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,119 +1,156 @@ | ||
import PbEnhancedElement from "../pb_enhanced_element"; | ||
import PbEnhancedElement from "../pb_enhanced_element" | ||
|
||
const STAR_RATING_SELECTOR = "[data-pb-star-rating]"; | ||
const STAR_RATING_INPUT_ID = "star-rating-input"; | ||
const STAR_RATING_WRAPPER_SELECTOR = "[data-pb-star-rating-wrapper]" | ||
const STAR_RATING_SELECTOR = "[data-pb-star-rating]" | ||
const STAR_RATING_INPUT_DATA_SELECTOR = "[data-pb-star-rating-input]" | ||
|
||
export default class PbStarRating extends PbEnhancedElement { | ||
static get selector() { | ||
return STAR_RATING_SELECTOR; | ||
return STAR_RATING_WRAPPER_SELECTOR | ||
} | ||
|
||
connect() { | ||
this.element.addEventListener("click", (event) => { | ||
const clickedStarId = event.currentTarget.id; | ||
this.updateStarColors(clickedStarId); | ||
this.updateHiddenInputValue(clickedStarId); | ||
}); | ||
this.addEventListeners() | ||
this.handleFormReset() | ||
} | ||
|
||
addEventListeners() { | ||
this.element.querySelectorAll(STAR_RATING_SELECTOR).forEach(star => { | ||
star.addEventListener("click", (event) => { | ||
const clickedStarId = event.currentTarget.id | ||
this.updateStarColors(clickedStarId) | ||
this.updateHiddenInputValue(clickedStarId) | ||
this.clearFormValidation() | ||
}) | ||
|
||
document.querySelectorAll(STAR_RATING_SELECTOR).forEach(star => { | ||
star.addEventListener("mouseenter", (event) => { | ||
const hoveredStarId = event.currentTarget.id | ||
this.updateStarHoverColors(hoveredStarId); | ||
this.updateStarHoverColors(hoveredStarId) | ||
}) | ||
|
||
star.addEventListener("mouseleave", () => { | ||
this.removeStarHoverColors(); | ||
this.removeStarHoverColors() | ||
}) | ||
|
||
star.addEventListener("keydown", (event) => { | ||
if (event.key === 'Enter' || event.key === ' ') { | ||
event.preventDefault(); | ||
this.handleStarClick(star.id); | ||
event.preventDefault() | ||
this.handleStarClick(star.id) | ||
} | ||
}) | ||
}) | ||
} | ||
|
||
handleStarClick(starId) { | ||
this.updateStarColors(starId); | ||
this.updateHiddenInputValue(starId); | ||
this.updateStarColors(starId) | ||
this.updateHiddenInputValue(starId) | ||
} | ||
|
||
updateStarColors(clickedStarId) { | ||
const allStars = document.querySelectorAll(STAR_RATING_SELECTOR); | ||
const allStars = this.element.querySelectorAll(STAR_RATING_SELECTOR) | ||
|
||
allStars.forEach(star => { | ||
const starId = star.id; | ||
const icon = star.querySelector(".interactive-star-icon"); | ||
const starId = star.id | ||
const icon = star.querySelector(".interactive-star-icon") | ||
|
||
if (icon) { | ||
if (starId <= clickedStarId) { | ||
if (star.classList.contains("yellow_star")) { | ||
icon.classList.add("yellow-star-selected"); | ||
icon.classList.add("yellow-star-selected") | ||
} else if (star.classList.contains("primary_star_light")) { | ||
icon.classList.add("primary-star-selected"); | ||
icon.classList.add("primary-star-selected") | ||
} else if (star.classList.contains("primary_star_dark")) { | ||
icon.classList.add("primary-star-selected"); | ||
icon.classList.add("primary-star-selected") | ||
} else if (star.classList.contains("subtle_star_light")) { | ||
icon.classList.add("subtle-star-selected"); | ||
icon.classList.add("subtle-star-selected") | ||
} else if (star.classList.contains("subtle_star_dark")) { | ||
icon.classList.add("subtle-star-selected"); | ||
icon.classList.add("subtle-star-selected") | ||
} else { | ||
icon.classList.add("yellow-star-selected"); | ||
icon.classList.add("yellow-star-selected") | ||
} | ||
} else { | ||
icon.classList.remove("yellow-star-selected", "primary-star-selected", "subtle-star-selected"); | ||
icon.classList.remove("yellow-star-selected", "primary-star-selected", "subtle-star-selected") | ||
} | ||
icon.classList.remove("star-hovered"); | ||
icon.classList.remove("star-hovered") | ||
} | ||
}); | ||
}) | ||
} | ||
|
||
updateHiddenInputValue(value) { | ||
const hiddenInput = document.getElementById(STAR_RATING_INPUT_ID); | ||
const hiddenInput = this.element.querySelector(STAR_RATING_INPUT_DATA_SELECTOR) | ||
if (hiddenInput) { | ||
hiddenInput.value = value; | ||
hiddenInput.value = value | ||
} | ||
} | ||
|
||
updateStarHoverColors(hoveredStarId) { | ||
const allStars = document.querySelectorAll(STAR_RATING_SELECTOR); | ||
const allStars = this.element.querySelectorAll(STAR_RATING_SELECTOR) | ||
|
||
allStars.forEach(star => { | ||
const starId = star.id; | ||
const icon = star.querySelector(".interactive-star-icon"); | ||
const starId = star.id | ||
const icon = star.querySelector(".interactive-star-icon") | ||
|
||
if (icon) { | ||
if (starId <= hoveredStarId) { | ||
if (!icon.classList.contains("yellow-star-selected") && | ||
!icon.classList.contains("primary-star-selected") && | ||
!icon.classList.contains("subtle-star-selected")) { | ||
icon.classList.add("star-hovered"); | ||
icon.classList.add("star-hovered") | ||
} | ||
} else { | ||
icon.classList.remove("star-hovered"); | ||
icon.classList.remove("star-hovered") | ||
} | ||
} | ||
}); | ||
}) | ||
} | ||
|
||
|
||
removeStarHoverColors() { | ||
const allStars = document.querySelectorAll(STAR_RATING_SELECTOR); | ||
const allStars = this.element.querySelectorAll(STAR_RATING_SELECTOR) | ||
|
||
allStars.forEach(star => { | ||
const icon = star.querySelector(".interactive-star-icon"); | ||
const icon = star.querySelector(".interactive-star-icon") | ||
if (icon) { | ||
if (!icon.classList.contains("yellow-star-selected") && | ||
!icon.classList.contains("primary-star-selected") && | ||
!icon.classList.contains("subtle-star-selected")) { | ||
icon.classList.remove("star-hovered"); | ||
icon.classList.remove("star-hovered") | ||
} | ||
} | ||
}); | ||
}) | ||
} | ||
|
||
isStarSelected() { | ||
return document.querySelectorAll(".yellow-star-selected, .primary-star-selected, .subtle-star-selected").length > 0; | ||
return this.element.querySelectorAll(".yellow-star-selected, .primary-star-selected, .subtle-star-selected").length > 0 | ||
} | ||
|
||
handleFormReset() { | ||
const form = this.element.closest("form") | ||
if (form) { | ||
form.addEventListener("reset", () => { | ||
this.updateHiddenInputValue("") | ||
this.resetStarRatingValues() | ||
}) | ||
} | ||
} | ||
|
||
resetStarRatingValues() { | ||
const allStars = this.element.querySelectorAll(STAR_RATING_SELECTOR) | ||
allStars.forEach(star => { | ||
const icon = star.querySelector(".interactive-star-icon") | ||
if (icon) { | ||
icon.classList.remove("yellow-star-selected", "primary-star-selected", "subtle-star-selected") | ||
} | ||
}) | ||
} | ||
|
||
clearFormValidation() { | ||
const hiddenInput = this.element.querySelector(STAR_RATING_INPUT_DATA_SELECTOR) | ||
if (hiddenInput.checkValidity()) { | ||
const errorLabelElement = this.element.querySelector(".pb_body_kit_negative") | ||
if (errorLabelElement) { | ||
errorLabelElement.remove() | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters