@beforeyoubid/form
v2.0.1
Published
BYB form system — TanStack Form field abstractions composed from @beforeyoubid/design-system primitives
Keywords
Readme
@beforeyoubid/form
BYB's form system — TanStack Form field abstractions composed from @beforeyoubid/design-system primitives. The single source of truth for forms across the BYB platform.
What you get
Three layers:
- Standalone composed fields —
InputField,SelectField,CheckboxField, … each bundles a design-system primitive with a label, hint, error and consistent spacing. Use them anywhere, with or without TanStack Form. createBybFormHook— a thin skin over TanStack'screateFormHookthat pre-populatesfieldComponents/formComponentswith the bound versions of those fields. You supply only the contexts and the functional form config.- (Internal) the context-bound field components wired between the two.
Install
yarn add @beforeyoubid/form @beforeyoubid/design-system @tanstack/react-form@beforeyoubid/design-system (2.x), @tanstack/react-form, react, react-dom and tailwindcss (v4) are peer dependencies.
Import this package's stylesheet once, after the design system's:
@import "@beforeyoubid/design-system/globals.css";
@import "@beforeyoubid/form/styles.css";It holds no styles of its own: it registers this package's build with Tailwind (@source), because Tailwind v4 doesn't scan node_modules. Without it, classes that only these fields use are not generated.
Usage with TanStack Form
import { createFormHookContexts } from '@tanstack/react-form';
import { createBybFormHook } from '@beforeyoubid/form';
// Create the contexts once (the "functional portion" you own) and build the hook.
const { fieldContext, formContext } = createFormHookContexts();
export const { useAppForm, withForm } = createBybFormHook({ fieldContext, formContext });
function SignupForm() {
const form = useAppForm({
defaultValues: { email: '', agree: false },
onSubmit: ({ value }) => console.log(value),
});
return (
<form onSubmit={(e) => { e.preventDefault(); form.handleSubmit(); }}>
<form.AppField
name="email"
validators={{ onChange: ({ value }) => (value.includes('@') ? undefined : 'Invalid email') }}
>
{(field) => <field.InputField label="Email" type="email" />}
</form.AppField>
<form.AppField name="agree">
{(field) => <field.CheckboxField label="I agree to the terms" />}
</form.AppField>
<form.AppForm>
<form.SubmitButton>Sign up</form.SubmitButton>
</form.AppForm>
</form>
);
}The bound fields read their value and errors from the active field and call handleChange/handleBlur for you. Errors surface into the field's error slot once the field is touched.
Typing
Everything is fully typed: form-data inference (field-name autocomplete, field.state.value, validators, form.state) and the bound components on form.AppField's render prop. field.InputField autocompletes, rejects unknown/mis-typed props, and flags missing required props (e.g. SelectField without options).
This is achieved by hand-writing the hook's return type (BybFormApiFor) from TanStack's exported generic types, rather than inferring it — TanStack's inferred createFormHook return includes a recursive extendForm member that overflows the TypeScript .d.ts serializer (TS7056). extendForm is therefore the one piece of the return surface not exposed here; if you need it, call TanStack's createFormHook directly.
Standalone usage (no TanStack Form)
import { InputField, SelectField, productSelectOptions } from '@beforeyoubid/form';
<InputField label="Email" value={email} onChange={(e) => setEmail(e.target.value)} error={error} />
<InputField label="Notes" multiline rows={4} />
<SelectField label="Country" value={country} onValueChange={setCountry} options={options} />
<SelectField multiple label="Products" options={productSelectOptions()} value={products} onValueChange={setProducts} />SelectField takes category groups ({ label, options }), per-option start / meta / chip content, multiple (chips, maxDisplayed, the "N selected · CLEAR" footer) and, with clearable, an × that clears the selection. DateField is the design system's date picker as a field: DD/MM/YYYY, the lime selected day, and 24-hour time columns when you pass time.
FormFieldWrapper is exported too, for composing bespoke fields with the same label/hint/error chrome. It is built from the design system's Field, FieldLabel, FieldDescription and FieldError, so the field levers (--field-label-*, --field-error-*, …) restyle every field here.
Coming from the design system's InputField / SelectField
Design system 2.0 removed its InputField, SelectField, SelectFieldOption, SelectFieldOptionGroup and productSelectOptions. npx design-system-migrate rewrites those imports to @beforeyoubid/form; the names and props are the same. Add @beforeyoubid/form to the app and the styles.css import above. See the 2.0.0 entry in CHANGELOG.md for the details.
Fields
Text & numeric: InputField, NumberField, TextareaField, InputGroupField, PasswordField, SearchField, PhoneField, CurrencyField, ColorField, InputOTPField
Choice: SelectField, ComboboxField, MultiSelectField, RadioGroupField, RadioCardField, CheckboxField, CheckboxGroupField, SwitchField, ToggleField, ToggleGroupField
Other: SliderField, RatingField, DateField, TimeField, DateRangeField, FileUploadField
FileUploadField is the design system's FileUpload as a field. Without uploadOptions its value is File[]. With uploadOptions each file uploads to a presigned S3 URL as soon as it's picked, and the value is what the upload returned:
const presignAttachment: FileUploadPresignFn = async (file) => {
const { data } = await prepareUpload({ variables: { fileName: file.name, mimeType: file.type } })
return data!.prepareUpload.uploadUrl
}
const form = useAppForm({ defaultValues: { contract: null as string | null } })
<form.AppField name="contract">
{(field) => <field.FileUploadField label="Contract" accept=".pdf" uploadOptions={{ presign: presignAttachment }} />}
</form.AppField>The value is the file's URL (string | null; string[] with multiple), or whatever uploadOptions.onUploadFinished(file, url) returns. It only changes once an upload finishes, so a required check blocks submit while one is running. In development, the bound field warns when the form's default value isn't the shape it stores (e.g. [] for a single upload).
Plus FormFieldWrapper for composing bespoke fields with the same label/hint/error chrome. Every field works standalone and as a bound form.AppField component.
Working on the fields
Before writing or changing UI, read the design system's own docs, which match the installed version: node_modules/@beforeyoubid/design-system/docs/README.md.
Then follow CONVENTIONS.md: each field passes the props it doesn't handle through to the design system control it wraps, takes no style, and is styled with classes.
Scripts
| Command | What it does |
|---|---|
| npm run build | Production build — ESM + CJS + types via tsup |
| npm run dev | Watch-mode build |
| npm run type-check | TypeScript strict check |
| npm run lint | ESLint across src/ |
| npm test | Vitest binding tests |
| npm run verify | type-check + lint + test (the pre-publish gate) |
| npm run storybook | Storybook on port 6006 |
| npm run release | Publish to npm (runs verify + build first) |
Publishing
@beforeyoubid/form is published under the public @beforeyoubid scope (npm auth required).
npm login # one-time, must be a @beforeyoubid maintainer
npm version patch # or minor / major / 0.1.0-alpha.0
npm run release # = npm publish --access publicprepublishOnly runs verify (type-check + lint + test) and a fresh build automatically, so a publish can never ship code that doesn't pass checks. For a pre-release, use npm run release -- --tag alpha to keep it off the latest tag.
