|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const { get, times } = lodash; |
| 4 | +const { registerBlockType } = wp.blocks; |
| 5 | +const { RichText, InspectorControls } = wp.editor; |
| 6 | +const { PanelBody, RangeControl } = wp.components; |
| 7 | +const { Fragment } = wp.element; |
| 8 | +const { __, sprintf } = wp.i18n; |
| 9 | + |
| 10 | +registerBlockType('snow-monkey-awesome-custom-blocks/faq', { |
| 11 | + title: __('FAQ', 'snow-monkey-awesome-custom-blocks'), |
| 12 | + icon: 'businessman', |
| 13 | + category: 'smacb', |
| 14 | + attributes: { |
| 15 | + content: { |
| 16 | + type: 'array', |
| 17 | + default: [ |
| 18 | + { |
| 19 | + question: [], |
| 20 | + answer: [], |
| 21 | + } |
| 22 | + ], |
| 23 | + }, |
| 24 | + rows: { |
| 25 | + type: 'number', |
| 26 | + default: 1, |
| 27 | + }, |
| 28 | + }, |
| 29 | + |
| 30 | + edit({ attributes, setAttributes, isSelected }) { |
| 31 | + const { rows, content } = attributes; |
| 32 | + |
| 33 | + const generateUpdatedAttribute = (parent, index, attribute, value) => { |
| 34 | + let newParent = [...parent]; |
| 35 | + newParent[ index ] = get(newParent, index, {}); |
| 36 | + newParent[ index ][ attribute ] = value; |
| 37 | + return newParent; |
| 38 | + } |
| 39 | + |
| 40 | + return ( |
| 41 | + <Fragment> |
| 42 | + <InspectorControls> |
| 43 | + <PanelBody title={ __('FAQ Settings', 'snow-monkey-awesome-custom-blocks') }> |
| 44 | + <RangeControl |
| 45 | + label={ __('Rows', 'snow-monkey-awesome-custom-blocks') } |
| 46 | + value={ rows } |
| 47 | + onChange={ value => setAttributes({ rows: value }) } |
| 48 | + min="1" |
| 49 | + max="50" |
| 50 | + /> |
| 51 | + </PanelBody> |
| 52 | + </InspectorControls> |
| 53 | + |
| 54 | + <div className="smacb-faq"> |
| 55 | + <div className="smacb-faq__body"> |
| 56 | + { times(rows, (index) => { |
| 57 | + const question = get(content, [index, 'question'], []); |
| 58 | + const answer = get(content, [index, 'answer'], []); |
| 59 | + |
| 60 | + return ( |
| 61 | + <div className="smacb-faq__item"> |
| 62 | + <RichText |
| 63 | + className="smacb-faq__item__question" |
| 64 | + placeholder={ __('Write question…', 'snow-monkey-awesome-custom-blocks') } |
| 65 | + value={ question } |
| 66 | + formattingControls={ [] } |
| 67 | + multiline={ false } |
| 68 | + onChange={ value => setAttributes({ content: generateUpdatedAttribute(content, index, 'question', value) }) } |
| 69 | + /> |
| 70 | + |
| 71 | + <RichText |
| 72 | + className="smacb-faq__item__answer" |
| 73 | + placeholder={ __('Write answer…', 'snow-monkey-awesome-custom-blocks') } |
| 74 | + value={ answer } |
| 75 | + formattingControls={ [] } |
| 76 | + multiline="p" |
| 77 | + onChange={ value => setAttributes({ content: generateUpdatedAttribute(content, index, 'answer', value) }) } |
| 78 | + /> |
| 79 | + </div> |
| 80 | + ); |
| 81 | + } ) } |
| 82 | + </div> |
| 83 | + </div> |
| 84 | + </Fragment> |
| 85 | + ); |
| 86 | + }, |
| 87 | + |
| 88 | + save({ attributes }) { |
| 89 | + const { rows, content } = attributes; |
| 90 | + |
| 91 | + return ( |
| 92 | + <div className="smacb-faq"> |
| 93 | + <div className="smacb-faq__body"> |
| 94 | + { times(rows, (index) => { |
| 95 | + const question = get(content, [index, 'question'], []); |
| 96 | + const answer = get(content, [index, 'answer'], []); |
| 97 | + |
| 98 | + return ( |
| 99 | + <div className="smacb-faq__item"> |
| 100 | + <div className="smacb-faq__item__question"> |
| 101 | + { question } |
| 102 | + </div> |
| 103 | + <div className="smacb-faq__item__answer"> |
| 104 | + { answer } |
| 105 | + </div> |
| 106 | + </div> |
| 107 | + ); |
| 108 | + } ) } |
| 109 | + </div> |
| 110 | + </div> |
| 111 | + ); |
| 112 | + }, |
| 113 | +} ); |
0 commit comments