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

feat(DateTimeControl): Add DateTimeControl to default renderers #102

Merged
merged 3 commits into from
Oct 22, 2024

Conversation

ashmortar
Copy link
Contributor

  • add a DateTimeControl that looks for string fields with the format of date-time with a priorty of 3.
  • expose some useful AntD DatePicker props via the uiSchema.options interface typed via updating the existing option type helper.
  • add storybook stories showing basic usage
  • add tests covering standard behavior.

package.json Outdated
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Antd uses dayjs under the hood, but in order to coerce initial values from string to dayjs format we need to import dayjs into this component to parse the string.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

this type was declared but unused inside this component.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

adding format optionally here allows us to distinguish between normal strings and date-time strings.

Copy link

codecov bot commented Oct 21, 2024

Codecov Report

Attention: Patch coverage is 93.10345% with 2 lines in your changes missing coverage. Please review.

Project coverage is 77.11%. Comparing base (a458c7a) to head (33ff832).
Report is 1 commits behind head on main.

✅ All tests successful. No failed tests found.

Files with missing lines Patch % Lines
src/controls/DateTimeControl.tsx 86.66% 0 Missing and 2 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #102      +/-   ##
==========================================
+ Coverage   74.80%   77.11%   +2.30%     
==========================================
  Files          38       40       +2     
  Lines         504      533      +29     
  Branches       96      102       +6     
==========================================
+ Hits          377      411      +34     
+ Misses         90       83       -7     
- Partials       37       39       +2     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Contributor

@NathanFarmer NathanFarmer left a comment

Choose a reason for hiding this comment

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

I tried out the storybook (sweet!!!). Couldn't find a single nit in the code. 🚀 🚀 🚀

@ashmortar ashmortar force-pushed the f/PH-1604/add-datetime-control branch from 5db15e4 to 33ff832 Compare October 21, 2024 23:14
? U extends "object" // ObjectControlOptions goes here
? unknown
: U extends "string"
? TextControlOptions
? F extends "date-time"
Copy link
Contributor

Choose a reason for hiding this comment

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

Nice

* can be found in [this section](https://ant.design/components/date-picker#datepicker)
* of the antd docs.
*/
export const allowedDateTimePropKeys: (keyof DatePickerProps)[] = [
Copy link
Contributor

Choose a reason for hiding this comment

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

If antd adds a property to DatePickerProps, this will compile just fine, but uischemas using the new properties will get rejected at runtime by isDateTimeControlOptions.

To facilitate keeping it up to date more easily, you can write a conditional type that evaluates to true if all of the values in DatePickerProps are contained in this array, and to never if some values in DatePickerProps are not contained in this array, and then write a variable assignment with that type, assigning it to true. That way if new values are added, our code won't compile =)

if (!visible) return null

const initialValue =
typeof schema.default === "string" ? dayjs(schema.default) : undefined
Copy link
Contributor

Choose a reason for hiding this comment

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

Using our own instance of dayjs isn't ideal (or even using a hardcoded timezone library since AntD lets you generate time-based ui controls with your own library) and may cause problems in any app that has a configurable timezone, but I think it's ok for us to use it here since AntD does a pretty poor job making the timezone configurable (it doesn't, and it overrides any default you set on dayjs 😕). If we wanted to support configurable timezones, we'd have to use the generatePicker function and probably accept a datetimelibraryconfig object (inside of jsonforms's config) that contains library-specific details that we could use to generate the AntD DateTime component.
ant-design/ant-design#47490 (comment)

@ashmortar ashmortar merged commit d1e58f8 into main Oct 22, 2024
4 of 5 checks passed
@ashmortar ashmortar deleted the f/PH-1604/add-datetime-control branch October 22, 2024 20:23
Copy link

🎉 This PR is included in version 1.22.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

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

Successfully merging this pull request may close these issues.

3 participants