feat: 更新自定义切换组件,添加 TypeScript 支持并调整属性定义
All checks were successful
/ test (push) Successful in 13s
/ surge (push) Successful in 41s

This commit is contained in:
严浩
2024-11-21 14:06:40 +08:00
parent 3d3d9f0a7f
commit 9161a5aaa3
2 changed files with 23 additions and 9 deletions

View File

@ -15,7 +15,6 @@ import FormKitToggle from "./src/headlessui-switch.vue";
const library = createLibraryPlugin({
text, form, submit, group, checkbox, range, list, number, textarea,
mytoggle: createInput(FormKitToggle)
})
const validation = createValidationPlugin(defaultRules)

View File

@ -1,12 +1,30 @@
<script lang="ts">
import { FormKitFrameworkContext } from '@formkit/core';
import type { FormKitInputs } from '@formkit/inputs';
declare module '@formkit/inputs' {
// https://formkit.com/essentials/custom-inputs#typescript-support
interface FormKitInputProps<Props extends FormKitInputs<Props>> {
// This key and the `type` should match:
'mytoggle': {
// Define your input `type`:
type: 'mytoggle',
someProp: string,
}
}
}
</script>
<script setup lang="ts">
// https://www.ajahandideh.com/articles/formkit-custom-toggle
// https://headlessui.com/v1/vue
import { Switch, SwitchGroup, SwitchLabel } from '@headlessui/vue';
import { computed } from 'vue';
const props = defineProps({
context: Object,
});
const props = defineProps<{
context: FormKitFrameworkContext;
}>();
const value = computed({
get: () => {
@ -17,15 +35,12 @@ const value = computed({
},
});
const label = props.context?.label;
const name = props.context?.name;
</script>
<template>
<SwitchGroup>
<SwitchGroup data-switch-group-template-root>
<div class="flex items-center">
<SwitchLabel class="mr-4">{{ label }}</SwitchLabel>
<SwitchLabel class="mr-4">{{ context?.label }}</SwitchLabel>
<Switch
v-model="value"
:class='value ? "bg-blue-600" : "bg-gray-200"'