@@ -8,9 +8,15 @@ import { CalendarDate, Time } from '@internationalized/date'
88import * as theme from ' #build/ui'
99import { get , set } from ' #ui/utils'
1010
11+ interface CastImport {
12+ name: string
13+ from: string
14+ }
15+
1116interface Cast {
1217 get: (args : any ) => any
1318 template: (args : any ) => string
19+ imports: CastImport []
1420}
1521
1622type CastDateValue = [number , number , number ]
@@ -21,13 +27,15 @@ const castMap: Record<string, Cast> = {
2127 get : (args : CastDateValue ) => new CalendarDate (... args ),
2228 template : (value : CalendarDate ) => {
2329 return value ? ` new CalendarDate(${value .year }, ${value .month }, ${value .day }) ` : ' null'
24- }
30+ },
31+ imports: [{ name: ' CalendarDate' , from: ' @internationalized/date' }]
2532 },
2633 ' DateValue[]' : {
2734 get : (args : CastDateValue []) => args .map (date => new CalendarDate (... date )),
2835 template : (value : CalendarDate []) => {
2936 return value ? ` [${value .map (date => ` new CalendarDate(${date .year }, ${date .month }, ${date .day }) ` ).join (' , ' )}] ` : ' []'
30- }
37+ },
38+ imports: [{ name: ' CalendarDate' , from: ' @internationalized/date' }]
3139 },
3240 ' DateRange' : {
3341 get : (args : { start: CastDateValue , end: CastDateValue }) => ({ start: new CalendarDate (... args .start ), end: new CalendarDate (... args .end ) }),
@@ -37,13 +45,15 @@ const castMap: Record<string, Cast> = {
3745 }
3846
3947 return ` { start: new CalendarDate(${value .start .year }, ${value .start .month }, ${value .start .day }), end: new CalendarDate(${value .end .year }, ${value .end .month }, ${value .end .day }) } `
40- }
48+ },
49+ imports: [{ name: ' CalendarDate' , from: ' @internationalized/date' }]
4150 },
4251 ' TimeValue' : {
4352 get : (args : CastTimeValue ) => new Time (... args ),
4453 template : (value : Time ) => {
4554 return value ? ` new Time(${value .hour }, ${value .minute }, ${value .second }) ` : ' null'
46- }
55+ },
56+ imports: [{ name: ' Time' , from: ' @internationalized/date' }]
4757 }
4858}
4959
@@ -214,21 +224,46 @@ ${props.slots?.default}
214224 code += `
215225<script setup lang="ts">
216226`
227+ // Collect imports from cast types
228+ const importsBySource = new Map <string , Set <string >>()
229+ for (const key of props .external ) {
230+ const cast = props .cast ?.[key ]
231+ if (cast && castMap [cast ]) {
232+ for (const imp of castMap [cast ].imports ) {
233+ if (! importsBySource .has (imp .from )) {
234+ importsBySource .set (imp .from , new Set ())
235+ }
236+ importsBySource .get (imp .from )! .add (imp .name )
237+ }
238+ }
239+ }
240+
241+ // Generate import statements
242+ for (const [source, names] of importsBySource ) {
243+ code += ` import { ${Array .from (names ).join (' , ' )} } from '${source }'
244+ `
245+ }
246+
217247 if (props .externalTypes ?.length ) {
218248 const removeArrayBrackets = (type : string ): string => type .endsWith (' []' ) ? removeArrayBrackets (type .slice (0 , - 2 )) : type
219249
220250 const types = props .externalTypes .map (type => removeArrayBrackets (type ))
221251 code += ` import type { ${types .join (' , ' )} } from '@nuxt/ui'
252+ `
253+ }
222254
255+ if (importsBySource .size > 0 || props .externalTypes ?.length ) {
256+ code += `
223257`
224258 }
225259
226260 for (const [i, key] of props .external .entries ()) {
227261 const cast = props .cast ?.[key ]
228262 const value = cast ? castMap [cast ]! .template (componentProps [key ]) : json5 .stringify (componentProps [key ], null , 2 )?.replace (/ ,([ |\t\n ] + [}|\] ] )/ g , ' $1' )
229263 const type = props .externalTypes ?.[i ] ? ` <${props .externalTypes [i ]}> ` : ' '
264+ const refType = cast ? ' shallowRef' : ' ref'
230265
231- code += ` const ${key === ' modelValue' ? ' value' : key } = ref ${type }(${value })
266+ code += ` const ${key === ' modelValue' ? ' value' : key } = ${ refType } ${type }(${value })
232267`
233268 }
234269 code += ` <\/ script>
@@ -307,7 +342,9 @@ ${props.slots?.default}
307342 return code
308343})
309344
310- const { data : ast } = await useAsyncData (` component-code-${name }-${hash ({ props: componentProps , slots: props .slots , external: props .external , externalTypes: props .externalTypes , collapse: props .collapse })} ` , async () => {
345+ const codeKey = computed (() => ` component-code-${name }-${hash (props )} ` )
346+
347+ const { data : ast } = await useAsyncData (codeKey , async () => {
311348 if (! props .prettier ) {
312349 return parseMarkdown (code .value )
313350 }
0 commit comments