@@ -2,17 +2,32 @@ import PropTypes from 'prop-types';
22import arrayMove from 'array-move' ;
33import styled from '@emotion/styled' ;
44import { select } from '@wordpress/data' ;
5- import { useState } from '@wordpress/element' ; // eslint-disable-line
5+ import { useState , useEffect , useMemo } from '@wordpress/element' ;
66import { __ } from '@wordpress/i18n' ;
77import { ContentSearch } from '../ContentSearch' ;
88import SortableList from './SortableList' ;
99
1010const NAMESPACE = '10up-content-picker' ;
1111
12+ /**
13+ * Unfortunately, we had to use !important because on PickedItem we couldn't @emotion/styled css
14+ * as it was breaking sortability from react-sortable-hoc
15+ */
16+ const StyleWrapper = styled ( 'div' ) `
17+ & .block-editor-link-control__search-item {
18+ border: none !important;
19+ cursor: default;
20+
21+ &:hover {
22+ background: transparent;
23+ }
24+ }
25+ ` ;
26+
1227/**
1328 * Content Picker
1429 *
15- * @param {Object } props React props
30+ * @param {object } props React props
1631 * @param props.label
1732 * @param props.mode
1833 * @param props.contentTypes
@@ -25,7 +40,7 @@ const NAMESPACE = '10up-content-picker';
2540 * @param props.content
2641 * @param props.uniqueContentItems
2742 * @param props.excludeCurrentPost
28- * @return {* } React JSX
43+ * @returns {* } React JSX
2944 */
3045const ContentPicker = ( {
3146 label,
@@ -54,55 +69,40 @@ const ContentPicker = ({
5469 }
5570 }
5671
57- function handleSelect ( item ) {
58- const newContent = [ ...content ] ;
59-
60- newContent . unshift ( {
61- id : item . id ,
62- type : 'subtype' in item ? item . subtype : item . type ,
63- } ) ;
64-
65- onPickChange ( newContent ) ;
66-
67- setContent ( newContent ) ;
68- }
69-
70- function handleItemDelete ( item , sortIndex ) {
71- const newContent = [ ...content ] ;
72-
73- newContent . splice ( sortIndex , 1 ) ;
74-
75- onPickChange ( newContent ) ;
76-
77- setContent ( newContent ) ;
78- }
79-
80- /**
81- * Unfortunately, we had to use !important because on PickedItem we couldn't @emotion/styled css
82- * as it was breaking sortability from react-sortable-hoc
83- */
84- const StyleWrapper = styled ( 'div' ) `
85- & .block-editor-link-control__search-item {
86- border: none !important;
87- cursor: default;
88-
89- &:hover {
90- background: transparent;
91- }
72+ // Run onPickChange callback when content changes.
73+ useEffect ( ( ) => {
74+ onPickChange ( content ) ;
75+ } , [ content , onPickChange ] ) ;
76+
77+ const handleSelect = ( item ) => {
78+ setContent ( ( previousContent ) => [
79+ {
80+ id : item . id ,
81+ type : 'subtype' in item ? item . subtype : item . type ,
82+ } ,
83+ ...previousContent ,
84+ ] ) ;
85+ } ;
86+
87+ const onDeleteItem = ( deletedItem ) => {
88+ setContent ( ( previousContent ) => previousContent . filter ( ( { id } ) => id !== deletedItem . id ) ) ;
89+ } ;
90+
91+ const excludeItems = useMemo ( ( ) => {
92+ const items = uniqueContentItems ? [ ...content ] : [ ] ;
93+
94+ if ( excludeCurrentPost && currentPostId ) {
95+ items . push ( {
96+ id : currentPostId ,
97+ } ) ;
9298 }
93- ` ;
94-
95- const excludeItems = uniqueContentItems ? [ ...content ] : [ ] ;
9699
97- if ( excludeCurrentPost && currentPostId ) {
98- excludeItems . push ( {
99- id : currentPostId ,
100- } ) ;
101- }
100+ return items ;
101+ } , [ content , currentPostId , excludeCurrentPost , uniqueContentItems ] ) ;
102102
103103 return (
104104 < div className = { `${ NAMESPACE } ` } >
105- { ! content . length || ( content . length && content . length < maxContentItems ) ? (
105+ { ( ! content . length || ( content . length && content . length < maxContentItems ) ) && (
106106 < ContentSearch
107107 placeholder = { placeholder }
108108 label = { label }
@@ -111,8 +111,8 @@ const ContentPicker = ({
111111 contentTypes = { contentTypes }
112112 mode = { mode }
113113 />
114- ) : null }
115- { content . length ? (
114+ ) }
115+ { Boolean ( content ? .length ) > 0 && (
116116 < StyleWrapper >
117117 < span
118118 style = { {
@@ -127,19 +127,17 @@ const ContentPicker = ({
127127 < SortableList
128128 items = { content }
129129 useDragHandle
130+ handleItemDelete = { onDeleteItem }
130131 isOrderable = { isOrderable }
131132 mode = { mode }
132- handleItemDelete = { handleItemDelete }
133133 onSortEnd = { ( { oldIndex, newIndex } ) => {
134134 const newContent = [ ...arrayMove ( content , oldIndex , newIndex ) ] ;
135135
136- onPickChange ( newContent ) ;
137-
138136 setContent ( newContent ) ;
139137 } }
140138 />
141139 </ StyleWrapper >
142- ) : null }
140+ ) }
143141 </ div >
144142 ) ;
145143} ;
0 commit comments