-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
take a slightly different approach from the other stories - wrap SpaceList in a component specifically made for StoryBook testing that passes values to a Context Provider and renders the component under test inside the Provider. This lets us craft an interface in StoryBook that will make sense to designers and other folks.
- Loading branch information
Showing
8 changed files
with
510 additions
and
210 deletions.
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
37 changes: 37 additions & 0 deletions
37
examples/react/playground/src/stories/SpaceList.stories.tsx
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,37 @@ | ||
import { Space } from '@w3ui/keyring-core' | ||
import type { DID } from '@ucanto/interface' | ||
|
||
import React from 'react' | ||
import { SpaceList } from '@w3ui/react' | ||
import { KeyringContext, keyringContextDefaultValue } from '@w3ui/react-keyring' | ||
|
||
function contextValue (state = {}, actions = {}) { | ||
return [ | ||
{ ...keyringContextDefaultValue[0], ...state }, | ||
{ ...keyringContextDefaultValue[1], ...actions } | ||
] | ||
} | ||
|
||
function WrappedSpaceList ({ spaceDIDs = [], setCurrentSpace }: { spaceDIDs: DID[], setCurrentSpace: any }) { | ||
const spaces = spaceDIDs.map(did => new Space(did, {})) | ||
return ( | ||
<KeyringContext.Provider value={contextValue({ spaces }, { setCurrentSpace })}> | ||
<SpaceList /> | ||
</KeyringContext.Provider> | ||
) | ||
} | ||
|
||
export default { | ||
title: 'w3ui/SpaceList', | ||
component: WrappedSpaceList, | ||
tags: ['autodocs'], | ||
argTypes: { | ||
setCurrentSpace: { action: 'set space' } | ||
} | ||
} | ||
|
||
export const Primary = { | ||
args: { | ||
spaceDIDs: ['did:example:abc123'] | ||
} | ||
} |
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
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 |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import type { Space } from '@w3ui/keyring-core' | ||
|
||
import React from 'react' | ||
import { useKeyring } from '@w3ui/react-keyring' | ||
|
||
export function SpaceList (props: any): JSX.Element { | ||
const [{ space: currentSpace, spaces }, { setCurrentSpace }] = useKeyring() | ||
async function selectSpace (space: Space): Promise<void> { | ||
await setCurrentSpace(space.did()) | ||
} | ||
return ( | ||
<ul {...props}> | ||
{spaces.map((space, i) => ( | ||
<li key={space.did()} className={`hover:font-bold ${space.sameAs(currentSpace) ? 'font-bold' : ''}`}> | ||
<button onClick={() => { void selectSpace(space) }}> | ||
{space.name() ?? `Space ${i + 1}`} | ||
</button> | ||
</li> | ||
))} | ||
</ul> | ||
) | ||
} |
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.