This repository has been archived by the owner on Nov 28, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: creating a custom DateTimeWidget for datetime-local inputs (#895)
* fix: creating a custom DateTimeWidget for datetime-local inputs * docs: removing an outdated comment * test: updating some broken tests
- Loading branch information
Showing
4 changed files
with
52 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
packages/api-explorer/src/form-components/DateTimeWidget.jsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
const React = require('react'); | ||
const PropTypes = require('prop-types'); | ||
const extensions = require('@readme/oas-extensions'); | ||
|
||
function createDateTimeWidget(oas) { | ||
const explorerEnabled = oas[extensions.EXPLORER_ENABLED]; | ||
|
||
// Return a function that renders null when the explorer is disabled | ||
if (!explorerEnabled) return () => null; | ||
|
||
// Why use this instead of the DateTimeWidget that RJSF provides? Well that's running UTC conversion on dates that | ||
// doesn't actually need to happen for browsers that support `datetime-local`, and when it does that in browsers that | ||
// don't support it, it's overwriting the data that the user inputs with an empty string. | ||
function DateTimeWidget(props) { | ||
const { | ||
registry: { | ||
widgets: { BaseInput }, | ||
}, | ||
} = props; | ||
|
||
return <BaseInput type="datetime-local" {...props} />; | ||
} | ||
|
||
DateTimeWidget.propTypes = { | ||
registry: PropTypes.shape({ | ||
FieldTemplate: PropTypes.func, | ||
rootSchema: PropTypes.object, | ||
widgets: PropTypes.object, | ||
}).isRequired, | ||
}; | ||
|
||
return DateTimeWidget; | ||
} | ||
|
||
module.exports = createDateTimeWidget; |