-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add chainEventHandlers helper function
fix #49
- Loading branch information
1 parent
d2c5853
commit 269b36d
Showing
7 changed files
with
304 additions
and
0 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,51 @@ | ||
import * as React from 'react' | ||
import Button from '@mui/material/Button' | ||
import Dialog from '@mui/material/Dialog' | ||
import DialogTitle from '@mui/material/DialogTitle' | ||
import DialogContent from '@mui/material/DialogContent' | ||
import DialogContentText from '@mui/material/DialogContentText' | ||
import DialogActions from '@mui/material/DialogActions' | ||
import { | ||
usePopupState, | ||
bindTrigger, | ||
bindDialog, | ||
} from 'material-ui-popup-state/hooks' | ||
import { useChainEventHandlers } from 'material-ui-popup-state/useChainEventHandlers' | ||
|
||
const DialogPopupState = () => { | ||
const popupState = usePopupState({ variant: 'dialog' }) | ||
const boundDialogProps = useChainEventHandlers( | ||
bindDialog(popupState), | ||
{ onClose: () => setTimeout(() => alert('closed dialog!'), 10) }, | ||
[popupState] | ||
) | ||
return ( | ||
<React.Fragment> | ||
<Button variant="contained" {...bindTrigger(popupState)}> | ||
Open Dialog | ||
</Button> | ||
<Dialog | ||
{...boundDialogProps} | ||
aria-labelledby="alert-dialog-title" | ||
aria-describedby="alert-dialog-description" | ||
> | ||
<DialogTitle id="alert-dialog-title"> | ||
Chaining Event Handlers example | ||
</DialogTitle> | ||
<DialogContent> | ||
<DialogContentText id="alert-dialog-description"> | ||
After you close this dialog, you should get an alert | ||
</DialogContentText> | ||
</DialogContent> | ||
<DialogActions> | ||
<Button onClick={popupState.close}>Cancel</Button> | ||
<Button onClick={popupState.close} autoFocus> | ||
OK | ||
</Button> | ||
</DialogActions> | ||
</Dialog> | ||
</React.Fragment> | ||
) | ||
} | ||
|
||
export default DialogPopupState |
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,45 @@ | ||
import * as React from 'react' | ||
import Button from '@mui/material/Button' | ||
import Dialog from '@mui/material/Dialog' | ||
import DialogTitle from '@mui/material/DialogTitle' | ||
import DialogContent from '@mui/material/DialogContent' | ||
import DialogContentText from '@mui/material/DialogContentText' | ||
import DialogActions from '@mui/material/DialogActions' | ||
import PopupState, { bindTrigger, bindDialog } from 'material-ui-popup-state' | ||
import { chainEventHandlers } from 'material-ui-popup-state/chainEventHandlers' | ||
|
||
const DialogPopupState = () => ( | ||
<PopupState variant="dialog"> | ||
{(popupState) => ( | ||
<React.Fragment> | ||
<Button variant="contained" {...bindTrigger(popupState)}> | ||
Open Dialog | ||
</Button> | ||
<Dialog | ||
{...chainEventHandlers(bindDialog(popupState), { | ||
onClose: () => setTimeout(() => alert('closed dialog!'), 10), | ||
})} | ||
aria-labelledby="alert-dialog-title" | ||
aria-describedby="alert-dialog-description" | ||
> | ||
<DialogTitle id="alert-dialog-title"> | ||
Chaining Event Handlers example | ||
</DialogTitle> | ||
<DialogContent> | ||
<DialogContentText id="alert-dialog-description"> | ||
After you close this dialog, you should get an alert | ||
</DialogContentText> | ||
</DialogContent> | ||
<DialogActions> | ||
<Button onClick={popupState.close}>Cancel</Button> | ||
<Button onClick={popupState.close} autoFocus> | ||
OK | ||
</Button> | ||
</DialogActions> | ||
</Dialog> | ||
</React.Fragment> | ||
)} | ||
</PopupState> | ||
) | ||
|
||
export default DialogPopupState |
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,88 @@ | ||
/* eslint-disable @typescript-eslint/ban-types */ | ||
export function chainEventHandlers<T1 extends {}, T2 extends {}>( | ||
t1: T1, | ||
t2: T2 | ||
): T1 & T2 | ||
export function chainEventHandlers<T1 extends {}, T2 extends {}, T3 extends {}>( | ||
t1: T1, | ||
t2: T2, | ||
t3: T3 | ||
): T1 & T2 & T3 | ||
export function chainEventHandlers< | ||
T1 extends {}, | ||
T2 extends {}, | ||
T3 extends {}, | ||
T4 extends {} | ||
>(t1: T1, t2: T2, t3: T3, t4: T4): T1 & T2 & T3 & T4 | ||
export function chainEventHandlers< | ||
T1 extends {}, | ||
T2 extends {}, | ||
T3 extends {}, | ||
T4 extends {}, | ||
T5 extends {} | ||
>(t1: T1, t2: T2, t3: T3, t4: T4, t5: T5): T1 & T2 & T3 & T4 & T5 | ||
export function chainEventHandlers< | ||
T1 extends {}, | ||
T2 extends {}, | ||
T3 extends {}, | ||
T4 extends {}, | ||
T5 extends {}, | ||
T6 extends {} | ||
>(t1: T1, t2: T2, t3: T3, t4: T4, t5: T5, t6: T6): T1 & T2 & T3 & T4 & T5 & T6 | ||
export function chainEventHandlers< | ||
T1 extends {}, | ||
T2 extends {}, | ||
T3 extends {}, | ||
T4 extends {}, | ||
T5 extends {}, | ||
T6 extends {}, | ||
T7 extends {} | ||
>( | ||
t1: T1, | ||
t2: T2, | ||
t3: T3, | ||
t4: T4, | ||
t5: T5, | ||
t6: T6, | ||
t7: T7 | ||
): T1 & T2 & T3 & T4 & T5 & T6 & T7 | ||
export function chainEventHandlers< | ||
T1 extends {}, | ||
T2 extends {}, | ||
T3 extends {}, | ||
T4 extends {}, | ||
T5 extends {}, | ||
T6 extends {}, | ||
T7 extends {}, | ||
T8 extends {} | ||
>( | ||
t1: T1, | ||
t2: T2, | ||
t3: T3, | ||
t4: T4, | ||
t5: T5, | ||
t6: T6, | ||
t7: T7, | ||
t8: T8 | ||
): T1 & T2 & T3 & T4 & T5 & T6 & T7 & T8 | ||
export function chainEventHandlers( | ||
first: Record<string, any>, | ||
...rest: Record<string, any>[] | ||
): Record<string, any> { | ||
const result: Record<string, any> = { ...first } | ||
for (const obj of rest) { | ||
for (const key in obj) { | ||
const value = obj[key] | ||
const prev = result[key] | ||
if (typeof prev === 'function' && typeof value === 'function') { | ||
result[key] = (...args: any[]) => { | ||
prev(...args) | ||
return value(...args) | ||
} | ||
} else { | ||
result[key] = value | ||
} | ||
} | ||
} | ||
return result | ||
} |
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,55 @@ | ||
import { describe, it } from 'mocha' | ||
import { expect } from 'chai' | ||
import { chainEventHandlers } from '../src/chainEventHandlers' | ||
|
||
describe(`chainEventHandlers`, function () { | ||
it(`works`, function () { | ||
const calls: [string, any][] = [] | ||
const combined = chainEventHandlers( | ||
{ | ||
a: 1, | ||
b: 3, | ||
onClick: (arg: any) => { | ||
calls.push(['a', arg]) | ||
}, | ||
}, | ||
{ | ||
a: 2, | ||
onClick: (arg: any) => { | ||
calls.push(['b', arg]) | ||
}, | ||
onBlah: (arg: any) => { | ||
calls.push(['blah-b', arg]) | ||
}, | ||
}, | ||
{ | ||
c: 4, | ||
onClick: (arg: any) => { | ||
calls.push(['c', arg]) | ||
}, | ||
onBlah: (arg: any) => { | ||
calls.push(['blah-c', arg]) | ||
}, | ||
} | ||
) | ||
expect(Object.keys(combined).sort()).to.deep.equal( | ||
['a', 'b', 'c', 'onClick', 'onBlah'].sort() | ||
) | ||
expect(combined.a).to.equal(2) | ||
expect(combined.b).to.equal(3) | ||
expect(combined.c).to.equal(4) | ||
|
||
combined.onClick('foo') | ||
expect(calls).to.deep.equal([ | ||
['a', 'foo'], | ||
['b', 'foo'], | ||
['c', 'foo'], | ||
]) | ||
calls.length = 0 | ||
combined.onBlah('blah') | ||
expect(calls).to.deep.equal([ | ||
['blah-b', 'blah'], | ||
['blah-c', 'blah'], | ||
]) | ||
}) | ||
}) |