Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

export AutoFormProps and AutoTableProps for auto/polaris #807

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

infiton
Copy link
Contributor

@infiton infiton commented Feb 26, 2025

@pistachiobaby has run into an issue when generating reference cards for AutoForm and AutoTable that the props type for these are not exported; this PR adds these as exports

I also changed the type of AutoForm props that make it clear that for actions that take record identity that either a findBy or record prop is required. I also added some more test coverage for passing a record instead of a findBy

cc @airhorns

PR Checklist

  • Important or complicated code is tested
  • Any user facing changes are documented in the Gadget-side changelog
  • Any immediate changes are slated for release in Gadget via a generated package dependency bump
  • Versions within this monorepo are matching and there's a valid upgrade path

Copy link
Contributor

@MillanWangGadget MillanWangGadget left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • 🍏 Types getting exported for the Form and Table look reasonable
  • 🟡 It feels weird that you can pass in record and defaultValues at the same time given they do similar things in slightly different ways. Perhaps the type should make these mutually exclusive.

I'm gonna come back to this with fresh eyes to test deeply selected records in deep AutoRelationshipForms.

Comment on lines 92 to 93
/** A record for this form to act on */
record?: GadgetRecord<any>;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment contradicts the above comment saying
This action doesn't run against existing records, so you can't pass a findBy option

I'm realizing now that this record prop clashes with the defaultValues prop for prepopulating the form values. Should we deprecate one of them to avoid confusion?

Also I think it'd be better to have a action input dependent type passed into the GadgetRecord. I tried doing something like that for defaultValues previously, but it breaks for relationships where the type is based on nested actions like belongsToField: {create: {.... I think that would need another API client core change. That could be considered separately tho

import { testApi as api } from "../../../apis.ts";

const AutoFormWithExistingRecord = () => {
const [{ data: widget }] = useFindOne(api.widget, "1145");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting

When this fetch has a select: {...} that does not include all of the fields in the form, the unselected fields will get nullified in the submit.

I think this is correct because the resulting record matches the state of the form. Not selecting in the fetch is basically a reset field operation

Comment on lines +5 to +7
export type { PolarisAutoFormProps as AutoFormProps } from "./PolarisAutoForm.js";
export { PolarisAutoTable as AutoTable } from "./PolarisAutoTable.js";
export type { PolarisAutoTableProps as AutoTableProps } from "./PolarisAutoTable.js";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💯

Copy link
Contributor

@MillanWangGadget MillanWangGadget left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • record prop
    • 🍏 Passing in a record without id selected makes an error
    • 🍏 defaultValues wins over record for root level fields
    • 🟡 Passing in a record with hasMany children does not load them in the form
      • I suspect that ...edges.node... not getting translated properly to the form state is the reason. I think this could be followed up though as unblocking Nick feels more imporatant

packages/react/src/auto/shadcn/unreleasedIndex.ts
^ in here, it'd be nice to add the type exports aswell for the Shadcn side so we don't gotta remember to do it later

: // eslint-disable-next-line @typescript-eslint/ban-types
{
/** This action doesn't run against existing records, so you can't pass a findBy option */
findBy?: never;
/** This action doesn't operate with a record, so you can't pass a record option */
record?: never;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this can be safely removed. When it's here, the record prop still appears in the autocomplete dropdown like this.
CleanShot 2025-02-26 at 12 34 10@2x

throw new Error("The 'findBy' prop is required for actions that operate with a record identity.");
} else if (!operatesWithRecordId && hasFindBy) {
throw new Error("The 'findBy' prop is only allowed for actions that operate with a record identity.");
if (record) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Having record and findBy at the same time should probably throw an error. When that happens, the findBy lookup is paused because record is given, and record.id will be used in the submit request regardless of what findBy is

if (record) {
if (operatesWithRecordId && !record.id) {
throw new Error("Passing a record to an action that operates with a record identity requires the record to have an id.");
} else if (!operatesWithRecordId && record.id) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a type error for having the record prop for non-id actions. Perhaps this should throw when there's a record instead of a record that has an ID

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants