edit.js
127 lines
| 1 | import { __ } from '@wordpress/i18n'; |
| 2 | import { useBlockProps, InspectorControls } from '@wordpress/block-editor'; |
| 3 | import { PanelBody, SelectControl } from '@wordpress/components'; |
| 4 | import Styler from '../../styler'; |
| 5 | import Wrapper from '../../wrapper'; |
| 6 | |
| 7 | const Inspector = ( { attributes, setAttributes } ) => { |
| 8 | const { rating_type } = attributes; |
| 9 | |
| 10 | return ( |
| 11 | <InspectorControls> |
| 12 | <PanelBody |
| 13 | title={ __( 'General', 'shop-press' ) } |
| 14 | initialOpen={ true } |
| 15 | > |
| 16 | <SelectControl |
| 17 | label={ __( 'Type', 'shop-press' ) } |
| 18 | value={ rating_type } |
| 19 | options={ [ |
| 20 | { label: 'Classic', value: 'classic' }, |
| 21 | { label: 'Modern', value: 'modern' }, |
| 22 | ] } |
| 23 | onChange={ ( value ) => |
| 24 | setAttributes( { rating_type: value } ) |
| 25 | } |
| 26 | __nextHasNoMarginBottom |
| 27 | /> |
| 28 | </PanelBody> |
| 29 | <PanelBody title={ __( 'Styles', 'shop-press' ) }> |
| 30 | <Styler |
| 31 | clientId={ clientId } |
| 32 | label={ __( 'Wrapper', 'shop-press' ) } |
| 33 | selector=" .sp-loop-rating" |
| 34 | wrapper={ `.${ attributes[ 'wrapperID' ] }` } |
| 35 | attributes={ attributes } |
| 36 | setAttributes={ setAttributes } |
| 37 | /> |
| 38 | |
| 39 | <Styler |
| 40 | clientId={ clientId } |
| 41 | label={ __( 'Modern Container', 'shop-press' ) } |
| 42 | selector=".sp-loop-rating.sp-modern-rating .sp-rating" |
| 43 | wrapper={ `.${ attributes[ 'wrapperID' ] }` } |
| 44 | attributes={ attributes } |
| 45 | setAttributes={ setAttributes } |
| 46 | /> |
| 47 | |
| 48 | <Styler |
| 49 | clientId={ clientId } |
| 50 | label={ __( 'Modern Star', 'shop-press' ) } |
| 51 | selector=".sp-loop-rating.sp-modern-rating .sp-rating .sp-rating-star" |
| 52 | wrapper={ `.${ attributes[ 'wrapperID' ] }` } |
| 53 | attributes={ attributes } |
| 54 | setAttributes={ setAttributes } |
| 55 | /> |
| 56 | |
| 57 | <Styler |
| 58 | clientId={ clientId } |
| 59 | label={ __( 'Classic Container', 'shop-press' ) } |
| 60 | selector=".sp-loop-rating.sp-classic-rating .star-rating" |
| 61 | wrapper={ `.${ attributes[ 'wrapperID' ] }` } |
| 62 | attributes={ attributes } |
| 63 | setAttributes={ setAttributes } |
| 64 | /> |
| 65 | |
| 66 | <Styler |
| 67 | clientId={ clientId } |
| 68 | label={ __( 'Classic Empty Stars', 'shop-press' ) } |
| 69 | selector=".sp-loop-rating.sp-classic-rating .star-rating::before" |
| 70 | wrapper={ `.${ attributes[ 'wrapperID' ] }` } |
| 71 | attributes={ attributes } |
| 72 | setAttributes={ setAttributes } |
| 73 | /> |
| 74 | |
| 75 | <Styler |
| 76 | clientId={ clientId } |
| 77 | label={ __( 'Classic Stars', 'shop-press' ) } |
| 78 | selector=".sp-loop-rating.sp-classic-rating .star-rating span" |
| 79 | wrapper={ `.${ attributes[ 'wrapperID' ] }` } |
| 80 | attributes={ attributes } |
| 81 | setAttributes={ setAttributes } |
| 82 | /> |
| 83 | </PanelBody> |
| 84 | </InspectorControls> |
| 85 | ); |
| 86 | }; |
| 87 | |
| 88 | const Edit = ( props ) => { |
| 89 | const { attributes, setAttributes, clientId } = props; |
| 90 | const { rating_type } = attributes; |
| 91 | const blockProps = useBlockProps(); |
| 92 | |
| 93 | return ( |
| 94 | <div { ...blockProps }> |
| 95 | <Inspector |
| 96 | attributes={ attributes } |
| 97 | setAttributes={ setAttributes } |
| 98 | clientId={ clientId } |
| 99 | /> |
| 100 | |
| 101 | <Wrapper { ...props }> |
| 102 | { rating_type === 'modern' && ( |
| 103 | <div className="woocommerce-product-rating sp-loop-rating sp-modern-rating"> |
| 104 | <div className="sp-rating"> |
| 105 | <span className="sp-rating-star">S</span>4.5 |
| 106 | </div> |
| 107 | </div> |
| 108 | ) } |
| 109 | |
| 110 | { rating_type === 'classic' && ( |
| 111 | <div className="woocommerce-product-rating sp-loop-rating sp-classic-rating"> |
| 112 | <div |
| 113 | className="star-rating" |
| 114 | role="img" |
| 115 | aria-label="Rated 4 out of 5" |
| 116 | > |
| 117 | <span style={ { width: '80%' } }></span> |
| 118 | </div> |
| 119 | </div> |
| 120 | ) } |
| 121 | </Wrapper> |
| 122 | </div> |
| 123 | ); |
| 124 | }; |
| 125 | |
| 126 | export default Edit; |
| 127 |