Skip to content

Commit

Permalink
[PBNTR-462] Adding default value prop to Rails Star Rating kit (#3629)
Browse files Browse the repository at this point in the history
**What does this PR do?**
Adding default value prop to Rails Star Rating kit


![image](https://github.com/user-attachments/assets/1d8699c8-6cfb-4b0e-ae31-e138bcc732ab)

**How to test?** Steps to confirm the desired behavior:
1. Go to the Rails Star Rating kit page.

#### 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
carloslimasd authored Sep 6, 2024
1 parent bbd1485 commit ff15779
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<%= pb_rails("star_rating", props: { default_value: "2", padding_bottom: "xs", variant: "interactive" }) %>
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ examples:
- star_rating_number_config: Number Config
- star_rating_size_options: Size Options
- star_rating_interactive: Interactive
- star_rating_default_value: Default Value

react:
- star_rating_default: Default
Expand Down
12 changes: 11 additions & 1 deletion playbook/app/pb_kits/playbook/pb_star_rating/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export default class PbStarRating extends PbEnhancedElement {
connect() {
this.addEventListeners()
this.handleFormReset()
this.setDefaultValue()
}

addEventListeners() {
Expand Down Expand Up @@ -128,7 +129,7 @@ export default class PbStarRating extends PbEnhancedElement {
const form = this.element.closest("form")
if (form) {
form.addEventListener("reset", () => {
this.updateHiddenInputValue("")
this.element.querySelector(STAR_RATING_INPUT_DATA_SELECTOR)?.setAttribute("value","")
this.resetStarRatingValues()
})
}
Expand All @@ -153,4 +154,13 @@ export default class PbStarRating extends PbEnhancedElement {
}
}
}

setDefaultValue() {
const hiddenInput = this.element.querySelector(STAR_RATING_INPUT_DATA_SELECTOR)
const defaultValue = hiddenInput.value

if (defaultValue) {
this.updateStarColors(defaultValue)
}
}
}
4 changes: 3 additions & 1 deletion playbook/app/pb_kits/playbook/pb_star_rating/star_rating.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class StarRating < Playbook::KitBase
default: false
prop :input_options, type: Playbook::Props::HashProp,
default: {}
prop :default_value

def one_decimal_rating
rating.to_f.round(1)
Expand Down Expand Up @@ -120,7 +121,8 @@ def all_input_options
data: { "pb-star-rating-input": true },
name: name,
required: required,
style: "display: none"
style: "display: none",
value: default_value || ""
)
end
end
Expand Down

0 comments on commit ff15779

Please sign in to comment.