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(SegmentedControl): allow for passing stricter type for options #7051

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from

Conversation

bvandercar-vt
Copy link
Contributor

Checklist

  • [N/A] Includes tests
  • Update documentation

Changes proposed in this pull request:

Allow for stricter type on SegmentedControl than just string, which reduces the need for type casting in many cases (such as onChange call).

For example, you can still have just

<SegmentedControl

as before, for general string Options, or you can have

<SegmentedControl<OptionsEnum>

to make sure that all passed option values, and the onChange, have the OptionsEnum type.

Reviewers should focus on:

Improved type casting abilities, and mainly, the reduction of having to type cast.

Screenshot

No visual changes.

@changelog-app
Copy link

changelog-app bot commented Nov 7, 2024

Generate changelog in packages/core/changelog/@unreleased

What do the change types mean?
  • feature: A new feature of the service.
  • improvement: An incremental improvement in the functionality or operation of the service.
  • fix: Remedies the incorrect behaviour of a component of the service in a backwards-compatible way.
  • break: Has the potential to break consumers of this service's API, inclusive of both Palantir services
    and external consumers of the service's API (e.g. customer-written software or integrations).
  • deprecation: Advertises the intention to remove service functionality without any change to the
    operation of the service itself.
  • manualTask: Requires the possibility of manual intervention (running a script, eyeballing configuration,
    performing database surgery, ...) at the time of upgrade for it to succeed.
  • migration: A fully automatic upgrade migration task with no engineer input required.

Note: only one type should be chosen.

How are new versions calculated?
  • ❗The break and manual task changelog types will result in a major release!
  • 🐛 The fix changelog type will result in a minor release in most cases, and a patch release version for patch branches. This behaviour is configurable in autorelease.
  • ✨ All others will result in a minor version release.

Type

  • Feature
  • Improvement
  • Fix
  • Break
  • Deprecation
  • Manual task
  • Migration

Description

feat(SegmentedControl): allow for passing stricter type for options


Generate changelog in packages/docs-app/changelog/@unreleased

What do the change types mean?
  • feature: A new feature of the service.
  • improvement: An incremental improvement in the functionality or operation of the service.
  • fix: Remedies the incorrect behaviour of a component of the service in a backwards-compatible way.
  • break: Has the potential to break consumers of this service's API, inclusive of both Palantir services
    and external consumers of the service's API (e.g. customer-written software or integrations).
  • deprecation: Advertises the intention to remove service functionality without any change to the
    operation of the service itself.
  • manualTask: Requires the possibility of manual intervention (running a script, eyeballing configuration,
    performing database surgery, ...) at the time of upgrade for it to succeed.
  • migration: A fully automatic upgrade migration task with no engineer input required.

Note: only one type should be chosen.

How are new versions calculated?
  • ❗The break and manual task changelog types will result in a major release!
  • 🐛 The fix changelog type will result in a minor release in most cases, and a patch release version for patch branches. This behaviour is configurable in autorelease.
  • ✨ All others will result in a minor version release.

Type

  • Feature
  • Improvement
  • Fix
  • Break
  • Deprecation
  • Manual task
  • Migration

Description

feat(SegmentedControl): allow for passing stricter type for options


Check the box to generate changelog(s)

  • Generate changelog entry

@@ -83,133 +83,151 @@ export interface SegmentedControlProps
small?: boolean;
}

// This allows the ability to pass a more strict type for `options`/`onValueChange`
// i.e. <SegmentedControl<Intent> />
interface ReactFCWithGeneric extends React.FC<SegmentedControlProps> {
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit this is a specific interface for SegmentedControl so lets name it something in accordance with that.

Suggested change
interface ReactFCWithGeneric extends React.FC<SegmentedControlProps> {
interface GenericSegmentedControl extends React.FC<SegmentedControlProps> {

Copy link
Contributor Author

Choose a reason for hiding this comment

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

✔️

Copy link
Contributor Author

@bvandercar-vt bvandercar-vt Nov 13, 2024

Choose a reason for hiding this comment

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

Also just as an FYI, I created this type to match the fact that all other components in this repo specify the type React.FC. But really you don't need to manually add a type at all, you could just have

export const SegmentedControl = React.forwardRef(
    <T extends string>(props: SegmentedControlProps<T>, ref: React.ForwardedRef<HTMLDivElement>) => {

So let me know if you like that better?

};
export const SizeSelect: React.FC<SizeSelectProps> = ({ label, size, optionLabels, onChange }) => (
<FormGroup label={label}>
<SegmentedControl<Size>
Copy link
Contributor

Choose a reason for hiding this comment

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

Out of curiosity, in these types of cases if you omit <Size> is the generic type parameter properly inferred from the options array? I'm guessing not because there's no as const.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm guessing not because there's no as const.

Correct, it can't be inferred from the options in that case. But it is inferred from the type of the value prop as well as the arg of the onChange prop

{ label: optionLabels[1], value: "regular" },
{ label: optionLabels[2], value: "large" },
]}
onValueChange={onChange}
Copy link
Contributor

Choose a reason for hiding this comment

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

Another point of curiosity (since we ran into this issue with another generic component recently), if you provide an arrow function here, does the type of the argument get properly inferred?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes. If you specify a type in the generic, it will make sure onChange arg has the proper type as well as the options. If you don't specify a generic, the type of the onChange arg may determine what the options have to be.

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.

2 participants