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

Support enums when scaffolding models to display values and generate form inputs #4395

Merged
merged 26 commits into from Feb 9, 2022

Conversation

dthyresson
Copy link
Contributor

@dthyresson dthyresson commented Feb 6, 2022

Fixes: #4384
Fixes #4383

Enums are a defined set values or can be a list of defined set values allowed a model's field.

For example, a Spot model can be:

model Spot {
  id          String       @id @default(uuid())
  createdAt   DateTime     @default(now())
  updatedAt   DateTime     @updatedAt
  place       Place        @relation(fields: [placeId], references: [id])
  placeId     String
  name        String
  spotType    SpotType
  colors      COLOR[]
  shape       SHAPE
  size        SIZE
  material    MATERIAL
  possessions Possession[]
  codes       Code[]
}

enum SpotType {
  BAG
  BOX
  CASE
  CONTAINER
  DRAWER
}

enum COLOR {
  AMBER
  BLACK
  BLUE
  BROWN
  FUSCHIA
  GRAY
  GREEN
  ORANGE
  PINK
  PURPLE
  RED
  SKY
  TEAL
  TRANSPARENT
  WHITE
  YELLOW
}

enum MATERIAL {
  CARDBOARD
  PLASTIC
  WOOD
  METAL
  PAPER
  STYROFOAM
}

enum SHAPE {
  RECTANGULAR
  ROUND
  SPHERICAL
  SQUARE
  TRIANGULAR
}

enum SIZE {
  X_SMALL
  SMALL
  MEDIUM
  LARGE
  X_LARGE
  XX_LARGE
}

This means that a Spot needs to have a material, size, shape and type defined in those enums. It can also be set with multiple colors.

This PR displays enums in scaffold components, including forms to save the values.

Single enums are rendered as RadioFields and multiple lots of enums and CheckboxFields.

image

And and lists are displayed in words:

image

Ideally, the generated components would have cleaner markup, but I didn't want to create a lint option after scaffolding.

