-
-
Notifications
You must be signed in to change notification settings - Fork 102
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Loading status checks…
feat: basic template ref
Showing
10 changed files
with
135 additions
and
12 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
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,31 @@ | ||
import { | ||
NodeTypes, | ||
type SimpleExpressionNode, | ||
findProp, | ||
} from '@vue/compiler-dom' | ||
import type { NodeTransform } from '../transform' | ||
import { type IRExpression, IRNodeTypes } from '../ir' | ||
import { normalizeBindShorthand } from './vBind' | ||
|
||
export const transformRef: NodeTransform = (node, context) => { | ||
if (node.type !== NodeTypes.ELEMENT) return | ||
const dir = findProp(node, 'ref', false, true) | ||
|
||
if (!dir) return | ||
|
||
let value: IRExpression | ||
if (dir.type === NodeTypes.DIRECTIVE) { | ||
value = | ||
(dir.exp as SimpleExpressionNode | undefined) || | ||
normalizeBindShorthand(dir.arg as SimpleExpressionNode) | ||
} else { | ||
value = dir.value ? JSON.stringify(dir.value.content) : '""' | ||
} | ||
|
||
context.registerOperation({ | ||
type: IRNodeTypes.SET_REF, | ||
element: context.reference(), | ||
value, | ||
loc: dir.loc, | ||
}) | ||
} |
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,52 @@ | ||
import { type Ref, type SchedulerJob, isRef } from '@vue/reactivity' | ||
import { currentInstance } from '../component' | ||
import { VaporErrorCodes, callWithErrorHandling } from '../errorHandling' | ||
import { hasOwn, isFunction, isString } from '@vue/shared' | ||
import { warn } from '../warning' | ||
import { queuePostRenderEffect } from '../scheduler' | ||
|
||
export type NodeRef = string | Ref | ((ref: Element) => void) | ||
|
||
/** | ||
* Function for handling a template ref | ||
*/ | ||
export function setRef(el: Element, ref: NodeRef) { | ||
if (!currentInstance) return | ||
const { setupState, isUnmounted } = currentInstance | ||
|
||
if (isFunction(ref)) { | ||
callWithErrorHandling(ref, currentInstance, VaporErrorCodes.FUNCTION_REF, [ | ||
el, | ||
// refs, | ||
]) | ||
} else { | ||
const _isString = isString(ref) | ||
const _isRef = isRef(ref) | ||
|
||
if (_isString || _isRef) { | ||
const doSet = () => { | ||
if (_isString) { | ||
if (hasOwn(setupState, ref)) { | ||
setupState[ref] = el | ||
} | ||
} else if (_isRef) { | ||
ref.value = el | ||
} else if (__DEV__) { | ||
warn('Invalid template ref type:', ref, `(${typeof ref})`) | ||
} | ||
} | ||
// #9908 ref on v-for mutates the same array for both mount and unmount | ||
// and should be done together | ||
if (isUnmounted /* || isVFor */) { | ||
doSet() | ||
} else { | ||
// #1789: set new refs in a post job so that they don't get overwritten | ||
// by unmounting ones. | ||
;(doSet as SchedulerJob).id = -1 | ||
queuePostRenderEffect(doSet) | ||
} | ||
} else if (__DEV__) { | ||
warn('Invalid template ref type:', ref, `(${typeof ref})`) | ||
} | ||
} | ||
} |
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