Skip to content

Commit

Permalink
fix(refAtom): make the same type as fieldAtom
Browse files Browse the repository at this point in the history
  • Loading branch information
MiroslavPetrik committed Mar 7, 2024
1 parent 44a6227 commit f7dac70
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 44 deletions.
81 changes: 45 additions & 36 deletions src/atoms/list-atom/listAtom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { atom } from "jotai";
import { RESET, atomWithDefault, atomWithReset, splitAtom } from "jotai/utils";

import { type ListItemForm, listItemForm } from "./listItemForm";
import { type ExtendFieldAtom } from "../types";
import type { FieldAtomState } from "../types";

export type ListItem<Fields extends FormFields> = PrimitiveAtom<
ListItemForm<Fields>
Expand All @@ -35,39 +35,42 @@ type SplitAtomAction<Item> =
before?: PrimitiveAtom<Item>;
};

export type ListAtom<Fields extends FormFields, Value> = ExtendFieldAtom<
Value[],
{
/**
* An atom indicating whether the list is empty.
*/
empty: Atom<boolean>;
/**
* A splitAtom() instance from jotai/utils.
* It handles adding, removing and moving of items in the list.
* @internal
*/
_splitList: WritableAtom<
PrimitiveAtom<ListItemForm<Fields>>[],
[SplitAtomAction<ListItemForm<Fields>>],
void
>;
/**
* An atom holding the list of forms of each item.
* @internal
*/
_formList: WritableAtom<
ListItemForm<Fields>[],
[typeof RESET | SetStateAction<ListItemForm<Fields>[]>],
void
>;
/**
* An atom holding the fields of the internal formAtom of each item.
* @internal
*/
_formFields: Atom<Fields[]>;
buildItem(fields?: Fields): ListItemForm<Fields>;
}
type ListAtomState<Fields extends FormFields, Value> = FieldAtomState<
Value[]
> & {
/**
* An atom indicating whether the list is empty.
*/
empty: Atom<boolean>;
/**
* A splitAtom() instance from jotai/utils.
* It handles adding, removing and moving of items in the list.
* @internal
*/
_splitList: WritableAtom<
PrimitiveAtom<ListItemForm<Fields>>[],
[SplitAtomAction<ListItemForm<Fields>>],
void
>;
/**
* An atom holding the list of forms of each item.
* @internal
*/
_formList: WritableAtom<
ListItemForm<Fields>[],
[typeof RESET | SetStateAction<ListItemForm<Fields>[]>],
void
>;
/**
* An atom holding the fields of the internal formAtom of each item.
* @internal
*/
_formFields: Atom<Fields[]>;
buildItem(fields?: Fields): ListItemForm<Fields>;
};

export type ListAtom<Fields extends FormFields, Value> = Atom<
ListAtomState<Fields, Value>
>;

export type ListAtomConfig<Fields extends FormFields, Value> = {
Expand Down Expand Up @@ -207,7 +210,14 @@ export function listAtom<
) as PrimitiveAtom<string[]>;
const validateCountAtom = atom(0);
const validateResultAtom = atom<ValidateStatus>("valid");
const refAtom = atom<HTMLFieldSetElement | null>(null);

/**
* The ref is practicaly not usable for list, but required to match the fieldAtom signature.
* @internal
*/
const refAtom = atom<
HTMLInputElement | HTMLTextAreaElement | HTMLSelectElement | null
>(null);
const emptyAtom = atom((get) => get(_formListAtom).length === 0);
const valueAtom = atom(
(get) => {
Expand Down Expand Up @@ -374,7 +384,6 @@ export function listAtom<
_itemErrorsAtom.debugPrivate = true;
}

// @ts-expect-error ref with HTMLFieldset is ok
return self;
}

Expand Down
10 changes: 4 additions & 6 deletions src/atoms/list-atom/listItemForm.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import { FormAtom, FormFields, RESET, formAtom, walkFields } from "form-atoms";
import { Atom, Getter, SetStateAction, WritableAtom, atom } from "jotai";
import { type FormFields, RESET, formAtom, walkFields } from "form-atoms";
import type { Atom, Getter, SetStateAction, WritableAtom } from "jotai";
import { atom } from "jotai";
import { atomEffect } from "jotai-effect";

import { extendAtom } from "../extendAtom";
import { PrimitiveFormAtom } from "../types";

type FormAtomState<Fields extends FormFields> =
FormAtom<Fields> extends Atom<infer State> ? State : never;
import type { FormAtomState, PrimitiveFormAtom } from "../types";

type NamedFormAtomState<Fields extends FormFields> = FormAtomState<Fields> & {
nameAtom: Atom<string>;
Expand Down
10 changes: 8 additions & 2 deletions src/atoms/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import { FieldAtom, FormAtom, FormFields } from "form-atoms";
import { Atom, PrimitiveAtom } from "jotai";
import type { FieldAtom, FormAtom, FormFields } from "form-atoms";
import type { Atom, PrimitiveAtom } from "jotai";

export type FormAtomState<Fields extends FormFields> =
FormAtom<Fields> extends Atom<infer State> ? State : never;

export type FieldAtomState<Value> =
FieldAtom<Value> extends Atom<infer State> ? State : never;

export type ExtendFieldAtom<Value, State> =
FieldAtom<Value> extends Atom<infer DefaultState>
Expand Down

0 comments on commit f7dac70

Please sign in to comment.