@@ -73,6 +73,13 @@ type Props = {
7373 */
7474 onLoad : ( params : any ) => Promise < any > ,
7575
76+ /**
77+ * Callback fired when the list is reloaded as a result of an operation outside of this component.
78+ *
79+ * @param state
80+ */
81+ onReload ?: ( state : any ) => any ,
82+
7683 /**
7784 * Callback fired when an item is saved via the add/edit modal.
7885 */
@@ -89,6 +96,12 @@ type Props = {
8996 */
9097 polling ?: number ,
9198
99+ /**
100+ * If true, the data for the list will be re-fetched. This is useful for operations that change the state of the
101+ * list outside of this component.
102+ */
103+ reload ?: boolean ,
104+
92105 /**
93106 * Callback fired when an error occurs. The passed error can take any form and is up to the consuming component to
94107 * interpret. The return value should be an array of user-friendly error messages.
@@ -114,7 +127,13 @@ type Props = {
114127 session ?: {
115128 key : string ,
116129 storage : typeof sessionStorage
117- }
130+ } ,
131+
132+ /**
133+ * The item in the list to update. This is useful if the items can be modified outside of the list and should
134+ * reflect the changes without loading the entire list.
135+ */
136+ updateItem ?: any
118137} ;
119138
120139type State = {
@@ -183,6 +202,14 @@ const useDataList = (WrappedComponent: ComponentType<any>) => (
183202 if ( prevProps . saved !== this . props . saved && this . props . saved ) {
184203 this . setState ( { saved : this . props . saved } , this . fetchData . bind ( this ) ) ;
185204 }
205+
206+ if ( prevProps . updateItem !== this . props . updateItem && this . props . updateItem ) {
207+ this . onUpdateItem ( ) ;
208+ }
209+
210+ if ( prevProps . reload !== this . props . reload && this . props . reload ) {
211+ this . setState ( this . onReload . bind ( this ) , this . fetchData . bind ( this ) ) ;
212+ }
186213 }
187214
188215 /**
@@ -435,7 +462,7 @@ const useDataList = (WrappedComponent: ComponentType<any>) => (
435462 * When no columns are sortable, load data as is
436463 *
437464 */
438- onInit ( page ? : number = 1 ) {
465+ onInit ( page : number = 1 ) {
439466 this . setState ( { sortColumn : '' , sortDirection : '' , page } , this . fetchData . bind ( this ) ) ;
440467 }
441468
@@ -460,6 +487,21 @@ const useDataList = (WrappedComponent: ComponentType<any>) => (
460487 this . setState ( { page : 1 , perPage : value } , this . fetchData . bind ( this ) ) ;
461488 }
462489
490+ /**
491+ * Calls the onReload prop if provided and returns the value.
492+ *
493+ * @param state
494+ *
495+ * @returns {* }
496+ */
497+ onReload ( state ) {
498+ if ( ! this . props . onReload ) {
499+ return state ;
500+ }
501+
502+ return this . props . onReload ( state ) ;
503+ }
504+
463505 /**
464506 * Calls the onSave prop and reloads the data.
465507 *
@@ -500,7 +542,7 @@ const useDataList = (WrappedComponent: ComponentType<any>) => (
500542 * @param direction
501543 * @param page
502544 */
503- onSort ( sortColumn : string , direction ? : string , page ? : number = 1 ) {
545+ onSort ( sortColumn : string , direction ? : string , page : number = 1 ) {
504546 let sortDirection = direction ;
505547
506548 if ( ! sortDirection ) {
@@ -511,6 +553,15 @@ const useDataList = (WrappedComponent: ComponentType<any>) => (
511553 this . setState ( { sortColumn, sortDirection, page } , this . fetchData . bind ( this ) ) ;
512554 }
513555
556+ /**
557+ * Replaces the item in the list with the updateItem.
558+ */
559+ onUpdateItem ( ) {
560+ this . setState ( ( state ) => ( {
561+ items : _ . map ( state . items , ( item ) => item . id !== this . props . updateItem . id ? item : this . props . updateItem )
562+ } ) ) ;
563+ }
564+
514565 /**
515566 * Renders the DataList component.
516567 *
0 commit comments