Skip to content

Commit

Permalink
fix: add missing collection observer to watch changes (#443)
Browse files Browse the repository at this point in the history
  • Loading branch information
ghiscoding authored Dec 14, 2024
1 parent 8887111 commit ca9340d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
1 change: 0 additions & 1 deletion src/examples/slickgrid/Example3.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -634,7 +634,6 @@ export default function Example3() {

/*
// remove your column the full set of columns
// and use slice or spread [...] to trigger an React dirty change
allOriginalColumns.pop();
columnDefinitions = allOriginalColumns.slice();
*/
Expand Down
24 changes: 21 additions & 3 deletions src/slickgrid-react/components/slickgrid-react.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {

// services
BackendUtilityService,
collectionObserver,
CollectionService,
EventNamingStyle,
ExtensionService,
Expand Down Expand Up @@ -97,6 +98,7 @@ export class SlickgridReact<TData = any> extends React.Component<SlickgridReactP
protected _currentDatasetLength = 0;
protected _dataset: any[] | null = null;
protected _elm?: HTMLDivElement | null;
protected _collectionObservers: Array<null | ({ disconnect: () => void; })> = [];
protected _eventHandler!: SlickEventHandler;
protected _eventPubSubService!: EventPubSubService;
protected _hideHeaderRowAfterPageLoad = false;
Expand Down Expand Up @@ -602,6 +604,9 @@ export class SlickgridReact<TData = any> extends React.Component<SlickgridReactP
this.instances = reactElementInstance;
this.setStateValue('instances', reactElementInstance);
this._eventPubSubService.publish(`onReactGridCreated`, reactElementInstance);

// subscribe to column definitions assignment changes
this.observeColumnDefinitions();
}

componentWillUnmount(shouldEmptyDomElementContainer = false) {
Expand All @@ -614,6 +619,7 @@ export class SlickgridReact<TData = any> extends React.Component<SlickgridReactP
this.emptyGridContainerElm();
}

this._collectionObservers.forEach(obs => obs?.disconnect());
this._eventPubSubService.publish(`onAfterGridDestroyed`, true);

// dispose of all Services
Expand Down Expand Up @@ -696,7 +702,7 @@ export class SlickgridReact<TData = any> extends React.Component<SlickgridReactP

if (this.props.columnDefinitions !== prevProps.columnDefinitions) {
this._columnDefinitions = this.props.columnDefinitions;
this.columnDefinitionsChanged();
this.columnDefinitionsChanged(this.props.columnDefinitions);
}

if (this.props.dataset !== prevProps.dataset) {
Expand All @@ -709,8 +715,10 @@ export class SlickgridReact<TData = any> extends React.Component<SlickgridReactP
this.suggestDateParsingWhenHelpful();
}

columnDefinitionsChanged() {
this._columnDefinitions = this.props.columnDefinitions;
columnDefinitionsChanged(columnDefinitions?: Column[]) {
if (columnDefinitions) {
this._columnDefinitions = columnDefinitions;
}
if (this._isGridInitialized) {
this.updateColumnDefinitionsList(this._columnDefinitions);
}
Expand Down Expand Up @@ -1176,6 +1184,16 @@ export class SlickgridReact<TData = any> extends React.Component<SlickgridReactP
// protected functions
// ------------------

/**
* assignment changes are not triggering on the column definitions, for that
* we can use our internal array observer for any changes done via (push, pop, shift, ...)
*/
protected observeColumnDefinitions(): void {
this._collectionObservers.push(
collectionObserver(this._columnDefinitions, this.columnDefinitionsChanged.bind(this))
);
}

/**
* Loop through all column definitions and copy the original optional `width` properties optionally provided by the user.
* We will use this when doing a resize by cell content, if user provided a `width` it won't override it.
Expand Down

0 comments on commit ca9340d

Please sign in to comment.