Skip to content

Commit

Permalink
Merge pull request #16508 from pahan35/fix/15278-take-args-state-from…
Browse files Browse the repository at this point in the history
…-url

Core: Fix args values updated from url to control
  • Loading branch information
shilman committed Nov 7, 2021
2 parents 36a59af + ded097e commit 8fc84eb
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
10 changes: 10 additions & 0 deletions lib/preview-web/src/PreviewWeb.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,16 @@ describe('PreviewWeb', () => {
foo: 'url',
});
});
it('updates args from the URL', async () => {
document.location.search = '?id=component-one--a&args=foo:url';

await createAndRenderPreview();

expect(mockChannel.emit).toHaveBeenCalledWith(Events.STORY_ARGS_UPDATED, {
storyId: 'component-one--a',
args: { foo: 'url' },
});
});

it('allows async getProjectAnnotations', async () => {
const preview = new PreviewWeb();
Expand Down
15 changes: 8 additions & 7 deletions lib/preview-web/src/PreviewWeb.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -401,14 +401,12 @@ export class PreviewWeb<TFramework extends AnyFramework> {
// - a story selected in "docs" viewMode,
// in which case we render the docsPage for that story
async renderSelection({ persistedArgs }: { persistedArgs?: Args } = {}) {
if (!this.urlStore.selection) {
const { selection } = this.urlStore;
if (!selection) {
throw new Error('Cannot render story as no selection was made');
}

const {
selection,
selection: { storyId },
} = this.urlStore;
const { storyId } = selection;

let story;
try {
Expand Down Expand Up @@ -457,8 +455,11 @@ export class PreviewWeb<TFramework extends AnyFramework> {
args,
});
}
// If the implementation changed, the args also may have changed
if (implementationChanged) {

// For v6 mode / compatibility
// If the implementation changed, or args were persisted, the args may have changed,
// and the STORY_PREPARED event above may not be respected.
if (implementationChanged || persistedArgs) {
this.channel.emit(Events.STORY_ARGS_UPDATED, { storyId, args });
}

Expand Down

0 comments on commit 8fc84eb

Please sign in to comment.