-
Notifications
You must be signed in to change notification settings - Fork 130
feat(types): improve generic types in specs, and spec prop types #1421
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
Merged
Conversation
This file contains hidden or 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
9 tasks
Collaborator
Author
|
@elasticmachine merge upstream |
Collaborator
Author
|
@markov00 can you review this one please 🙏🏼 |
9 tasks
rshen91
reviewed
Dec 15, 2021
rshen91
approved these changes
Dec 15, 2021
nickofthyme
pushed a commit
that referenced
this pull request
Jan 5, 2022
# [42.0.0](v41.0.1...v42.0.0) (2022-01-05) ### Bug Fixes * **flamegraph:** solve animation regression occurring with 6db2677 ([#1541](#1541)) ([5ec6037](5ec6037)), closes [#1540](#1540) * **heatmap:** render empty state ([#1532](#1532)) ([59002df](59002df)) * **waffle:** fix strange 0 text in legend item extra when label is 0 ([#1538](#1538)) ([72224b9](72224b9)) ### Features * **goal:** add valueFormatter for tooltip ([#1529](#1529)) ([8139973](8139973)) * **heatmap:** add axis titles ([#1503](#1503)) ([a87325d](a87325d)) * **types:** improve generic types in specs, and spec prop types ([#1421](#1421)) ([562929e](562929e)) ### BREAKING CHANGES * **types:** The `xAccessor` and `yAccessor` are now required on all xy chart specs. Stronger typing on `data` prop that may cause type errors when using untyped array (i.e. `const arr: never[] = []`). Other minor type changes related to spec types. * **heatmap:** The heatmap yAxisLabel.padding style type is changed from Pixel | Partial<Padding> to Pixels | Padding. The heatmap axis labels are now correctly subjected to padding calculations and it will result in a slightly different position of labels. Co-authored-by: Marco Vettorello <[email protected]>
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
:all
Applies to all chart types
API
Changes the external API types
enhancement
New feature or request
:specs
Chart specifications related issue
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
Adds improved generic type for
DatuminBarSeries,LineSeries,AreaSeries,BubbleSeries,BarSeries,HistogramBarSeries,Partition,Heatmap,GroupByandLineAnnotationspecs.All spec components now export corresponding
___Propstypes (i.e.BarSeriesPropsfromBarSeries).BREAKING CHANGE
The
xAccessorandyAccessorprops on all relevant specs have been removed in favor of explicit accessors derived from the genericDatumtype. Stronger typing ondataprop that may cause type errors.Details
specComponentFactoryArguments
overrides- Object containing overrides extended from theSpectype. These values are replaced on theSpecand are never exposed in the public API of the components. The propschartTypeandspecTypeare required. All values listed in overrides are excluded from being used adefaults.defaults- Object containing default values extended from theSpec. Values provided will be forced as optional on theSpecregardless of the optionality of the type on the originalSpectype. These values will only be used when their respective prop on the spec component is not defined, this is in the form of a spread operator.The
specComponentFactorymethod has been refactored to automatically infer the types of the final component props given the spec type, the overridden props and the default props. An example of this is shown below.buildSFPropsArguments
overrides- SeespecComponentFactorydescriptiondefaults- SeespecComponentFactorydescriptionThis method is used to extract the implicit typings for a given spec definition outside of the
specComponentFactorymethod. But why? The way typescript allows react component props to be driven my generics requires a top level function component that defines the generic types and props that consume these generics. ThusspecComponentFactorywould not work to defined generics that are unique to eachSpec.The
BuildPropsinterface is used to define the props given theoverridesanddefaults. You will see that the final type includesoptionalsandrequiresprops but the note says it's deprecated. I did this to warn against using these as values because these are simply used to share the inferred types as we do not know ahead of time what optional or required props will be passed but we can infer what their types are.useSpecFactoryArguments
props- The fully built out props including overrides, defaults, optionals and required values.This react hook is a the magic behind the spec parser. The logic within this hook used to be called by implementing the same logic inside the factory-created spec component. This was fine but this required calling
getConnect()(MySpecFactory)everywhere it was used. At the timereact-reduxconnectwas used to connect the spec component to the redux store using thepureoption.The used of
pureoption is no longer required asreact-reduxhas since made pure the default behavior across the board, see code.With this change we can now use the
useDispatchhook in place of theconnectmethod for a simplified usage.Thus creating a spec factory component with implied generic types would look something like...
This setup, defining
buildPropsbefore the spec withunknownfor the generic, can conflict with types and you may need to define thebuildPropswithin the spec component function to access the correctDatumtype. In such case, use theuseRefhook to only call this once. Seepackages/charts/src/chart_types/heatmap/specs/heatmap.tsfor an example of this.Now the shape of the
dataprop is used as theDatumtype across theBarSeriesprops.Screen.Recording.2021-10-07.at.07.29.12.PM.mp4
The generic type for these spec components are implicit by default but can be explicitly defined as well such as...
Limitations
xAccessorandyAccessorscould be forced to be a key of thedatabut this would be overly restrictive based on current chart usage. Thus, for now this will remainnumber | stringin addition to thekeyof Datumhowever the loose typing will not show thekeyof Datumvalues in the type.generic types cannot be shared between siblings, as of my current understanding. Thus
Settingsand other props that currently useDatum as anywill remain.This does NOT allow for generic types to be used in the vast majority of the source logic because of the separation of the react components and the redux store. Thus the generics will still fallback to a datum type of
any.Issues
Related to #1380 and #547
Checklist
:xy,:partition):interactions,:axis)closes #123,fixes #123)packages/charts/src/index.ts