Note: Cannot write tests to cover enums because [SQLite does not support them](https://www.prisma.io/docs/reference/api-reference/prisma-schema-reference#enum.

@dthyresson dthyresson requested a review from Tobbe February 6, 2022 13:57
@dthyresson
Copy link
Contributor Author

Per @cannikin improve the text and check label positioning (it's not centered).

@dthyresson
Copy link
Contributor Author

Also, I am using an ES2020 Intl method

Property 'ListFormat' does not exist on type 'typeof Intl'.

image

I could addd ES2020 to TS but do we think @Tobbe that I should avoid this for browser compatibility?

@dthyresson
Copy link
Contributor Author

I could simply join with ',' and not make a sentence

@Tobbe
Copy link
Member

Tobbe commented Feb 7, 2022

I could addd ES2020 to TS but do we think @Tobbe that I should avoid this for browser compatibility?

I thought about that when you first mentioned using ListFormat. We use a lot of the Intl stuff at work, but have to pollyfill the crap out of almost everything :(

Not sure what minimum browser version a default RW app needs to support. Do we have any official guidelines on that?

@dthyresson
Copy link
Contributor Author

dthyresson commented Feb 7, 2022

Better align using flex.

Before:

After

image

@dthyresson dthyresson force-pushed the dt-scaffold-enums-as-checkboxes branch from ffee0c2 to f9f190d Compare February 7, 2022 19:06
@dthyresson
Copy link
Contributor Author

For handling the enum is null to reset, optional lists are not supported:

Screen Shot 2022-02-07 at 10 52 24 PM

So, an "none" case for an option single enum is needed. But, 9 times out of 10 I'd never have an optional enum, but have one of the options be an empty case like NONE, or N_A. That way if you were to aggregate or count by enum you would never have to deal with nulls -- and counting nulls.

@dthyresson
Copy link
Contributor Author

Now supports an option single enum.

model Spot {
  id          String       @id @default(uuid())
  createdAt   DateTime     @default(now())
  updatedAt   DateTime     @updatedAt
  place       Place        @relation(fields: [placeId], references: [id])
  placeId     String
  name        String
  spotType    SPOT_TYPE?
  colors      COLOR[]
  shape       SHAPE
  size        SIZE
  material    MATERIAL
  possessions Possession[]
  codes       Code[]
}

enum SPOT_TYPE {
  BAG
  BOX
  CASE
  CONTAINER
  DRAWER
}

here the spotyType is an optional SPOT_TYPE.

The form now adds a "None" radio option and sets to null on save thus unsetting the value to null.

image

Now set ...

image

and un-set to None ...

image

and can see is not set

image

Coercing is done in the onSubmit maybe can be done elsewhere?

errorClassName="rw-input rw-input-error"
/>
<div>
None
Copy link
Member

Choose a reason for hiding this comment

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

What do you think about adding some style to this to make it italic? That'd make None stand out a bit to show that it's a special value

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not sure I love this:

image

Copy link
Member

Choose a reason for hiding this comment

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

Sure. Any other ideas? What about explaining it some more? "* None (will not set this value in the database)". Ideally something a bit shorter than that though 😅

Copy link
Member

@Tobbe Tobbe Feb 8, 2022

Choose a reason for hiding this comment

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

Maybe just "* Not set" is enough? Could also play around with text color to signify that it's a special value, like make it a bit muted maybe

@dthyresson
Copy link
Contributor Author

I kept the italics. Let's use that and get feedback from team and community for now.

@Tobbe Any items left to do?

@cannikin
Copy link
Member

cannikin commented Feb 8, 2022

I like a subtle gray for my "empty" or "none" entries!

@dthyresson dthyresson requested a review from Tobbe February 9, 2022 02:07
@dthyresson
Copy link
Contributor Author

Put the b subtle

image

@Tobbe
Copy link
Member

Tobbe commented Feb 9, 2022

I kept the italics. Let's use that and get feedback from team and community for now.

@Tobbe Any items left to do?

I like your approach of getting it in, and then getting feedback. Such an easy thing to iterate on too!

This is good to go now!

I see you switched to gray for None. I like it as long as people don't mistake it for being disabled, so they don't even reallize they can click it 😬 . But, as said, let's do it, and see if we get any feedback!

@dthyresson dthyresson merged commit 58becc4 into redwoodjs:main Feb 9, 2022
@dthyresson dthyresson deleted the dt-scaffold-enums-as-checkboxes branch February 9, 2022 19:23
@jtoar jtoar added this to the next-release milestone Feb 9, 2022
@thedavidprice
Copy link
Contributor

Well done. Looks great 🚀

@thedavidprice thedavidprice modified the milestones: next-release, v0.45.0 Feb 11, 2022
dac09 added a commit to dac09/redwood that referenced this pull request Feb 14, 2022
…bundle

* 'main' of github.com:redwoodjs/redwood: (324 commits)
  (render.com deployment) resolve deploy render exceeding memory on free tier (redwoodjs#4433)
  Update dependency @clerk/types to v1.25.0 (redwoodjs#4419)
  Update dependency esbuild to v0.14.21 (redwoodjs#4427)
  Update dependency @types/prettier to v2.4.4 (redwoodjs#4426)
  Update dependency @envelop/depth-limit to v1.2.3 (redwoodjs#4428)
  Support enums when scaffolding modes to display values and generate form inputs (redwoodjs#4395)
  Update dependency @clerk/clerk-js to v2.13.1 (redwoodjs#4418)
  Update jest monorepo to v27.5.1 (redwoodjs#4421)
  Update dependency @types/md5 to v2.3.2 (redwoodjs#4420)
  Polish release script (redwoodjs#4417)
  Update dependency @azure/msal-browser to v2.22.0 (redwoodjs#4414)
  Update dependency fastify-http-proxy to v6.2.2 (redwoodjs#4413)
  Roll back dependency @magic-sdk/admin to 1.3.4 (redwoodjs#4409)
  update yarn.lock
  Update typescript-eslint monorepo to v5.11.0 (redwoodjs#4412)
  v0.44.1
  Downgrade Prisma to 3.8.1 (redwoodjs#4411)
  Downgrade Prisma to 3.8.1 (redwoodjs#4411)
  update yarn.lock
  fix(generators): Generate types after gen-ing files (redwoodjs#4376)
  ...
@dthyresson dthyresson changed the title Support enums when scaffolding modes to display values and generate form inputs Support enums when scaffolding models to display values and generate form inputs Jul 20, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
release:feature This PR introduces a new feature topic/generators-&-scaffolds
Projects
No open projects
Status: Archived
Status: Done
5 participants