import type { Component, InputHTMLAttributes } from "vue";
import type { ZodAny, z } from "zod";
import type { INPUT_COMPONENTS } from "./constant";
export interface FieldProps {
fieldName: string;
label?: string;
required?: boolean;
config?: ConfigItem;
disabled?: boolean;
}
export interface Shape {
type: string;
default?: any;
required?: boolean;
options?: string[];
schema?: ZodAny;
}
export interface ConfigItem {
/** Value for the `FormLabel` */
label?: string;
/** Value for the `FormDescription` */
description?: string;
/** Pick which component to be rendered. */
component?: keyof typeof INPUT_COMPONENTS | Component;
/** Hide `FormLabel`. */
hideLabel?: boolean;
inputProps?: InputHTMLAttributes;
}
// Define a type to unwrap an array
type UnwrapArray = T extends (infer U)[] ? U : never;
export type Config = {
// If SchemaType.key is an object, create a nested Config, otherwise ConfigItem
[Key in keyof SchemaType]?: SchemaType[Key] extends any[]
? UnwrapArray>
: SchemaType[Key] extends object
? Config
: ConfigItem;
};
export enum DependencyType {
DISABLES,
REQUIRES,
HIDES,
SETS_OPTIONS,
}
interface BaseDependency>> {
sourceField: keyof SchemaType;
type: DependencyType;
targetField: keyof SchemaType;
when: (sourceFieldValue: any, targetFieldValue: any) => boolean;
}
export type ValueDependency>> =
BaseDependency & {
type:
| DependencyType.DISABLES
| DependencyType.REQUIRES
| DependencyType.HIDES;
};
export type EnumValues = readonly [string, ...string[]];
export type OptionsDependency<
SchemaType extends z.infer>,
> = BaseDependency & {
type: DependencyType.SETS_OPTIONS;
// Partial array of values from sourceField that will trigger the dependency
options: EnumValues;
};
export type Dependency>> =
| ValueDependency
| OptionsDependency;