Version that supports react-hook-form #49
mihirverma7781
started this conversation in
Ideas
Replies: 2 comments
-
|
for tanstack/forms multi-select-field.tsx import {MultiSelect} from "@repo/ui/components/sumo/multi-select";
import type { ReactNode } from "react";
import { BaseField, type FieldControlProps } from "./base-field";
import { useFieldContext } from "./context";
type MultiSelectFieldProps = Omit<React.ComponentProps<typeof MultiSelect>, 'onValueChange'| "children"> & FieldControlProps;
export function MultiSelectField({
label,
description,
options,
...props
}: MultiSelectFieldProps) {
const field = useFieldContext<string[]>();
const isInvalid = field.state.meta.isTouched && !field.state.meta.isValid;
return (
<BaseField description={description} label={label}>
<MultiSelect
aria-invalid={isInvalid}
id={field.name}
name={field.name}
options={options}
onValueChange={(value) => field.handleChange(value)}
value={field.state.value}
{...props}
/>
</BaseField>
);
}Usage <form.AppField name="frameworks">
{(field) => (
<field.MultiSelectField
animationConfig={{
badgeAnimation: "bounce",
popoverAnimation: "scale",
}}
defaultValue={field.state.value}
label="Frameworks"
options={frameworksList}
placeholder="Choose frameworks..."
variant="inverted"
/>
)}
</form.AppField>
<form.AppField name="tags">
{(field) => (
<field.MultiSelectField
animationConfig={{
badgeAnimation: "pulse",
popoverAnimation: "fade",
}}
defaultValue={field.state.value}
label="Tags"
options={techStackOptions}
placeholder="Choose tags..."
variant="secondary"
/>
)}
</form.AppField> |
Beta Was this translation helpful? Give feedback.
0 replies
-
|
base-field.tsx import {
Field,
FieldContent,
FieldDescription,
FieldError,
FieldLabel,
} from "@repo/ui/components/shadcn/field";
import type { ReactNode } from "react";
import { useFieldContext } from "./context";
export type FieldControlProps = {
label: string;
description?: string;
};
type BaseFieldProps = FieldControlProps & {
children: ReactNode;
orientation?: React.ComponentPropsWithoutRef<typeof Field>["orientation"];
controlFirst?: boolean;
};
export function BaseField({
children,
label,
description,
controlFirst,
orientation,
}: BaseFieldProps) {
const field = useFieldContext();
const isInvalid = field.state.meta.isTouched && !field.state.meta.isValid;
const labelElement = (
<>
<FieldLabel htmlFor={field.name}>{label}</FieldLabel>
{description && <FieldDescription>{description}</FieldDescription>}
</>
);
const errorElem = isInvalid && (
<FieldError errors={field.state.meta.errors} />
);
return (
<Field data-invalid={isInvalid} orientation={orientation}>
{controlFirst ? (
<>
{children}
<FieldContent>
{labelElement}
{errorElem}
</FieldContent>
</>
) : (
<>
<FieldContent>{labelElement}</FieldContent>
{children}
{errorElem}
</>
)}
</Field>
);
} |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Here is a version that can make use of react-hook-form:
multi-select.jsx
Usage:
Beta Was this translation helpful? Give feedback.
All reactions