Skip to content

Commit

Permalink
fix: required textField with min 1 length
Browse files Browse the repository at this point in the history
  • Loading branch information
MiroslavPetrik committed Feb 27, 2023
1 parent d599292 commit eda10d6
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 16 deletions.
7 changes: 0 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,6 @@ Most UI libraries provide styled primitive `<Input>` components, form `<Label>`
| [`textField()`](#textfield) | A fieldAtom to capture `string` values. |
| [`selectField()`](#selectfield) | A fieldAtom to capture enum values. |

| Headless Component | About |
| ---------------------------------------------------- | -------------------------------------------------------------- |
| [`<FieldLabel />`](#fieldlabel-) | A headless component with accessible label. |
| [`<RequirementIndicator />`](#requirementindicator-) | Displays an indicator whether a field is required or optional. |
| [`<ArrayField />`](#arrayfield-) | A headless component to control `FieldAtom<>[]`. |
| [`<RadioControl />`](#radiocontrol-) | A headless component to control multiple `FieldAtom<boolean>`. |

| Hooks | About |
| ------------------------------------------------------- | ----------------------------- |
| [`useClearFileFieldEffect()`](#useclearfilefieldeffect) | A hook to control file input. |
Expand Down
5 changes: 3 additions & 2 deletions src/Intro.contents.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
| --------------------------------------------------------- | ----------------------------------------------------------- |
| [checkboxField()](?path=/docs/fields-checkboxfield--docs) | A true boolean 2-state checkbox field. |
| [booleanField()](?path=/docs/fields-booleanfield--docs) | 3-state checkbox with `true`, `false` or `undefined` value. |
| [fileField()](?path=/docs/fields-filefield--docs) | A field to hold `FileList` value. |
| [numberField()](?path=/docs/fields-numberfield--docs) | A field to a `number` type. |
| [fileField()](?path=/docs/fields-filefield--docs) | A field to hold a `FileList` value. |
| [numberField()](?path=/docs/fields-numberfield--docs) | A field to hold a `number` type. |
| [textField()](?path=/docs/fields-textfield--docs) | A field to hold a `string` type. |

### Components

Expand Down
21 changes: 21 additions & 0 deletions src/fields/text-field/Docs.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { Meta, Markdown } from "@storybook/blocks";

import Config from "./config.md?raw";

<Meta title="fields/textField" />

# `textField(): ValidatedFieldAtom<string>`

```ts
import { textField } from "@form-atoms/field";

// required string, must be filled to submit
const userName = textField();

// optional string, can be empty to submit
const bio = textField({ optional: true });
```

## Initial Config

<Markdown>{Config}</Markdown>
5 changes: 5 additions & 0 deletions src/fields/text-field/config.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
| Param | Value |
| ---------------- | -------------------------- |
| `value` | `""` |
| `schema` | `z.string().trim().min(1)` |
| `optionalSchema` | `z.string().trim()` |
18 changes: 11 additions & 7 deletions src/fields/text-field/textField.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,20 @@ import {
ValidatedFieldAtomConfig,
validatedFieldAtom,
} from "..";
import { ZodParams, defaultParams } from "../zodParams";

export type TextValue = string | undefined;
export type TextFieldValue = string;

export type TextFieldAtom = ValidatedFieldAtom<TextValue>;
export type TextFieldAtom = ValidatedFieldAtom<TextFieldValue>;

export const textField = (
config: Partial<ValidatedFieldAtomConfig<TextValue>> = {}
) =>
export const textField = ({
required_error = defaultParams.required_error,
...config
}: Partial<ValidatedFieldAtomConfig<TextFieldValue>> & ZodParams = {}) =>
validatedFieldAtom({
value: undefined,
schema: z.string({ required_error: "This field is required" }),
value: "",
// https://github.com/colinhacks/zod/issues/63
schema: z.string().trim().min(1, required_error),
optionalSchema: z.string().trim(),
...config,
});

0 comments on commit eda10d6

Please sign in to comment.