AdvancedFields.js
2 years ago
AttributeHelp.js
2 years ago
BlockAddPrevButton.js
2 years ago
BlockAdvancedValue.js
2 years ago
BlockClassName.js
2 years ago
BlockDefaultValue.js
2 years ago
BlockDescription.js
2 years ago
BlockLabel.js
2 years ago
BlockName.js
2 years ago
BlockPlaceholder.js
2 years ago
BlockPrevButtonLabel.js
2 years ago
BlockRequired.js
2 years ago
BlockVisibility.js
2 years ago
FieldControl.js
2 years ago
FieldSettingsWrapper.js
2 years ago
FieldWrapper.js
2 years ago
GeneralFields.js
2 years ago
SelectVariations.js
2 years ago
ToggleGroupVariations.js
2 years ago
ToolBarDefault.js
2 years ago
ToolBarFields.js
2 years ago
ToggleGroupVariations.js
107 lines
| 1 | import useBlockAttributes from '../hooks/useBlockAttributes'; |
| 2 | |
| 3 | const { |
| 4 | Children, |
| 5 | cloneElement, |
| 6 | useContext, |
| 7 | } = wp.element; |
| 8 | |
| 9 | const { |
| 10 | useSelect, |
| 11 | } = wp.data; |
| 12 | |
| 13 | const { |
| 14 | useBlockEditContext, |
| 15 | } = wp.blockEditor; |
| 16 | |
| 17 | let { |
| 18 | __experimentalToggleGroupControl, |
| 19 | __experimentalToggleGroupControlOptionIcon, |
| 20 | __experimentalToolbarContext, |
| 21 | ToggleGroupControl, |
| 22 | ToggleGroupControlOptionIcon, |
| 23 | ToolbarItem, |
| 24 | ToolbarGroup, |
| 25 | ToolbarContext, |
| 26 | } = wp.components; |
| 27 | |
| 28 | ToggleGroupControl = ( |
| 29 | ToggleGroupControl || __experimentalToggleGroupControl |
| 30 | ); |
| 31 | ToggleGroupControlOptionIcon = ( |
| 32 | ToggleGroupControlOptionIcon || __experimentalToggleGroupControlOptionIcon |
| 33 | ); |
| 34 | ToolbarContext = ( |
| 35 | ToolbarContext || __experimentalToolbarContext |
| 36 | ); |
| 37 | |
| 38 | function ToggleGroupVariationsBase( { value } ) { |
| 39 | const { name } = useBlockEditContext(); |
| 40 | const toolbarState = useContext( ToolbarContext ); |
| 41 | |
| 42 | const [ , setAttributes ] = useBlockAttributes(); |
| 43 | |
| 44 | const { variations, components } = useSelect( |
| 45 | ( select ) => { |
| 46 | const { |
| 47 | getBlockVariations, |
| 48 | } = select( 'core/blocks' ); |
| 49 | |
| 50 | const items = getBlockVariations( name, 'block' ); |
| 51 | |
| 52 | return { |
| 53 | variations: items, |
| 54 | components: items.map( |
| 55 | current => ( |
| 56 | toolbarState?.currentId ?? |
| 57 | toolbarState?.baseId |
| 58 | ) |
| 59 | ? <ToolbarItem |
| 60 | as={ ToggleGroupControlOptionIcon } |
| 61 | value={ current.name } |
| 62 | label={ current.title } |
| 63 | icon={ current.icon } |
| 64 | /> : <ToggleGroupControlOptionIcon |
| 65 | value={ current.name } |
| 66 | label={ current.title } |
| 67 | icon={ current.icon } |
| 68 | /> ), |
| 69 | }; |
| 70 | }, |
| 71 | [], |
| 72 | ); |
| 73 | |
| 74 | if ( !variations.length ) { |
| 75 | return null; |
| 76 | } |
| 77 | |
| 78 | return <ToggleGroupControl |
| 79 | hideLabelFromVision |
| 80 | onChange={ varName => setAttributes( { |
| 81 | ...variations.find( |
| 82 | ( { name } ) => name === varName, |
| 83 | ).attributes, |
| 84 | } ) } |
| 85 | value={ value } |
| 86 | isBlock |
| 87 | > |
| 88 | { Children.map( |
| 89 | components, |
| 90 | cloneElement, |
| 91 | ) } |
| 92 | </ToggleGroupControl>; |
| 93 | } |
| 94 | |
| 95 | function ToggleGroupVariations( props ) { |
| 96 | const toolbarState = useContext( ToolbarContext ); |
| 97 | |
| 98 | if ( !toolbarState?.currentId ) { |
| 99 | return <ToggleGroupVariationsBase { ...props }/>; |
| 100 | } |
| 101 | |
| 102 | return <ToolbarGroup className={ 'jet-fb toggle-toolbar-group' }> |
| 103 | <ToggleGroupVariationsBase { ...props }/> |
| 104 | </ToolbarGroup>; |
| 105 | } |
| 106 | |
| 107 | export default ToggleGroupVariations; |