12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879 |
- import type {
- BaseFormComponentType,
- FormCommonConfig,
- VbenFormAdapterOptions,
- } from './types';
- import type { Component } from 'vue';
- import { h } from 'vue';
- import {
- VbenButton,
- VbenCheckbox,
- Input as VbenInput,
- VbenInputPassword,
- VbenPinInput,
- VbenSelect,
- } from '@vben-core/shadcn-ui';
- import { defineRule } from 'vee-validate';
- const DEFAULT_MODEL_PROP_NAME = 'modelValue';
- export const DEFAULT_FORM_COMMON_CONFIG: FormCommonConfig = {};
- export const COMPONENT_MAP: Record<BaseFormComponentType, Component> = {
- DefaultResetActionButton: h(VbenButton, { size: 'sm', variant: 'outline' }),
- DefaultSubmitActionButton: h(VbenButton, { size: 'sm', variant: 'default' }),
- VbenCheckbox,
- VbenInput,
- VbenInputPassword,
- VbenPinInput,
- VbenSelect,
- };
- export const COMPONENT_BIND_EVENT_MAP: Partial<
- Record<BaseFormComponentType, string>
- > = {
- VbenCheckbox: 'checked',
- };
- export function setupVbenForm<
- T extends BaseFormComponentType = BaseFormComponentType,
- >(options: VbenFormAdapterOptions<T>) {
- const { components, config, defineRules } = options;
- const { disabledOnChangeListener = false, emptyStateValue = undefined } =
- (config || {}) as FormCommonConfig;
- Object.assign(DEFAULT_FORM_COMMON_CONFIG, {
- disabledOnChangeListener,
- emptyStateValue,
- });
- if (defineRules) {
- for (const key of Object.keys(defineRules)) {
- defineRule(key, defineRules[key as never]);
- }
- }
- const baseModelPropName =
- config?.baseModelPropName ?? DEFAULT_MODEL_PROP_NAME;
- const modelPropNameMap = config?.modelPropNameMap as
- | Record<BaseFormComponentType, string>
- | undefined;
- for (const component of Object.keys(components)) {
- const key = component as BaseFormComponentType;
- COMPONENT_MAP[key] = components[component as never];
- if (baseModelPropName !== DEFAULT_MODEL_PROP_NAME) {
- COMPONENT_BIND_EVENT_MAP[key] = baseModelPropName;
- }
- // 覆盖特殊组件的modelPropName
- if (modelPropNameMap && modelPropNameMap[key]) {
- COMPONENT_BIND_EVENT_MAP[key] = modelPropNameMap[key];
- }
- }
- }
|