react-spectrum icon indicating copy to clipboard operation
react-spectrum copied to clipboard

Add TypeScript Examples

Open matamatanot opened this issue 6 years ago • 19 comments

🙋 Documentation Request

I'd like examples of TypeScript. Please consider adding.

For examples, React Hook Form api docs offer JS and TS examples. You can switch the displayed code snippets with a button.

🧢 Your Company/Team

matamatanot avatar Jul 30 '20 10:07 matamatanot

@devongovett Do you have any thoughts? If we implement a mechanism to display TS examples in the same way as the React Hook Form, Many users can contribute.

Of course I would like to contribute to the addition of examples.

matamatanot avatar Aug 05 '20 07:08 matamatanot

Hey @matamatanot. I think this is something we would be happy to have in the docs but the main question would be how would it work? I think it would require some investigation to determine if it is possible with our current documentation builder/website. If you had any thoughts on this, we can discuss the options here. If it ends up proving to be a challenge, perhaps an RFC would be needed.

dannify avatar Aug 05 '20 23:08 dannify

Has there been any update on having TS examples?

aidan-keay avatar May 30 '21 19:05 aidan-keay

This is not something the core team is able to take on any time soon. Happy to discuss contributions!

dannify avatar May 31 '21 00:05 dannify

This is not something the core team is able to take on any time soon. Happy to discuss contributions!

I'd be happy to contribute the ts examples although how easy would it be to add to the existing documentation?

aidan-keay avatar May 31 '21 10:05 aidan-keay

I think the biggest hurdle to start with will be setting up the docs site to display and build ts examples. We have not done any investigations into this yet so I am not too sure how easy it will be.

dannify avatar Jun 10 '21 23:06 dannify

How about, while we wait to know whether or not it's feasible in the site, to add some examples in Typescript to this repo so at least people looking for Typescript examples can find something. This would simplify the process.

alveshelio avatar Jun 11 '21 10:06 alveshelio

Would Love to contribute some examples, I don't want to have to figure out how to integrate with your docs site however. I love typescript and react-aria however i have to say the current DX trying to get them to play nice together has been woeful. Would love to see the adobe team provide an outlet for TS dev looking to contribute TS examples

xxKeefer avatar Nov 06 '22 07:11 xxKeefer

I've been working on the setup for this in #3710. That will allow us to run TSC over the examples in the docs, which are in MDX files. I've only fixed the existing errors in that PR, so once it is merged, we'd need to go through each example and add type annotations where appropriate. We'd love help with that if people are looking to contribute!

devongovett avatar Nov 07 '22 02:11 devongovett

Until types in the examples are ready I have a really hard time to wrap my head around the usage of props in the existing examples. Is there some guidance on how to read the existing examples?

Example:

function CheckboxGroup(props) {
  let { children, label, description, errorMessage, validationState } = props;
  let state = useCheckboxGroupState(props);
  let { groupProps, labelProps, descriptionProps, errorMessageProps } =
    useCheckboxGroup(props, state);

What is "props' actually? I can see it has { children, label, description, errorMessage, validationState }, but shall this be read as the "complete" list of props that are supposed to be passed to CheckboxGroup?

donaldpipowitch avatar Feb 16 '23 20:02 donaldpipowitch

@donaldpipowitch For this example, the "complete" list can be found in type AriaCheckboxGroupProps type. The type of props for your CheckboxGroup component is up to you and specific to what kind of API you'd like your component to have. You may want to add or remove options.

One direction you could go is to extend AriaCheckboxGroupProps, and include any additional props specific to your implementation.

Example:

import {AriaCheckboxGroupProps} from 'react-aria';

export interface CheckboxGroupProps extends AriaCheckboxGroupProps {
  /* any custom props you'd like to add */
}

function CheckboxGroup(props: CheckboxGroupProps)
...

reidbarber avatar Feb 23 '23 18:02 reidbarber

@reidbarber AriaCheckboxGroupProps is used by useCheckboxGroup, but there is also useCheckboxGroupState that gets the whole props object which uses CheckboxGroupProps. Is CheckboxGroupProps extending AriaCheckboxGroupProps? Or the other way around? Or should my interface do export interface CheckboxGroupProps extends AriaCheckboxGroupProps, CheckboxGroupProps?

donaldpipowitch avatar Feb 24 '23 21:02 donaldpipowitch

@donaldpipowitch Sorry, should have picked a better name for that interface, since CheckboxGroupProps already exists in our types.

export interface MyCheckboxGroupProps extends AriaCheckboxGroupProps {
  /* any custom props you'd like to add */
}

No need to extend CheckboxGroupProps from react-aria since AriaCheckboxGroupProps already extends it.

I threw some types on the example from the docs so hopefully it's a little more clear: https://codesandbox.io/s/checkbox-example-7yh5df?file=/src/App.tsx

reidbarber avatar Feb 24 '23 22:02 reidbarber

Thank you very much @reidbarber . Is this the general rule that the "State" hooks extend from the "Aria" hooks?

donaldpipowitch avatar Feb 25 '23 08:02 donaldpipowitch

Typically it is the other way around. The stately hooks usually make use of a "base" prop definition and the props for the aria hook extend from that "base", adding on aria labeling props/DOM attribute props/etc. For the component props themselves, we extend from the aria props and add style props/props to control Spectrum Design specific behavior/appearance to the type definition.

LFDanLu avatar Feb 27 '23 17:02 LFDanLu

Setup work for type checking the examples is now merged! Some of the examples have been converted, but there are still lots of annotations to add. If anyone wants to help with this, we'd really appreciate PRs!

devongovett avatar Mar 02 '23 23:03 devongovett

Sounds great. Do you have an example MR to show which annotations you are looking for?

donaldpipowitch avatar Mar 03 '23 06:03 donaldpipowitch

For example, the useListBox examples have annotations for props going into the component. We need to expand this to all examples in the docs so that eventually we can turn on noImplicitAny in the tsconfig.

btw, if anyone is interested in helping, you can test your changes by running make check-examples to run TSC over the docs.

devongovett avatar Mar 07 '23 21:03 devongovett

I think we can close this. All of the examples in the new docs website are written in TypeScript.

devongovett avatar Dec 19 '25 17:12 devongovett