Changeset 5024 for sites/trunk/wordpress.org/public_html/wp-content/themes/pub/wporg-plugins/client/components/plugin-ratings/stars/index.jsx
- Timestamp:
- 03/01/2017 06:08:54 PM (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
sites/trunk/wordpress.org/public_html/wp-content/themes/pub/wporg-plugins/client/components/plugin-ratings/stars/index.jsx
r4223 r5024 1 import React from 'react'; 1 /** 2 * External dependencies. 3 */ 4 import React, { Component, PropTypes } from 'react'; 5 import { identity } from 'lodash'; 6 import { localize } from 'i18n-calypso'; 2 7 3 export default React.createClass( { 4 displayName: 'Stars', 8 export class Stars extends Component { 9 static propTypes = { 10 rating: PropTypes.number, 11 translate: PropTypes.func, 12 }; 5 13 6 fillStars( rating ) { 7 const template = '<span class="%1$s"></span>'; 14 static defaultProps = { 15 rating: 0, 16 translate: identity, 17 }; 18 19 /** 20 * Returns filled stars representative of rating. 21 * 22 * @param {Number} rating Plugin rating. 23 * @return {String} Rating stars. 24 */ 25 fillStars = ( rating ) => { 8 26 let counter = rating * 2, 9 27 output = '', … … 13 31 switch ( counter ) { 14 32 case 0: 15 output += template.replace( '%1$s', 'dashicons dashicons-star-empty' );33 output += '<span class="dashicons dashicons-star-empty"></span>'; 16 34 break; 17 35 18 36 case 1: 19 output += template.replace( '%1$s', 'dashicons dashicons-star-half' );37 output += '<span class="dashicons dashicons-star-half"></span>'; 20 38 counter--; 21 39 break; 22 40 23 41 default: 24 output += template.replace( '%1$s', 'dashicons dashicons-star-filled' );42 output += '<span class="dashicons dashicons-star-filled"></span>'; 25 43 counter -= 2; 26 44 } … … 28 46 29 47 return output; 30 } ,48 }; 31 49 32 50 render() { 33 const titleTemplate = '%s out of 5 stars',34 title = titleTemplate.replace( '%s', this.props.rating / 20 );51 const { rating, translate } = this.props; 52 const stars = Math.round( rating / 0.5 ) * 0.5; 35 53 36 54 return ( 37 55 <div 38 56 className="wporg-ratings" 39 aria-label={ title } 40 data-title-template={ titleTemplate } 41 data-rating={ this.props.rating / 20 } 42 dangerouslySetInnerHTML={ { __html: this.fillStars( Math.round( this.props.rating / 10 ) / 2 ) } } 43 ></div> 44 ) 57 aria-label={ translate( '%(stars)s out of 5 stars', { args: { stars } } ) } 58 dangerouslySetInnerHTML={ { __html: this.fillStars( stars ) } } 59 /> 60 ); 45 61 } 46 } ); 62 } 63 64 export default localize( Stars );
Note: See TracChangeset
for help on using the changeset viewer.