-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Core: Add story state to store #9817
Closed
Closed
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
523be64
Refactor story_store.test slightly to get rid of linting errors
tmeasday 7344654
Fix linting and remove redundant test
tmeasday ca79c64
Remove redundant stuff from story_store
tmeasday 45e5c5b
Add very basic story state
tmeasday a6ffc8f
Add STORY_STATE_CHANGED event
tmeasday 654a562
Listen to `CHANGE_STORY_STATE` and change it in the store
tmeasday fdf34c5
Ensure that the state is passed to the story function
tmeasday 654a508
Ensure the story re-renders when the state changes
tmeasday File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
|
||
export default { | ||
title: 'Core/State', | ||
}; | ||
|
||
export const PassedToStory = ({ state: { name } }) => ( | ||
<pre>The value of the name field is {name}</pre> | ||
); | ||
|
||
PassedToStory.propTypes = { | ||
state: PropTypes.shape({ | ||
name: PropTypes.string, | ||
}).isRequired, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,24 +14,13 @@ import { | |
StoreData, | ||
AddStoryArgs, | ||
StoreItem, | ||
StoryState, | ||
ErrorLike, | ||
GetStorybookKind, | ||
} from './types'; | ||
import { HooksContext } from './hooks'; | ||
import storySort from './storySort'; | ||
|
||
// TODO: these are copies from components/nav/lib | ||
// refactor to DRY | ||
const toKey = (input: string) => | ||
input.replace(/[^a-z0-9]+([a-z0-9])/gi, (...params) => params[1].toUpperCase()); | ||
|
||
let count = 0; | ||
|
||
const getId = (): number => { | ||
count += 1; | ||
return count; | ||
}; | ||
|
||
tmeasday marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const toExtracted = <T>(obj: T) => | ||
Object.entries(obj).reduce((acc, [key, value]) => { | ||
if (typeof value === 'function') { | ||
|
@@ -87,13 +76,17 @@ export default class StoryStore extends EventEmitter { | |
this._data = {} as any; | ||
this._revision = 0; | ||
this._selection = {} as any; | ||
this._channel = params.channel; | ||
this._error = undefined; | ||
this._kindOrder = {}; | ||
|
||
if (params.channel) this.setChannel(params.channel); | ||
} | ||
|
||
setChannel = (channel: Channel) => { | ||
this._channel = channel; | ||
channel.on(Events.CHANGE_STORY_STATE, (id: string, newState: StoryState) => | ||
this.setStoryState(id, newState) | ||
); | ||
}; | ||
|
||
// NEW apis | ||
|
@@ -232,6 +225,7 @@ export default class StoryStore extends EventEmitter { | |
...p, | ||
hooks, | ||
parameters: { ...parameters, ...(p && p.parameters) }, | ||
state: _data[id].state, | ||
}); | ||
|
||
_data[id] = { | ||
|
@@ -243,6 +237,7 @@ export default class StoryStore extends EventEmitter { | |
storyFn, | ||
|
||
parameters, | ||
state: {}, | ||
}; | ||
|
||
// Store 1-based order of kind loading to preserve sorting on HMR | ||
|
@@ -308,6 +303,17 @@ export default class StoryStore extends EventEmitter { | |
this.pushToManager(); | ||
} | ||
|
||
setStoryState(id: string, newState: StoryState) { | ||
if (!this._data[id]) throw new Error(`No story for id ${id}`); | ||
const { state } = this._data[id]; | ||
this._data[id].state = { ...state, ...newState }; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there any way to delete something from state? Do we care? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not currently. Let's wait until that use case comes up, maybe? |
||
|
||
// TODO: Sort out what is going on with both the store and the channel being event emitters. | ||
// It has something to do with React Native, but need to get to the bottom of it | ||
this._channel.emit(Events.STORY_STATE_CHANGED, id, this._data[id].state); | ||
this.emit(Events.STORY_STATE_CHANGED, id, this._data[id].state); | ||
} | ||
|
||
// This API is a reimplementation of Storybook's original getStorybook() API. | ||
// As such it may not behave *exactly* the same, but aims to. Some notes: | ||
// - It is *NOT* sorted by the user's sort function, but remains sorted in "insertion order" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is a whole other, larger sorting test.