-
Notifications
You must be signed in to change notification settings - Fork 343
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
Showing
10 changed files
with
142 additions
and
54 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { getCurrentInstance, SetupContext } from '../runtimeContext' | ||
import { warn } from '../utils' | ||
|
||
export function useSlots(): SetupContext['slots'] { | ||
return getContext().slots | ||
} | ||
|
||
export function useAttrs(): SetupContext['attrs'] { | ||
return getContext().attrs | ||
} | ||
|
||
function getContext(): SetupContext { | ||
const i = getCurrentInstance()! | ||
if (__DEV__ && !i) { | ||
warn(`useContext() called without active instance.`) | ||
} | ||
return i.setupContext! | ||
} |
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
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,49 @@ | ||
import { | ||
createApp, | ||
defineComponent, | ||
SetupContext, | ||
useAttrs, | ||
useSlots, | ||
} from '../../../src' | ||
|
||
describe('SFC <script setup> helpers', () => { | ||
// test('useSlots / useAttrs (no args)', () => { | ||
// let slots: SetupContext['slots'] | undefined | ||
// let attrs: SetupContext['attrs'] | undefined | ||
// const Comp = { | ||
// setup() { | ||
// slots = useSlots() | ||
// attrs = useAttrs() | ||
// return () => {} | ||
// } | ||
// } | ||
// const passedAttrs = { id: 'foo' } | ||
// const passedSlots = { | ||
// default: () => {}, | ||
// x: () => {} | ||
// } | ||
// const root = document.createElement('div') | ||
// const vm = createApp(Comp).mount(root) | ||
// expect(typeof slots!.default).toBe('function') | ||
// expect(typeof slots!.x).toBe('function') | ||
// expect(attrs).toMatchObject(passedAttrs) | ||
// }) | ||
|
||
test('useSlots / useAttrs (with args)', () => { | ||
let slots: SetupContext['slots'] | undefined | ||
let attrs: SetupContext['attrs'] | undefined | ||
let ctx: SetupContext | undefined | ||
const Comp = defineComponent({ | ||
setup(_, _ctx) { | ||
slots = useSlots() | ||
attrs = useAttrs() | ||
ctx = _ctx | ||
return () => {} | ||
}, | ||
}) | ||
const root = document.createElement('div') | ||
createApp(Comp, { foo: 'bar' }).mount(root) | ||
expect(slots).toBe(ctx!.slots) | ||
expect(attrs).toBe(ctx!.attrs) | ||
}) | ||
}) |