-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
253 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
.smacb-rating-box { | ||
&__body {} | ||
|
||
&__item { | ||
&:not(:first-child) { | ||
@include _padding-top(.5); | ||
} | ||
|
||
&__title { | ||
font-weight: bold; | ||
} | ||
|
||
&__evaluation { | ||
&__bar, | ||
&__rating { | ||
height: 10px; | ||
border-radius: 3px; | ||
} | ||
|
||
&__bar { | ||
position: relative; | ||
background-color: _lighter($_color-gray); | ||
} | ||
|
||
&__rating { | ||
position: absolute; | ||
top: 0; | ||
left: 0; | ||
background-color: #f9bb2d; | ||
} | ||
|
||
&__numeric { | ||
position: absolute; | ||
top: $_base-font-size-px * -1; | ||
right: 0; | ||
@include _font-size($_base-font-size-px - 4); | ||
line-height: 1; | ||
color: $_color-gray; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
'use strict'; | ||
|
||
const { get, times } = lodash; | ||
const { registerBlockType } = wp.blocks; | ||
const { RichText, InspectorControls, ColorPalette } = wp.editor; | ||
const { PanelBody, RangeControl, BaseControl } = wp.components; | ||
const { Fragment } = wp.element; | ||
const { __, sprintf } = wp.i18n; | ||
|
||
registerBlockType('snow-monkey-awesome-custom-blocks/rating-box', { | ||
title: __('Rating box', 'snow-monkey-awesome-custom-blocks'), | ||
icon: 'editor-alignleft', | ||
category: 'smacb', | ||
attributes: { | ||
ratings: { | ||
type: 'array', | ||
default: [ | ||
{ | ||
title: [], | ||
rating: 0, | ||
color: '', | ||
} | ||
], | ||
}, | ||
rows: { | ||
type: 'number', | ||
default: 1, | ||
}, | ||
}, | ||
|
||
edit({ attributes, setAttributes, isSelected }) { | ||
const { rows, ratings } = attributes; | ||
|
||
const generateUpdatedAttribute = (parent, index, attribute, value) => { | ||
let newParent = [...parent]; | ||
newParent[ index ] = get(newParent, index, {}); | ||
newParent[ index ][ attribute ] = value; | ||
return newParent; | ||
} | ||
|
||
return ( | ||
<Fragment> | ||
<InspectorControls> | ||
<PanelBody title={ __('Rating box Settings', 'snow-monkey-awesome-custom-blocks') }> | ||
<RangeControl | ||
label={ __('Rows', 'snow-monkey-awesome-custom-blocks') } | ||
value={ rows } | ||
onChange={ value => setAttributes({ rows: value }) } | ||
min="1" | ||
max="10" | ||
/> | ||
</PanelBody> | ||
|
||
{ times(rows, (index) => { | ||
const title = get(ratings, [index, 'title'], []); | ||
const rating = get(ratings, [index, 'rating'], 0); | ||
const color = get(ratings, [index, 'color'], ''); | ||
|
||
return ( | ||
<PanelBody | ||
title={ sprintf( __('(%d) Rating Settings', 'snow-monkey-awesome-custom-blocks'), index + 1) } | ||
initialOpen={ false } | ||
> | ||
<RangeControl | ||
label={ __('Rating', 'snow-monkey-awesome-custom-blocks') } | ||
value={ rating } | ||
onChange={ value => setAttributes({ ratings: generateUpdatedAttribute(ratings, index, 'rating', value) }) } | ||
min="1" | ||
max="10" | ||
/> | ||
|
||
<BaseControl label={ __('Color', 'snow-monkey-awesome-custom-blocks') }> | ||
<ColorPalette | ||
value={ color } | ||
onChange={ value => setAttributes({ ratings: generateUpdatedAttribute(ratings, index, 'color', value) }) } | ||
/> | ||
</BaseControl> | ||
</PanelBody> | ||
); | ||
} ) } | ||
</InspectorControls> | ||
|
||
<div className="smacb-rating-box"> | ||
<div className="smacb-rating-box__body"> | ||
{ times(rows, (index) => { | ||
const title = get(ratings, [index, 'title'], []); | ||
const rating = get(ratings, [index, 'rating'], 0); | ||
const color = get(ratings, [index, 'color'], ''); | ||
|
||
return ( | ||
<div className="smacb-rating-box__item"> | ||
<RichText | ||
className="smacb-rating-box__item__title" | ||
placeholder={ __('Write title…', 'snow-monkey-awesome-custom-blocks') } | ||
value={ title } | ||
formattingControls={ [] } | ||
multiline={ false } | ||
onChange={ value => setAttributes({ ratings: generateUpdatedAttribute(ratings, index, 'title', value) }) } | ||
/> | ||
|
||
<div className="smacb-rating-box__item__evaluation"> | ||
<div className="smacb-rating-box__item__evaluation__bar"> | ||
<div className="smacb-rating-box__item__evaluation__numeric"> | ||
{rating} | ||
</div> | ||
<div className="smacb-rating-box__item__evaluation__rating" style={{ width: `${rating * 10}%`, backgroundColor: color }}></div> | ||
</div> | ||
</div> | ||
</div> | ||
); | ||
} ) } | ||
</div> | ||
</div> | ||
</Fragment> | ||
); | ||
}, | ||
|
||
save({ attributes }) { | ||
const { rows, ratings } = attributes; | ||
|
||
return ( | ||
<div className="smacb-rating-box"> | ||
<div className="smacb-rating-box__body"> | ||
{ times(rows, (index) => { | ||
const title = get(ratings, [index, 'title'], []); | ||
const rating = get(ratings, [index, 'rating'], 0); | ||
const color = get(ratings, [index, 'color'], ''); | ||
|
||
return ( | ||
<div className="smacb-rating-box__item"> | ||
<div className="smacb-rating-box__item__title" > | ||
{ title } | ||
</div> | ||
|
||
<div className="smacb-rating-box__item__evaluation"> | ||
<div className="smacb-rating-box__item__evaluation__bar"> | ||
<div className="smacb-rating-box__item__evaluation__numeric"> | ||
{rating} | ||
</div> | ||
<div className="smacb-rating-box__item__evaluation__rating" style={{ width: `${rating * 10}%`, backgroundColor: color }}></div> | ||
</div> | ||
</div> | ||
</div> | ||
); | ||
} ) } | ||
</div> | ||
</div> | ||
); | ||
}, | ||
} ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Oops, something went wrong.