PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 16.3-beta
Jetpack – WP Security, Backup, Speed, & Growth v16.3-beta
16.3-beta 16.3-a.5 16.3-a.7 16.3-a.3 16.3-a.1 16.2 16.2-beta 12.0.3 12.1.3 12.2.3 12.3.2 12.4.2 12.5.2 12.6.4 12.7.3 12.8.3 12.9.5 13.0.2 13.1.5 13.2.4 13.3.3 13.4.5 13.5.2 13.6.2 13.7.2 All 507 releases
jetpack / jetpack_vendor / automattic / jetpack-forms / src / modules / field-rating / view.js

view.js in Jetpack – WP Security, Backup, Speed, & Growth 16.3-beta, at jetpack_vendor/automattic/jetpack-forms/src/modules/field-rating/view.js

38 lines 1.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 import { store, getContext, getElement } from '@wordpress/interactivity';
2 import { renderRatingIconsHtml } from '../../blocks/field-rating/rating-icons.js';
3 // Re-export helpers for use by other modules.
4 export { getRating, isRatingValue, getRatingDisplayValue } from './helpers.js';
5
6 const NAMESPACE = 'jetpack/form';
7
8 store( NAMESPACE, {
9 callbacks: {
10 watchRatingIcons() {
11 const { ref } = getElement();
12 const context = getContext();
13
14 // Try to get rating data from context (AJAX submissions) or data attribute (server-rendered).
15 let rating = context.submission?.rating;
16
17 // For server-rendered content, read rating data from data attribute.
18 if ( ! rating && ref?.dataset?.rating ) {
19 try {
20 rating = JSON.parse( ref.dataset.rating );
21 } catch {
22 // Invalid JSON, ignore.
23 }
24 }
25
26 // If no rating data is available, don't render icons.
27 if ( ! rating ) {
28 return;
29 }
30
31 const { rating: ratingValue, maxRating, iconStyle, screenReaderText } = rating;
32
33 // Render icons using the shared function.
34 ref.innerHTML = renderRatingIconsHtml( ratingValue, maxRating, iconStyle, screenReaderText );
35 },
36 },
37 } );
38