All files / react-classic/src/view Types.tsx

0% Statements 0/0
0% Branches 0/0
0% Functions 0/0
0% Lines 0/0

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90                                                                                                                                                                                   
import type { ReactNode, RefObject } from 'react';
import type { Bindable, ReactiveProps, VariableRef } from '../index.js';
import type { WritableKeys } from '@anchorlib/core';
 
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export type InitProps = { [key: string]: any };
 
export type TextBinding = 'text' | 'password' | 'email' | 'tel' | 'url' | 'search' | 'color' | 'time';
export type NumberBinding = 'number' | 'range';
export type BooleanBinding = 'checkbox' | 'radio';
export type BindingType = TextBinding | NumberBinding | BooleanBinding | 'file' | 'date';
 
export type BindingKeys<Value, Binding extends Bindable> = WritableKeys<Binding> extends infer K
  ? K extends keyof Binding
    ? Binding[K] extends Value
      ? K
      : never
    : never
  : never;
 
// Base binding props that are common to all binding types
type BaseBindingProps<Kind extends BindingType, Props extends InitProps> = ReactiveProps<Props> & {
  type?: Kind;
  ref?: RefObject<unknown>;
};
 
// Variable binding (using VariableRef)
export type VariableBindingProps<
  Kind extends BindingType = 'text',
  Props extends InitProps = InitProps,
> = BaseBindingProps<Kind, Props> & {
  bind: VariableRef<GetValueByBindingType<Kind>>;
};
 
// State binding with prop
export type StateFlatBindingProps<
  Kind extends BindingType = 'text',
  Binding extends Bindable = Bindable,
  Props extends InitProps = InitProps,
> = BaseBindingProps<Kind, Props> & {
  bind: Binding;
  prop: BindingKeys<GetValueByBindingType<Kind>, Binding>;
};
 
// State binding with name (for backward compatibility)
export type StateFlexBindingProps<
  Kind extends BindingType = 'text',
  Binding extends Bindable = Bindable,
  Props extends InitProps = InitProps,
> = BaseBindingProps<Kind, Props> & {
  bind: Binding;
  name: BindingKeys<GetValueByBindingType<Kind>, Binding>;
};
 
// Helper type to get the value type based on the binding type
export type GetValueByBindingType<Kind extends BindingType> = Kind extends NumberBinding
  ? number | undefined
  : Kind extends BooleanBinding
    ? boolean | undefined
    : Kind extends 'date'
      ? Date | undefined
      : Kind extends 'file'
        ? FileList | undefined
        : string | undefined;
 
// Union of all possible binding props
export type InputBindingProps<
  Kind extends BindingType = 'text',
  Binding extends Bindable = Bindable,
  Props extends InitProps = InitProps,
> =
  | VariableBindingProps<Kind, Props>
  | StateFlatBindingProps<Kind, Binding, Props>
  | StateFlexBindingProps<Kind, Binding, Props>;
 
export interface InputBinding<Props extends InitProps = InitProps> {
  (props: Props): ReactNode;
  <Bind extends VariableRef<unknown> | Bindable, Kind extends BindingType = 'text'>(
    props: Bind extends VariableRef<unknown>
      ? VariableBindingProps<Kind, Props>
      : StateFlatBindingProps<Kind, Bind, Props>
  ): ReactNode;
  <Bind extends VariableRef<unknown> | Bindable, Kind extends BindingType = 'text'>(
    props: Bind extends VariableRef<unknown>
      ? VariableBindingProps<Kind, Props>
      : StateFlexBindingProps<Kind, Bind, Props>
  ): ReactNode;
  <Kind extends BindingType = 'text'>(props: VariableBindingProps<Kind, Props>): ReactNode;
}