jetpack
/
jetpack_vendor
/
automattic
/
jetpack-forms
/
src
/
modules
/
field-rating
Last commit date
helpers.js
6 months ago
view.js
6 months ago
helpers.js
46 lines
| 1 | /** |
| 2 | * Pure helper functions for rating field. |
| 3 | * Extracted to allow testing without Interactivity API dependencies. |
| 4 | */ |
| 5 | |
| 6 | /** |
| 7 | * Extract rating data from a field value. |
| 8 | * |
| 9 | * @param {object} value - The field value. |
| 10 | * @return {Object|null} Rating data object or null if not a rating field. |
| 11 | */ |
| 12 | export function getRating( value ) { |
| 13 | if ( value?.type === 'rating' ) { |
| 14 | return { |
| 15 | rating: value.rating ?? 0, |
| 16 | maxRating: value.maxRating ?? 5, |
| 17 | iconStyle: value.iconStyle ?? 'stars', |
| 18 | }; |
| 19 | } |
| 20 | |
| 21 | return null; |
| 22 | } |
| 23 | |
| 24 | /** |
| 25 | * Check if a value is a rating field value. |
| 26 | * |
| 27 | * @param {object} value - The field value. |
| 28 | * @return {boolean} True if this is a rating field value. |
| 29 | */ |
| 30 | export function isRatingValue( value ) { |
| 31 | return value?.type === 'rating'; |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * Get the display value for a rating field. |
| 36 | * |
| 37 | * @param {object} value - The field value. |
| 38 | * @return {string|null} The display value (e.g., "3/5") or null if not a rating. |
| 39 | */ |
| 40 | export function getRatingDisplayValue( value ) { |
| 41 | if ( isRatingValue( value ) && value?.displayValue ) { |
| 42 | return value.displayValue; |
| 43 | } |
| 44 | return null; |
| 45 | } |
| 46 |