-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Widgets editor] Disallow multiple instances of reference widgets (#2…
…6148) * Disallow multiple instances of reference widgets * Improve visuals * Inline inspector controls which are now only used once * Fix issue with filter converting options object to array by amalgamating logic into previous pickBy callback * Switch to using placeholder instructions text Co-authored-by: Daniel Richards <[email protected]>
- Loading branch information
Showing
5 changed files
with
201 additions
and
107 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
52 changes: 0 additions & 52 deletions
52
packages/edit-widgets/src/blocks/legacy-widget/edit/placeholder.js
This file was deleted.
Oops, something went wrong.
77 changes: 77 additions & 0 deletions
77
packages/edit-widgets/src/blocks/legacy-widget/edit/widget-select-control.js
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,77 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { pickBy, isEmpty, map } from 'lodash'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useSelect } from '@wordpress/data'; | ||
import { useMemo } from '@wordpress/element'; | ||
import { __ } from '@wordpress/i18n'; | ||
import { SelectControl } from '@wordpress/components'; | ||
|
||
export default function WidgetSelectControl( { | ||
availableLegacyWidgets, | ||
currentWidget, | ||
onChange, | ||
label = __( 'Select a legacy widget to display:' ), | ||
emptyLabel = __( 'There are no widgets available.' ), | ||
} ) { | ||
const usedReferenceWidgetNames = useSelect( ( select ) => | ||
select( 'core/edit-widgets' ) | ||
.getReferenceWidgetBlocks() | ||
.map( ( { attributes } ) => attributes?.referenceWidgetName ) | ||
); | ||
const choosableLegacyWidgets = useMemo( () => { | ||
return pickBy( | ||
availableLegacyWidgets, | ||
// Filter out hidden widgets and already used reference widgets. | ||
( { isHidden }, referenceWidgetName ) => | ||
! isHidden && | ||
( ! referenceWidgetName || | ||
! usedReferenceWidgetNames.includes( referenceWidgetName ) ) | ||
); | ||
}, [ availableLegacyWidgets, usedReferenceWidgetNames ] ); | ||
|
||
if ( isEmpty( choosableLegacyWidgets ) ) { | ||
return emptyLabel; | ||
} | ||
|
||
return ( | ||
<SelectControl | ||
label={ label } | ||
value={ currentWidget || 'none' } | ||
onChange={ ( newWidget ) => { | ||
const { | ||
isReferenceWidget, | ||
id_base: idBase, | ||
} = availableLegacyWidgets[ newWidget ]; | ||
|
||
if ( isReferenceWidget ) { | ||
onChange( { | ||
instance: {}, | ||
idBase, | ||
referenceWidgetName: newWidget, | ||
widgetClass: undefined, | ||
} ); | ||
} else { | ||
onChange( { | ||
instance: {}, | ||
idBase, | ||
referenceWidgetName: undefined, | ||
widgetClass: newWidget, | ||
} ); | ||
} | ||
} } | ||
options={ [ { value: 'none', label: 'Select widget' } ].concat( | ||
map( choosableLegacyWidgets, ( widget, key ) => { | ||
return { | ||
value: key, | ||
label: widget.name, | ||
}; | ||
} ) | ||
) } | ||
/> | ||
); | ||
} |
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
Oops, something went wrong.