Skip to content

Commit 0b8753d

Browse files
committedJan 2, 2024
feat: adds new didMount state and mounted event
1 parent 32f3983 commit 0b8753d

File tree

4 files changed

+43
-1
lines changed

4 files changed

+43
-1
lines changed
 

‎packages/vue/__tests__/inputs/text.spec.ts

+17
Original file line numberDiff line numberDiff line change
@@ -294,4 +294,21 @@ describe('the number feature', () => {
294294
const node = getNode(id)!
295295
expect(node.value).toBe(12345)
296296
})
297+
it('knows when it is mounted', async () => {
298+
const wrapper = mount(FormKit, {
299+
props: {
300+
type: 'text',
301+
help: 'what happens next?',
302+
sectionsSchema: {
303+
help: {
304+
children: '$didMount && "Mounted" || "Not mounted"',
305+
},
306+
},
307+
},
308+
...global,
309+
})
310+
expect(wrapper.find('.formkit-help').text()).toBe('Not mounted')
311+
await nextTick()
312+
expect(wrapper.find('.formkit-help').text()).toBe('Mounted')
313+
})
297314
})

‎packages/vue/src/FormKit.ts

+14-1
Original file line numberDiff line numberDiff line change
@@ -153,12 +153,25 @@ function FormKit<Props extends FormKitInputs<Props>>(
153153
...definitionLibrary,
154154
}
155155

156+
/**
157+
* Emit the mounted event.
158+
*/
159+
function didMount() {
160+
node.emit('mounted')
161+
}
162+
156163
// // Expose the FormKitNode to template refs.
157164
context.expose({ node })
158165
return () =>
159166
h(
160167
FormKitSchema,
161-
{ schema: schema.value, data: node.context, library, memoKey },
168+
{
169+
schema: schema.value,
170+
data: node.context,
171+
onMounted: didMount,
172+
library,
173+
memoKey,
174+
},
162175
{ ...context.slots }
163176
)
164177
}

‎packages/vue/src/FormKitSchema.ts

+4
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
ConcreteComponent,
1919
onUnmounted,
2020
markRaw,
21+
onMounted,
2122
} from 'vue'
2223
import { has, isPojo } from '@formkit/utils'
2324
import {
@@ -883,6 +884,7 @@ export const FormKitSchema = /* #__PURE__ */ defineComponent({
883884
required: false,
884885
},
885886
},
887+
emits: ['mounted'],
886888
setup(props, context) {
887889
const instance = getCurrentInstance()
888890
let instanceKey = {}
@@ -939,6 +941,8 @@ export const FormKitSchema = /* #__PURE__ */ defineComponent({
939941
/* eslint-enable @typescript-eslint/no-non-null-assertion */
940942
}
941943

944+
// When the component is mounted, emit the mounted event
945+
onMounted(() => context.emit('mounted'))
942946
// For browser rendering:
943947
onUnmounted(cleanUp)
944948
// For SSR rendering:

‎packages/vue/src/bindings.ts

+8
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,7 @@ const vueBindings: FormKitPlugin = function vueBindings(node) {
277277
items,
278278
label: node.props.label,
279279
messages,
280+
didMount: false,
280281
node: markRaw(node),
281282
options: node.props.options,
282283
defaultMessagePlacement: true,
@@ -317,6 +318,13 @@ const vueBindings: FormKitPlugin = function vueBindings(node) {
317318
})()
318319
})
319320

321+
/**
322+
* When the node mounts, set the didMount flag.
323+
*/
324+
node.on('mounted', () => {
325+
context.didMount = true
326+
})
327+
320328
/**
321329
* Sets the settled state.
322330
*/

0 commit comments

Comments
 (0)
Please sign in to comment.