Skip to content

Commit

Permalink
fix: select field usage
Browse files Browse the repository at this point in the history
  • Loading branch information
MiroslavPetrik committed Mar 14, 2023
1 parent a5a21b6 commit ff6a082
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 24 deletions.
5 changes: 2 additions & 3 deletions src/select-field/SelectField.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { selectField } from "@form-atoms/field";

import { country, getLabel, getValue, options } from "./country";
import { SelectField } from "./SelectField";
import { FormStory, meta, optionalField } from "../stories";
import { stringField } from "@form-atoms/field";

export default {
title: "SelectField",
Expand All @@ -26,7 +25,7 @@ export const Required: FormStory = {
},
};

const optional = selectField({ optional: true });
const optional = stringField({ optional: true });

export const Optional: FormStory = {
...optionalField,
Expand Down
8 changes: 4 additions & 4 deletions src/select-field/SelectField.test.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { selectField } from "@form-atoms/field";
import { render, screen } from "@testing-library/react";
import { act as domAct, renderHook } from "@testing-library/react-hooks/dom";
import userEvent from "@testing-library/user-event";
Expand All @@ -8,6 +7,7 @@ import { describe, expect, it } from "vitest";
import { country, getLabel, getValue, options } from "./country";

import { SelectField } from "./";
import { stringField } from "@form-atoms/field";

describe("<SelectField />", () => {
const props = {
Expand All @@ -26,7 +26,7 @@ describe("<SelectField />", () => {
});

it("should render error message when submitting empty & required", async () => {
const field = selectField();
const field = stringField();
const form = formAtom({ field });
const { result } = renderHook(() => useFormSubmit(form));

Expand All @@ -49,7 +49,7 @@ describe("<SelectField />", () => {
});

it("should use the placeholder prop", () => {
const field = selectField();
const field = stringField();

render(
<SelectField field={field} {...props} placeholder="Pick a country" />
Expand All @@ -60,7 +60,7 @@ describe("<SelectField />", () => {

describe("with optional selectField()", () => {
it("submits with undefined", async () => {
const option = selectField({ optional: true });
const option = stringField({ optional: true });
const form = formAtom({ option });
const { result } = renderHook(() => useFormSubmit(form));

Expand Down
29 changes: 14 additions & 15 deletions src/select-field/SelectField.tsx
Original file line number Diff line number Diff line change
@@ -1,28 +1,32 @@
import {
SelectFieldProps,
useSelectFieldProps,
SelectProps,
useSelectOptions,
SelectField as SelectZodField,
} from "@form-atoms/field";
import { Select, SelectProps } from "flowbite-react";
import { Select, SelectProps as FlowbiteSelectProps } from "flowbite-react";
import { ReactNode } from "react";

import { FlowbiteField } from "../field";

export const SelectField = <Option,>({
export const SelectField = <Option, Field extends SelectZodField>({
field,
options,
getValue,
getLabel,
label,
placeholder,
label,
helperText,
required,
...uiProps
}: SelectFieldProps<Option> & SelectProps) => {
const props = useSelectFieldProps(field);
const { renderOptions, placeholderOption } = useSelectOptions(field, {
getValue,
getLabel,
}: SelectProps<Option, Field> &
FlowbiteSelectProps & { label?: ReactNode }) => {
// @ts-ignore
const props = useSelectFieldProps({ field, options, getValue });
const { selectOptions } = useSelectOptions({
field,
options,
getLabel,
placeholder,
});

Expand All @@ -35,12 +39,7 @@ export const SelectField = <Option,>({
>
{(fieldProps) => (
<Select role="combobox" {...uiProps} {...props} {...fieldProps}>
{placeholderOption}
{renderOptions.map(({ value, label }) => (
<option key={value} value={value}>
{label}
</option>
))}
{selectOptions}
</Select>
)}
</FlowbiteField>
Expand Down
4 changes: 2 additions & 2 deletions src/select-field/country.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { selectField } from "@form-atoms/field";
import { stringField } from "@form-atoms/field";

export const options = [
{ code: "SK", name: "Slovak Republic" },
Expand All @@ -10,4 +10,4 @@ type Country = { code: string; name: string };
export const getValue = (opt: Country) => opt.code;
export const getLabel = (opt: Country) => opt.name;

export const country = selectField();
export const country = stringField();

0 comments on commit ff6a082

Please sign in to comment.