File tree 2 files changed +46
-10
lines changed
2 files changed +46
-10
lines changed Original file line number Diff line number Diff line change 8
8
defineComponent ,
9
9
ref ,
10
10
serializeInner ,
11
- createApp
11
+ createApp ,
12
+ provide ,
13
+ inject
12
14
} from '@vue/runtime-test'
13
15
import { render as domRender , nextTick } from 'vue'
14
16
@@ -212,6 +214,32 @@ describe('component props', () => {
212
214
expect ( defaultFn ) . toHaveBeenCalledTimes ( 1 )
213
215
} )
214
216
217
+ test ( 'using inject in default value factory' , ( ) => {
218
+ const Child = defineComponent ( {
219
+ props : {
220
+ test : {
221
+ default : ( ) => inject ( 'test' , 'default' )
222
+ }
223
+ } ,
224
+ setup ( props ) {
225
+ return ( ) => {
226
+ return h ( 'div' , props . test )
227
+ }
228
+ }
229
+ } )
230
+
231
+ const Comp = {
232
+ setup ( ) {
233
+ provide ( 'test' , 'injected' )
234
+ return ( ) => h ( Child )
235
+ }
236
+ }
237
+
238
+ const root = nodeOps . createElement ( 'div' )
239
+ render ( h ( Comp ) , root )
240
+ expect ( serializeInner ( root ) ) . toBe ( `<div>injected</div>` )
241
+ } )
242
+
215
243
test ( 'optimized props updates' , async ( ) => {
216
244
const Child = defineComponent ( {
217
245
props : [ 'foo' ] ,
Original file line number Diff line number Diff line change @@ -27,7 +27,8 @@ import {
27
27
Data ,
28
28
ComponentInternalInstance ,
29
29
ComponentOptions ,
30
- ConcreteComponent
30
+ ConcreteComponent ,
31
+ setCurrentInstance
31
32
} from './component'
32
33
import { isEmitListener } from './componentEmits'
33
34
import { InternalObjectKey } from './vnode'
@@ -179,7 +180,8 @@ export function updateProps(
179
180
options ,
180
181
rawCurrentProps ,
181
182
camelizedKey ,
182
- value
183
+ value ,
184
+ instance
183
185
)
184
186
}
185
187
} else {
@@ -214,7 +216,8 @@ export function updateProps(
214
216
options ,
215
217
rawProps || EMPTY_OBJ ,
216
218
key ,
217
- undefined
219
+ undefined ,
220
+ instance
218
221
)
219
222
}
220
223
} else {
@@ -277,7 +280,8 @@ function setFullProps(
277
280
options ! ,
278
281
rawCurrentProps ,
279
282
key ,
280
- rawCurrentProps [ key ]
283
+ rawCurrentProps [ key ] ,
284
+ instance
281
285
)
282
286
}
283
287
}
@@ -287,18 +291,22 @@ function resolvePropValue(
287
291
options : NormalizedProps ,
288
292
props : Data ,
289
293
key : string ,
290
- value : unknown
294
+ value : unknown ,
295
+ instance : ComponentInternalInstance
291
296
) {
292
297
const opt = options [ key ]
293
298
if ( opt != null ) {
294
299
const hasDefault = hasOwn ( opt , 'default' )
295
300
// default values
296
301
if ( hasDefault && value === undefined ) {
297
302
const defaultValue = opt . default
298
- value =
299
- opt . type !== Function && isFunction ( defaultValue )
300
- ? defaultValue ( props )
301
- : defaultValue
303
+ if ( opt . type !== Function && isFunction ( defaultValue ) ) {
304
+ setCurrentInstance ( instance )
305
+ value = defaultValue ( props )
306
+ setCurrentInstance ( null )
307
+ } else {
308
+ value = defaultValue
309
+ }
302
310
}
303
311
// boolean casting
304
312
if ( opt [ BooleanFlags . shouldCast ] ) {
You can’t perform that action at this time.
0 commit comments