Skip to content

Instantly share code, notes, and snippets.

@rawnly
Last active July 20, 2022 13:53
Show Gist options
  • Save rawnly/730da1f2969cd78bac4f5c8a0545a2c2 to your computer and use it in GitHub Desktop.
Save rawnly/730da1f2969cd78bac4f5c8a0545a2c2 to your computer and use it in GitHub Desktop.
type IO<T, Input = never> = Input extends never ? () => T : (input: Input) => T;
export type InputType = 'text' | 'number' | 'password' | 'email' | 'date' | 'time' | 'datetime-local' | 'month' | 'week' | 'url' | 'search' | 'tel' | 'color';
export type BaseInput<Shape extends object, Metadata = Record<string, any>> = {
placeholder?: string;
required?: string | boolean;
disabled?: string | boolean;
readonly?: string | boolean;
autofocus?: string | boolean;
autocomplete?: string | boolean;
name?: keyof Shape;
label?: string | IO<string>;
hint?: string | IO<string>;
validate?: RegExp | ((value: string) => boolean) | { value: RegExp, message: string }
metadata?: Metadata;
}
export type Input<T extends object, U = Record<string, any>> = BaseInput<T, U> & (
{
type: 'number'
min?: number;
max?: number;
} | {
type: 'date' | 'time' | 'datetime-local' | 'month' | 'week';
min?: string;
max?: string;
} | {
type: Exclude<InputType, 'date' | 'time' | 'datetime-local' | 'month' | 'week' | 'number'>;
minLength?: number;
maxLength?: number;
pattern?: string | RegExp
}
)
export type FormStructure<T extends object, Metadata = Record<string, any>> = (Input<T, Metadata> | Input<T, Metadata>[])[]
import { FormStructure } from './generated-form-types'
export const useFormStructure = <T extends object, Metadata>(structure: FormStructure<T, Metadata>): FormStructure<T, Metadata> => structure;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment