Skip to content

Commit

Permalink
make FieldChangeEvent more flexible
Browse files Browse the repository at this point in the history
  • Loading branch information
George-Payne committed Jan 15, 2025
1 parent 89c40b7 commit b0f2624
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
26 changes: 26 additions & 0 deletions .changeset/hungry-spies-sniff.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
---
'@kurrent-ui/fields': minor
---

`FieldChangeEvent` is now more flexible.

Previously, it only took the complete form type, and a key to the form:

```ts
interface MyForm {
something: number;
another: string;
}

const handleSomethingChange = (e: FieldChangeEvent<MyForm, 'something'>) => {
// e.details is now `{ name: "something", value: number }`
};
```

Now, you can also pass a typing as a single value, so it is easier to use outside of a validatedForm context:

```ts
const handleSomethingChange = (e: FieldChangeEvent<number>) => {
// e.details is now `{ name: string, value: number }`
};
```
9 changes: 6 additions & 3 deletions packages/fields/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,11 @@ export interface FieldChange<T, N = string> {
value: T;
}

export type FieldChangeEvent<T, N extends keyof T> = CustomEvent<
FieldChange<T[N], N>
>;
export type FieldChangeEvent<
T,
N extends keyof T | void = void,
> = N extends keyof T
? CustomEvent<FieldChange<T[N], N>>
: CustomEvent<FieldChange<T>>;

export type Templated = boolean | 'no-edit';

0 comments on commit b0f2624

Please sign in to comment.