summaryrefslogtreecommitdiff
path: root/packages/exchange-backoffice-ui/src/handlers/useField.ts
blob: 94635646f72b89b6f257814af67d5c0dc302ddbb (plain)
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import { TargetedEvent, useContext, useState } from "preact/compat";
import {
  FormContext,
  InputArrayFieldState,
  InputFieldState,
} from "./FormProvider.js";

export interface InputFieldHandler<Type> {
  value: Type;
  onChange: (s: Type) => void;
  state: InputFieldState;
  isDirty: boolean;
}

export function useField<T extends object, K extends keyof T>(
  name: K,
): InputFieldHandler<T[K]> {
  const {
    initialValue,
    value: formValue,
    computeFormState,
    onUpdate: notifyUpdate,
  } = useContext(FormContext);
  type P = typeof name;
  type V = T[P];
  const formState = computeFormState ? computeFormState(formValue.current) : {};

  const fieldValue = readField(formValue.current, String(name)) as V;
  const [currentValue, setCurrentValue] = useState<any | undefined>(fieldValue);
  const fieldState =
    readField<Partial<InputFieldState>>(formState, String(name)) ?? {};

  //compute default state
  const state = {
    disabled: fieldState.disabled ?? false,
    readonly: fieldState.readonly ?? false,
    hidden: fieldState.hidden ?? false,
    error: fieldState.error,
    elements: "elements" in fieldState ? fieldState.elements ?? [] : [],
  };

  function onChange(value: V): void {
    setCurrentValue(value);
    formValue.current = setValueDeeper(
      formValue.current,
      String(name).split("."),
      value,
    );
    if (notifyUpdate) {
      notifyUpdate(formValue.current);
    }
  }

  return {
    value: fieldValue,
    onChange,
    isDirty: currentValue !== undefined,
    state,
  };
}

/**
 * read the field of an object an support accessing it using '.'
 *
 * @param object
 * @param name
 * @returns
 */
function readField<T>(object: any, name: string): T | undefined {
  return name
    .split(".")
    .reduce((prev, current) => prev && prev[current], object);
}

function setValueDeeper(object: any, names: string[], value: any): any {
  if (names.length === 0) return value;
  const [head, ...rest] = names;
  if (object === undefined) {
    return { [head]: setValueDeeper({}, rest, value) };
  }
  return { ...object, [head]: setValueDeeper(object[head] ?? {}, rest, value) };
}

type TTT<T extends object, K extends keyof T, R> = K extends keyof T
  ? R extends T[K]
    ? number
    : never
  : never;

function impl<T extends object, K extends keyof T, R extends T[K]>(
  obj: T,
  name: K,
): T[K] {
  return obj[name];
}

interface Pepe {
  name: string;
  when: Date;
  size: number;
}
const p: Pepe = {
  name: "n",
  when: new Date(),
  size: 1,
};
const a = impl(p, "size");