-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: infer field value from field schema
BREAKING CHANGE: renamed select&multiselect field as those are not 'real'. For multiselect there is arrayField and the selectField is regular stringField. selectFieldHook will be made generic to support boolean, number, string, date (all primitive values) selection from list of options.
- Loading branch information
1 parent
f54871e
commit 378d1b5
Showing
31 changed files
with
178 additions
and
138 deletions.
There are no files selected for viewing
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { ZodArray, z } from "zod"; | ||
|
||
import { ValidatedFieldAtomConfig, validatedFieldAtom } from ".."; | ||
import { ZodParams, defaultParams } from "../zodParams"; | ||
|
||
export const arrayField = <ElementSchema extends z.Schema>({ | ||
required_error = defaultParams.required_error, | ||
elementSchema, | ||
...config | ||
}: { elementSchema: ElementSchema } & Partial< | ||
ValidatedFieldAtomConfig< | ||
ZodArray<ElementSchema, "atleastone">, | ||
ZodArray<ElementSchema, "many"> | ||
> | ||
> & | ||
ZodParams) => | ||
validatedFieldAtom({ | ||
value: [], | ||
schema: z.array(elementSchema).nonempty(required_error), | ||
optionalSchema: z.array(elementSchema), | ||
...config, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from "./arrayField"; | ||
export * from "./useArrayFieldProps"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { ExtractAtomValue } from "jotai"; | ||
import { z } from "zod"; | ||
|
||
import { arrayField } from "./arrayField"; | ||
import { ZodParams } from "../zodParams"; | ||
|
||
export const stringArrayField = ( | ||
params: ZodParams & { | ||
value?: string[]; | ||
optional?: boolean; | ||
name?: string; | ||
} = {} | ||
) => arrayField({ elementSchema: z.string(), ...params }); | ||
|
||
export type StringArrayField = ReturnType<typeof stringArrayField>; | ||
|
||
export type StringArrayFieldValue = ExtractAtomValue< | ||
ExtractAtomValue<StringArrayField>["value"] | ||
>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
export * from "./boolean-field"; | ||
export * from "./checkbox-field"; | ||
export * from "./file-field"; | ||
export * from "./multi-select-field"; | ||
export * from "./array-field"; | ||
export * from "./number-field"; | ||
export * from "./select-field"; | ||
export * from "./string-field"; | ||
export * from "./text-field"; | ||
export * from "./validatedFieldAtom"; |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from "./stringField"; | ||
export * from "./useSelectFieldProps"; |
7 changes: 5 additions & 2 deletions
7
...elds/select-field/selectField.stories.tsx → ...elds/string-field/selectField.stories.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { ExtractAtomValue } from "jotai"; | ||
import { ZodString, z } from "zod"; | ||
|
||
import { ValidatedFieldAtomConfig, validatedFieldAtom } from ".."; | ||
import { ZodParams, defaultParams } from "../zodParams"; | ||
|
||
export type StringFieldAtom = ReturnType<typeof stringField>; | ||
|
||
export type StringFieldValue = ExtractAtomValue< | ||
ExtractAtomValue<StringFieldAtom>["value"] | ||
>; | ||
|
||
export const stringField = ({ | ||
required_error = defaultParams.required_error, | ||
...config | ||
}: Partial<ValidatedFieldAtomConfig<ZodString>> & ZodParams = {}) => | ||
validatedFieldAtom({ | ||
value: undefined, | ||
schema: z.string({ required_error }), | ||
...config, | ||
}); |
5 changes: 3 additions & 2 deletions
5
...elds/select-field/useSelectFieldProps.tsx → ...elds/string-field/useSelectFieldProps.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.