Skip to content
This repository has been archived by the owner on Nov 27, 2020. It is now read-only.

Commit

Permalink
feat: add TS definitions (#11)
Browse files Browse the repository at this point in the history
* feat: add TS definitions

* docs: added Typescript Usage md file

docs

* fix(feat): missing typescript dependency

* docs: added contributor, update kcd-scripts
  • Loading branch information
Jedrzej Lewandowski authored and Kent C. Dodds committed Dec 9, 2017
1 parent 2c8bafa commit 94b1a56
Show file tree
Hide file tree
Showing 7 changed files with 139 additions and 32 deletions.
9 changes: 9 additions & 0 deletions .all-contributorsrc
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,15 @@
"contributions": [
"code"
]
},
{
"login": "TheFullResolution",
"name": "Jedrzej Lewandowski",
"avatar_url": "https://avatars2.githubusercontent.com/u/11708648?v=4",
"profile": "http://www.thefullresolution.com/",
"contributions": [
"code"
]
}
]
}
64 changes: 35 additions & 29 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
[![Code Coverage][coverage-badge]][coverage]
[![downloads][downloads-badge]][npmcharts]
[![version][version-badge]][package]
[![MIT License][license-badge]][LICENSE]
[![MIT License][license-badge]][license]

[![All Contributors](https://img.shields.io/badge/all_contributors-3-orange.svg?style=flat-square)](#contributors)
[![All Contributors](https://img.shields.io/badge/all_contributors-4-orange.svg?style=flat-square)](#contributors)
[![PRs Welcome][prs-badge]][prs]
[![Chat][chat-badge]][chat]
[![Code of Conduct][coc-badge]][coc]
Expand All @@ -41,18 +41,23 @@ This follows the patterns in [`downshift`][downshift] to expose an API that
renders nothing and simply encapsulates the logic of a toggle component.

## Table of Contents

<!-- START doctoc generated TOC please keep comment here to allow auto update -->
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->

<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->

- [Installation](#installation)
- [Usage](#usage)
- [Props:](#props)
- [Examples](#examples)
- [Inspiration](#inspiration)
- [Other Solutions](#other-solutions)
- [Contributors](#contributors)
- [LICENSE](#license)
* [Installation](#installation)
* [Usage](#usage)
* [Props:](#props)
* [defaultOn](#defaulton)
* [onToggle](#ontoggle)
* [on](#on)
* [children](#children)
* [Examples](#examples)
* [Inspiration](#inspiration)
* [Other Solutions](#other-solutions)
* [Contributors](#contributors)
* [LICENSE](#license)

<!-- END doctoc generated TOC please keep comment here to allow auto update -->

Expand Down Expand Up @@ -88,7 +93,7 @@ render(
</div>
)}
</Toggle>,
document.getElementById('root')
document.getElementById('root'),
)
```

Expand All @@ -110,8 +115,8 @@ The initial `on` state.
Called when the toggler is clicked.

- `on`: The new on state
- `TogglerStateAndHelpers`: the exact same thing you get in your child render
* `on`: The new on state
* `TogglerStateAndHelpers`: the exact same thing you get in your child render
prop function.

### on
Expand All @@ -135,12 +140,11 @@ that state from other components, `redux`, `react-router`, or anywhere else.
> in react (like `<input />`). If you want to learn more about this concept, you
> can learn about that from this the
> ["Controlled Components" lecture][controlled-components-lecture] and
> exercises from [React Training's][react-training]
> [Advanced React][advanced-react] course.
> exercises from [React Training's][react-training] > [Advanced React][advanced-react] course.
### children

> `function({})` | *required*
> `function({})` | _required_
This is called with an object.

Expand All @@ -150,21 +154,21 @@ The function is passed as the child prop:

<!-- This table was generated via http://www.tablesgenerator.com/markdown_tables -->

| property | category | type | description |
|-------------------|-------------|---------------------------|--------------------------------------------------------------------------------------------------|
| `on` | state | `boolean` | The current `on` state of toggle |
| `getTogglerProps` | prop getter | `function(props: object)` | returns the props you should apply to the button element you render. Includes `aria-` attributes |
| `getInputTogglerProps` | prop getter | `function(props: object)` | returns the props you should apply to the input (checkbox) element you render. Includes `aria-` attributes |
| property | category | type | description |
| ------------------------ | ----------- | ------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `on` | state | `boolean` | The current `on` state of toggle |
| `getTogglerProps` | prop getter | `function(props: object)` | returns the props you should apply to the button element you render. Includes `aria-` attributes |
| `getInputTogglerProps` | prop getter | `function(props: object)` | returns the props you should apply to the input (checkbox) element you render. Includes `aria-` attributes |
| `getElementTogglerProps` | prop getter | `function(props: object)` | returns the props you should apply to the element you render. Use this if you are not using a button or input—for example, a span. Includes `aria-` attributes |
| `setOn` | action | `function()` | Sets the `on` state to `true` |
| `setOff` | action | `function()` | Sets the `on` state to `false` |
| `toggle` | action | `function()` | Toggles the `on` state (i.e. if it's currently `true`, will set to `false`) |
| `setOn` | action | `function()` | Sets the `on` state to `true` |
| `setOff` | action | `function()` | Sets the `on` state to `false` |
| `toggle` | action | `function()` | Toggles the `on` state (i.e. if it's currently `true`, will set to `false`) |

## Examples

Examples exist on [codesandbox.io][examples]:

- [Bare bones toggle](https://codesandbox.io/s/m38674w9vy)
* [Bare bones toggle](https://codesandbox.io/s/m38674w9vy)

If you would like to add an example, follow these steps:

Expand Down Expand Up @@ -194,8 +198,11 @@ are tons of them, so just
Thanks goes to these people ([emoji key][emojis]):

<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->
| [<img src="https://avatars.githubusercontent.com/u/1500684?v=3" width="100px;"/><br /><sub><b>Kent C. Dodds</b></sub>](https://kentcdodds.com)<br />[💻](https://github.com/kentcdodds/react-toggled/commits?author=kentcdodds "Code") [📖](https://github.com/kentcdodds/react-toggled/commits?author=kentcdodds "Documentation") [🚇](#infra-kentcdodds "Infrastructure (Hosting, Build-Tools, etc)") [⚠️](https://github.com/kentcdodds/react-toggled/commits?author=kentcdodds "Tests") | [<img src="https://avatars3.githubusercontent.com/u/9488719?v=4" width="100px;"/><br /><sub><b>Frank Tan</b></sub>](https://github.com/tansongyang)<br />[💻](https://github.com/kentcdodds/react-toggled/commits?author=tansongyang "Code") [📖](https://github.com/kentcdodds/react-toggled/commits?author=tansongyang "Documentation") [⚠️](https://github.com/kentcdodds/react-toggled/commits?author=tansongyang "Tests") | [<img src="https://avatars1.githubusercontent.com/u/9408641?v=4" width="100px;"/><br /><sub><b>Oliver</b></sub>](http://www.oliverjam.es)<br />[💻](https://github.com/kentcdodds/react-toggled/commits?author=oliverjam "Code") |
| :---: | :---: | :---: |

<!-- prettier-ignore -->
| [<img src="https://avatars.githubusercontent.com/u/1500684?v=3" width="100px;"/><br /><sub><b>Kent C. Dodds</b></sub>](https://kentcdodds.com)<br />[💻](https://github.com/kentcdodds/react-toggled/commits?author=kentcdodds "Code") [📖](https://github.com/kentcdodds/react-toggled/commits?author=kentcdodds "Documentation") [🚇](#infra-kentcdodds "Infrastructure (Hosting, Build-Tools, etc)") [⚠️](https://github.com/kentcdodds/react-toggled/commits?author=kentcdodds "Tests") | [<img src="https://avatars3.githubusercontent.com/u/9488719?v=4" width="100px;"/><br /><sub><b>Frank Tan</b></sub>](https://github.com/tansongyang)<br />[💻](https://github.com/kentcdodds/react-toggled/commits?author=tansongyang "Code") [📖](https://github.com/kentcdodds/react-toggled/commits?author=tansongyang "Documentation") [⚠️](https://github.com/kentcdodds/react-toggled/commits?author=tansongyang "Tests") | [<img src="https://avatars1.githubusercontent.com/u/9408641?v=4" width="100px;"/><br /><sub><b>Oliver</b></sub>](http://www.oliverjam.es)<br />[💻](https://github.com/kentcdodds/react-toggled/commits?author=oliverjam "Code") | [<img src="https://avatars2.githubusercontent.com/u/11708648?v=4" width="100px;"/><br /><sub><b>Jedrzej Lewandowski</b></sub>](http://www.thefullresolution.com/)<br />[💻](https://github.com/kentcdodds/react-toggled/commits?author=TheFullResolution "Code") |
| :---: | :---: | :---: | :---: |

<!-- ALL-CONTRIBUTORS-LIST:END -->

This project follows the [all-contributors][all-contributors] specification.
Expand Down Expand Up @@ -239,7 +246,6 @@ MIT
[all-contributors]: https://github.com/kentcdodds/all-contributors
[ryan]: https://github.com/ryanflorence
[compound-components-lecture]: https://courses.reacttraining.com/courses/advanced-react/lectures/3060560

[examples]: https://codesandbox.io/search?refinementList%5Btags%5D%5B0%5D=react-toggled%3Aexample&page=1
[controlled-components-lecture]: https://courses.reacttraining.com/courses/advanced-react/lectures/3172720
[react-training]: https://reacttraining.com/
Expand Down
6 changes: 6 additions & 0 deletions other/TYPESCRIPT_USAGE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Typescript Usage

The current bundled Typescript definitions are incomplete and based around the
needs of the developers who contributed them.

Pull requests to improve them are welcome and appreciated. If you've never contributed to open source before, then you may find [this free video course](https://egghead.io/courses/how-to-contribute-to-an-open-source-project-on-github) helpful.
9 changes: 6 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,22 @@
"main": "dist/react-toggled.cjs.js",
"jsnext:main": "dist/react-toggled.esm.js",
"module": "dist/react-toggled.esm.js",
"typings": "typings/index.d.ts",
"scripts": {
"add-contributor": "kcd-scripts contributors add",
"build": "kcd-scripts build --bundle --p-react",
"lint": "kcd-scripts lint",
"test": "kcd-scripts test",
"test:cover": "kcd-scripts test --coverage",
"test:ts": "tsc --noEmit -p ./tsconfig.json",
"test:update": "npm run test:cover -s -- --updateSnapshot",
"test:build":
"kcd-scripts test --config other/misc-tests/jest.config.js --no-watch",
"build-and-test": "npm run build -s && npm run test:build -s",
"storybook": "start-storybook -p 6006 -c stories",
"storybook:build":
"cd stories && npm install && cd .. && build-storybook -c stories",
"validate": "kcd-scripts validate lint,build-and-test,test:cover",
"validate": "kcd-scripts validate lint,build-and-test,test:cover,test:ts",
"precommit": "kcd-scripts precommit"
},
"files": ["dist", "typings", "preact"],
Expand All @@ -36,13 +38,14 @@
"enzyme-adapter-react-16": "^1.0.1",
"enzyme-to-json": "^3.1.2",
"jest-serializer-html": "^4.0.0",
"kcd-scripts": "^0.24.0",
"kcd-scripts": "^0.30.4",
"preact": "^8.2.1",
"preact-render-to-string": "^3.6.3",
"prop-types": "^15.5.10",
"react": "^16.0.0",
"react-dom": "^16.0.0",
"react-test-renderer": "^16.0.0"
"react-test-renderer": "^16.0.0",
"typescript": "^2.6.2"
},
"eslintConfig": {
"extends": "./node_modules/kcd-scripts/eslint.js"
Expand Down
41 changes: 41 additions & 0 deletions test/basic.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import * as React from 'react'
import Toggle from '../'

interface Props {}

interface State {}

export default class App extends React.Component<Props, State> {

onClickCall = () => console.log('hello')

render() {
return (
<Toggle>
{({
on,
getTogglerProps,
toggle,
setOn,
setOff,
getInputTogglerProps,
getElementTogglerProps
}) => (
<div>
<button {...getTogglerProps({onClick: this.onClickCall})}>Toggle</button>
<button onClick={toggle}>uses toggle action</button>
<button onClick={setOn}>set on</button>
<button onClick={setOff}>set off</button>
<input
type="button"
value="Toggle Input"
{...getInputTogglerProps()}
/>
<span {...getElementTogglerProps()}>ToggleSpan</span>
<div>{on ? 'Toggled On' : 'Toggled Off'}</div>
</div>
)}
</Toggle>
)
}
}
7 changes: 7 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"compilerOptions": {
"jsx": "react",
"noUnusedLocals": true
},
"include": ["test/**/*.tsx"]
}
35 changes: 35 additions & 0 deletions typings/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import * as React from 'react'

export interface GetElementPropsOptions extends React.HTMLProps<HTMLElement> {}

export interface GetInputPropsOptions
extends React.HTMLProps<HTMLInputElement> {}

export interface GetButtonPropsOptions
extends React.HTMLProps<HTMLButtonElement> {}

export interface TogglerStateAndHelpers {
readonly on: boolean
readonly getTogglerProps: (options?: GetButtonPropsOptions) => any
readonly getInputTogglerProps: (options?: GetButtonPropsOptions) => any
readonly getElementTogglerProps: (options?: GetElementPropsOptions) => any
readonly setOn: () => void
readonly setOff: () => void
readonly toggle: () => void
}

export type ChildrenFunction = (
options: TogglerStateAndHelpers,
) => React.ReactNode

export interface ReactToggledProps {
readonly defaultOn?: boolean
readonly onToggle?: (on: boolean, object: TogglerStateAndHelpers) => void
readonly on?: boolean
readonly children: ChildrenFunction
}

export type ReactToggledInterface = React.ComponentClass<ReactToggledProps>

declare const Toggle: ReactToggledInterface
export default Toggle

0 comments on commit 94b1a56

Please sign in to comment.