-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Truong Hoang Dung
authored and
Truong Hoang Dung
committed
May 11, 2024
1 parent
69fbd04
commit 48e558f
Showing
6 changed files
with
93 additions
and
7 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
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,9 @@ | ||
# How to contribute | ||
|
||
## Develop locally with storybook | ||
|
||
```js | ||
pnpm dev | ||
``` | ||
|
||
## Create pull request |
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,63 @@ | ||
import React from 'react'; | ||
import { state$ } from '../hooks'; | ||
|
||
export type ExampleProps = { | ||
text?: String; | ||
}; | ||
interface Message { | ||
Decrement: { | ||
amount: number; | ||
}; | ||
Increment: {}; | ||
} | ||
interface AppState { | ||
loading?: boolean; | ||
value: number; | ||
} | ||
export function AsyncExample(props: ExampleProps) { | ||
const [count, dispatch] = state$<AppState, Message>({ value: 0 }, [ | ||
{ | ||
messages: ['Increment'], | ||
handler: async (c) => { | ||
c.loading.set(true); | ||
await new Promise((resolve) => { | ||
setTimeout(resolve, 1000); | ||
}); | ||
c.loading.set(false); | ||
c.value.set(c.value.get() + 1); | ||
}, | ||
onChange: (c) => { | ||
// eslint-disable-next-line no-console | ||
console.log(`current count is ${c.get()}`); | ||
}, | ||
}, | ||
{ | ||
messages: ['Decrement'], | ||
handler: (c, payload) => { | ||
if ('amount' in payload) { | ||
c.value.set(c.value.get() - payload.amount ?? 1); | ||
} | ||
}, | ||
}, | ||
]); | ||
return ( | ||
<> | ||
{`${props.text} ${count.value}`} | ||
<button | ||
onClick={() => dispatch('Increment', {})} | ||
type="button" | ||
id="increment-button" | ||
disabled={count.loading} | ||
> | ||
Increment | ||
</button> | ||
<button | ||
onClick={() => dispatch('Decrement', { amount: 3 })} | ||
type="button" | ||
id="decrement-button" | ||
> | ||
Decrement | ||
</button> | ||
</> | ||
); | ||
} |
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,18 @@ | ||
import React from 'react'; | ||
import { Meta, StoryFn } from '@storybook/react'; | ||
|
||
import { AsyncExample as Example } from '../examples/Async'; | ||
|
||
export default { | ||
title: 'AsyncExample', | ||
component: Example, | ||
argTypes: {}, | ||
} as Meta<typeof Example>; | ||
|
||
const Template: StoryFn<typeof Example> = (args) => <Example {...args} />; | ||
|
||
export const Primary = Template.bind({}); | ||
|
||
Primary.args = { | ||
text: 'Clicked this many times:', | ||
}; |