Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/src/Navigation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,8 @@ export class NavigationRoot {
/**
* Update a mounted component's props
*/
public updateProps(componentId: string, props: object) {
this.commands.updateProps(componentId, props);
public updateProps(componentId: string, props: object, callback?: () => void) {
this.commands.updateProps(componentId, props, callback);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions lib/src/NavigationDelegate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,8 @@ export class NavigationDelegate {
/**
* Update a mounted component's props
*/
public updateProps(componentId: string, props: object) {
this.concreteNavigation.updateProps(componentId, props);
public updateProps(componentId: string, props: object, callback?: () => void) {
this.concreteNavigation.updateProps(componentId, props, callback);
}

/**
Expand Down
13 changes: 13 additions & 0 deletions lib/src/commands/Commands.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,19 @@ describe('Commands', () => {
)
);
});

test('update props with callback', done => {
function callback () {
try {
expect(true).toBe(true);
done();
} catch (error) {
done(error);
}
}

uut.updateProps('theComponentId', { someProp: 'someValue' }, callback);
});
});

describe('showModal', () => {
Expand Down
4 changes: 2 additions & 2 deletions lib/src/commands/Commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ export class Commands {
this.commandsObserver.notify(CommandName.MergeOptions, { componentId, options });
}

public updateProps(componentId: string, props: object) {
this.store.updateProps(componentId, props);
public updateProps(componentId: string, props: object, callback?: () => void) {
this.store.updateProps(componentId, props, callback);
this.commandsObserver.notify(CommandName.UpdateProps, { componentId, props });
}

Expand Down
13 changes: 13 additions & 0 deletions lib/src/components/ComponentWrapper.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,19 @@ describe('ComponentWrapper', () => {
});
});

test('update props with callback', done => {
function callback () {
try {
expect(true).toBe(true);
done();
} catch (error) {
done(error);
}
}

store.updateProps('component123', { someProp: 'someValue' }, callback);
});

it('updates props from store into inner component', () => {
const NavigationComponent = uut.wrap(
componentName,
Expand Down
6 changes: 3 additions & 3 deletions lib/src/components/ComponentWrapper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ interface HocProps {
}

export interface IWrappedComponent extends React.Component {
setProps(newProps: Record<string, any>): void;
setProps(newProps: Record<string, any>, callback?: () => void): void;
isMounted: boolean;
}

Expand Down Expand Up @@ -56,13 +56,13 @@ export class ComponentWrapper {
store.setComponentInstance(props.componentId, this);
}

public setProps(newProps: any) {
public setProps(newProps: any, callback?: () => void) {
this.setState((prevState) => ({
allProps: {
...prevState.allProps,
...newProps,
},
}));
}), callback);
}

componentDidMount() {
Expand Down
13 changes: 13 additions & 0 deletions lib/src/components/Store.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,19 @@ describe('Store', () => {
expect(uut.getPropsForId('component1')).toEqual({});
});

test('update props with callback', done => {
function callback () {
try {
expect(true).toBe(true);
done();
} catch (error) {
done(error);
}
}

uut.updateProps('component1', { someProp: 'someValue' }, callback);
});

it('holds original components classes by componentName', () => {
const MyWrappedComponent = () => class MyComponent extends React.Component {};
uut.setComponentClassForName('example.mycomponent', MyWrappedComponent);
Expand Down
4 changes: 2 additions & 2 deletions lib/src/components/Store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ export class Store {
private wrappedComponents: Record<string, React.ComponentClass<any>> = {};
private lazyRegistratorFn: ((lazyComponentRequest: string | number) => void) | undefined;

updateProps(componentId: string, props: any) {
updateProps(componentId: string, props: any, callback?: () => void) {
this.mergeNewPropsForId(componentId, props);
const component = this.componentsInstancesById[componentId];
if (component) {
this.componentsInstancesById[componentId].setProps(props);
this.componentsInstancesById[componentId].setProps(props, callback);
}
}

Expand Down
9 changes: 5 additions & 4 deletions website/docs/api/api-component.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,11 @@ Navigation.setLazyComponentRegistrator((componentName) => {
Update props of a component registered with [registerComponent](#registercomponent). Updating props triggers the component lifecycle methods related to [updating](https://reactjs.org/docs/react-component.html#updating).

#### Parameters
| Name | Required | Type | Description |
| ------ | -------- | ------------------- | ---------------------------------------------------------------------------------------------------------------------- |
| componentId | Yes | string | Unique component id |
| options | Yes | object | props object to pass to the component |
| Name | Required | Type | Description |
| ----------- | -------- | -------- | ---------------------------------------------------------------------------------------------------------------------- |
| componentId | Yes | string | Unique component id |
| options | Yes | object | Props object to pass to the component |
| callback | No | Function | Function that will be executed once inner `setState` is completed |

#### Example
```js
Expand Down