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

Improve typings #152

Merged
merged 5 commits into from
Aug 26, 2017
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .all-contributorsrc
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,13 @@
"contributions": [
"tool"
]
},
{
"login": "thomhos",
"name": "thom",
"avatar_url": "https://avatars1.githubusercontent.com/u/11661846?v=4",
"profile": "http://thom.kr",
"contributions": []
Copy link
Contributor

Choose a reason for hiding this comment

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

Mind adding me too (if it's okay with @kentcdodds)? I had forgotten to add myself to this list. 😃

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done!

}
]
}
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ autocomplete/dropdown/select/combobox components</p>
[![version][version-badge]][package]
[![MIT License][license-badge]][LICENSE]

[![All Contributors](https://img.shields.io/badge/all_contributors-21-orange.svg?style=flat-square)](#contributors)
[![All Contributors](https://img.shields.io/badge/all_contributors-22-orange.svg?style=flat-square)](#contributors)
[![PRs Welcome][prs-badge]][prs]
[![Chat][chat-badge]][chat]
[![Code of Conduct][coc-badge]][coc]
Expand Down Expand Up @@ -493,6 +493,7 @@ Thanks goes to these people ([emoji key][emojis]):
| :---: | :---: | :---: | :---: | :---: | :---: | :---: |
| [<img src="https://avatars2.githubusercontent.com/u/15073300?v=4" width="100px;"/><br /><sub>monssef</sub>](https://github.com/rezof)<br />[💡](#example-rezof "Examples") | [<img src="https://avatars2.githubusercontent.com/u/5382443?v=4" width="100px;"/><br /><sub>Federico Zivolo</sub>](https://fezvrasta.github.io)<br />[📖](https://github.com/paypal/downshift/commits?author=FezVrasta "Documentation") | [<img src="https://avatars3.githubusercontent.com/u/746482?v=4" width="100px;"/><br /><sub>Divyendu Singh</sub>](https://divyendusingh.com)<br />[💡](#example-divyenduz "Examples") | [<img src="https://avatars1.githubusercontent.com/u/841955?v=4" width="100px;"/><br /><sub>Muhammad Salman</sub>](https://github.com/salmanmanekia)<br />[💻](https://github.com/paypal/downshift/commits?author=salmanmanekia "Code") | [<img src="https://avatars3.githubusercontent.com/u/10820159?v=4" width="100px;"/><br /><sub>João Alberto</sub>](https://twitter.com/psicotropidev)<br />[💻](https://github.com/paypal/downshift/commits?author=psicotropicos "Code") | [<img src="https://avatars0.githubusercontent.com/u/16327281?v=4" width="100px;"/><br /><sub>Bernard Lin</sub>](https://github.com/bernard-lin)<br />[💻](https://github.com/paypal/downshift/commits?author=bernard-lin "Code") [📖](https://github.com/paypal/downshift/commits?author=bernard-lin "Documentation") | [<img src="https://avatars1.githubusercontent.com/u/7330124?v=4" width="100px;"/><br /><sub>Geoff Davis</sub>](https://geoffdavis.info)<br />[💡](#example-geoffdavis92 "Examples") |
| [<img src="https://avatars0.githubusercontent.com/u/3415488?v=4" width="100px;"/><br /><sub>Anup</sub>](https://github.com/reznord)<br />[📖](https://github.com/paypal/downshift/commits?author=reznord "Documentation") | [<img src="https://avatars0.githubusercontent.com/u/340520?v=4" width="100px;"/><br /><sub>Ferdinand Salis</sub>](http://ferdinandsalis.com)<br />[🐛](https://github.com/paypal/downshift/issues?q=author%3Aferdinandsalis "Bug reports") [💻](https://github.com/paypal/downshift/commits?author=ferdinandsalis "Code") | [<img src="https://avatars2.githubusercontent.com/u/662750?v=4" width="100px;"/><br /><sub>Kye Hohenberger</sub>](https://github.com/tkh44)<br />[🐛](https://github.com/paypal/downshift/issues?q=author%3Atkh44 "Bug reports") | [<img src="https://avatars0.githubusercontent.com/u/1443499?v=4" width="100px;"/><br /><sub>Julien Goux</sub>](https://github.com/jgoux)<br />[🐛](https://github.com/paypal/downshift/issues?q=author%3Ajgoux "Bug reports") [💻](https://github.com/paypal/downshift/commits?author=jgoux "Code") [⚠️](https://github.com/paypal/downshift/commits?author=jgoux "Tests") | [<img src="https://avatars2.githubusercontent.com/u/9586897?v=4" width="100px;"/><br /><sub>Joachim Seminck</sub>](https://github.com/jseminck)<br />[💻](https://github.com/paypal/downshift/commits?author=jseminck "Code") | [<img src="https://avatars3.githubusercontent.com/u/954596?v=4" width="100px;"/><br /><sub>Jesse Harlin</sub>](http://jesseharlin.net/)<br />[🐛](https://github.com/paypal/downshift/issues?q=author%3Athe-simian "Bug reports") [💡](#example-the-simian "Examples") | [<img src="https://avatars0.githubusercontent.com/u/1402095?v=4" width="100px;"/><br /><sub>Matt Parrish</sub>](https://github.com/pbomb)<br />[🔧](#tool-pbomb "Tools") |
| [<img src="https://avatars1.githubusercontent.com/u/11661846?v=4" width="100px;"/><br /><sub>thom</sub>](http://thom.kr)<br /> |
<!-- ALL-CONTRIBUTORS-LIST:END -->

This project follows the [all-contributors][all-contributors] specification.
Expand Down
6 changes: 5 additions & 1 deletion test/basic.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export default class App extends React.Component<Props, State> {
return (
<Downshift onChange={this.onChange}>
{({
getButtonProps,
getInputProps,
getItemProps,
isOpen,
Expand All @@ -35,6 +36,9 @@ export default class App extends React.Component<Props, State> {
placeholder: 'Favorite color ?',
})}
/>
<button
{...getButtonProps()}
/>
{isOpen
? <div style={{ border: '1px solid #ccc' }}>
{items
Expand All @@ -49,7 +53,7 @@ export default class App extends React.Component<Props, State> {
)
.map((item: any, index: number) =>
<div
{...getItemProps({ item, index })}
{...getItemProps({ item, index, isSelected: selectedItem === item })}
Copy link
Member

Choose a reason for hiding this comment

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

This is going to put an attribute on the div called isSelected. I realize this is just a test, but it's probably best to make sure that the test code is correct 👍

Copy link
Contributor

Choose a reason for hiding this comment

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

Agreed, we may want to remove user-defined props from the basic test. Perhaps a secondary test for cases like this would work?

Copy link
Collaborator

Choose a reason for hiding this comment

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

isSelected is not a valid DOM element prop, so would generate a warning from React, right? Tests shouldn't generate React warnings, so if this would, I think it would be better off removed.

I'm new to downshift and trying to understand why getItemProps returns any key/value pairs it doesn't recognize. In this example, it would be easy enough to get the same behavior using this code:

                                         <div
                                              {...getItemProps({ item, index })}
                                              isSelected={selectedItem === item}

Is there a different scenario where this approach is not possible and props need to be passed through getItemProps? If not, then why return ...rest inside that function?

Copy link
Contributor

@morajabi morajabi Aug 25, 2017

Choose a reason for hiding this comment

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

I think @pbomb's question is right.

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've added a new test that shows the above with a custom component for a list item. Adding isSelected separately is also possible, but since the ...rest is in the js, I figured to have the typings reflect that as well.

key={item}
style={{
backgroundColor:
Expand Down
12 changes: 8 additions & 4 deletions typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,17 +66,21 @@ export interface GetButtonPropsOptions extends React.HTMLProps<HTMLButtonElement
selectedItem: any;
}

export interface GetItemPropsOptions {
interface OptionalExtraGetItemPropsOptions {
[key: string]: any;
}

export interface GetItemPropsOptions extends OptionalExtraGetItemPropsOptions {
index: number;
item: any;
}

export interface ControllerStateAndHelpers {
// prop getters
getRootProps: (options: GetRootPropsOptions) => any;
getButtonProps: (options: GetButtonPropsOptions) => any;
getLabelProps: (options: GetLabelPropsOptions) => any;
getInputProps: (options: GetInputPropsOptions) => any;
getButtonProps: (options?: GetButtonPropsOptions) => any;
getLabelProps: (options?: GetLabelPropsOptions) => any;
getInputProps: (options?: GetInputPropsOptions) => any;
getItemProps: (options: GetItemPropsOptions) => any;

// actions
Expand Down