Skip to content

Commit 1ed0092

Browse files
ellingemrchief
authored andcommitted
fix: Add TypeScript definitions (#223)
Adds TypeScript-definitions I also noticed that onAction is not correctly implemented. Currently sends a { action: string, id: string }. There is code in index.js which tries to fetch the node. But it does not seem be working. It expects two parameters but receives the object described above in the first parameter. ![bild](https://user-images.githubusercontent.com/17863113/54728331-8d972980-4b7d-11e9-9490-a3bd6b08481f.png) Changed the examples/stories to get the node id at least. Also changed in readme where onAction should be applied. Fixes #209 - [X] New feature
1 parent e7ca18c commit 1ed0092

File tree

8 files changed

+176
-13
lines changed

8 files changed

+176
-13
lines changed

README.md

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,8 @@ const data = {
150150
const onChange = (currentNode, selectedNodes) => {
151151
console.log('onChange::', currentNode, selectedNodes)
152152
}
153-
const onAction = ({ action, node }) => {
154-
console.log(`onAction:: [${action}]`, node)
153+
const onAction = ({ action, id }) => {
154+
console.log(`onAction:: [${action}]`, id)
155155
}
156156
const onNodeToggle = currentNode => {
157157
console.log('onNodeToggle::', currentNode)
@@ -210,6 +210,20 @@ function onNodeToggle(currentNode) {
210210
return <DropdownTreeSelect data={data} onNodeToggle={onNodeToggle} />
211211
```
212212

213+
### onAction
214+
215+
Type: `function`
216+
217+
Fires when a action is triggered. Example:
218+
219+
```jsx
220+
function onAction({action, id}) {
221+
console.log(`onAction:: [${action}]`, id)
222+
}
223+
224+
return <DropdownTreeSelect data={data} onAction={onAction} />
225+
```
226+
213227
### onFocus
214228

215229
Type: `function`
@@ -250,7 +264,6 @@ The `action` object requires the following structure:
250264
```js
251265
{
252266
className, // required: CSS class for the node. e.g. `fa fa-info`
253-
onAction, // required: Fired on click of the action. The event handler receives `action` object as well as the `node` object.
254267
title, // optional: HTML tooltip text
255268
text, // optional: Any text to be displayed. This is helpful to pass ligatures if you're using ligature fonts
256269
... // optional: Any extra properties that you'd like to receive during `onChange` event
@@ -314,7 +327,7 @@ Type: `bool` (default: `false`)
314327

315328
Type: `string`
316329

317-
Specific id for container. The container renders with a default id of `rdtsN` where N is count of the current component rendered.
330+
Specific id for container. The container renders with a default id of `rdtsN` where N is the count of the current component rendered.
318331

319332
Use to ensure a own unique id when a simple counter is not sufficient, e.g in a partial server render (SSR)
320333

docs/src/stories/BigData/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import bigData from './big-data.json'
88
const onChange = (curNode, selectedNodes) => {
99
console.log('onChange::', curNode, selectedNodes)
1010
}
11-
const onAction = ({ action, node }) => {
12-
console.log(`onAction:: [${action}]`, node)
11+
const onAction = ({ action, id }) => {
12+
console.log(`onAction:: [${action}]`, id)
1313
}
1414
const onNodeToggle = curNode => {
1515
console.log('onNodeToggle::', curNode)

docs/src/stories/DefaultValues/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ import data from './data.json'
99
const onChange = (curNode, selectedNodes) => {
1010
console.log('onChange::', curNode, selectedNodes)
1111
}
12-
const onAction = ({ action, node }) => {
13-
console.log(`onAction:: [${action}]`, node)
12+
const onAction = ({ action, id }) => {
13+
console.log(`onAction:: [${action}]`, id)
1414
}
1515
const onNodeToggle = curNode => {
1616
console.log('onNodeToggle::', curNode)

docs/src/stories/Options/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ class WithOptions extends PureComponent {
2424
onChange = (curNode, selectedNodes) => {
2525
console.log('onChange::', curNode, selectedNodes)
2626
}
27-
onAction = ({ action, node }) => {
28-
console.log(`onAction:: [${action}]`, node)
27+
onAction = ({ action, id }) => {
28+
console.log(`onAction:: [${action}]`, id)
2929
}
3030
onNodeToggle = curNode => {
3131
console.log('onNodeToggle::', curNode)

docs/src/stories/Simple/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import data from './data.json'
88
const onChange = (curNode, selectedNodes) => {
99
console.log('onChange::', curNode, selectedNodes)
1010
}
11-
const onAction = ({ action, node }) => {
12-
console.log(`onAction:: [${action}]`, node)
11+
const onAction = ({ action, id }) => {
12+
console.log(`onAction:: [${action}]`, id)
1313
}
1414
const onNodeToggle = curNode => {
1515
console.log('onNodeToggle::', curNode)

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
"search"
2020
],
2121
"main": "dist/react-dropdown-tree-select.js",
22+
"types": "dist/react-dropdown-tree-select.d.ts",
2223
"repository": "https://github.com/dowjones/react-dropdown-tree-select.git",
2324
"author": "Hrusikesh Panda <hrusikesh.panda@dowjones.com>",
2425
"license": "MIT",
@@ -65,6 +66,7 @@
6566
"babel-preset-stage-0": "6.24.1",
6667
"commitizen": "^2.9.6",
6768
"conventional-changelog-cli": "^2.0.5",
69+
"copy-webpack-plugin": "^5.0.1",
6870
"coveralls": "^3.0.0",
6971
"cross-env": "^5.0.5",
7072
"css-loader": "^0.28.0",

types/react-dropdown-tree-select.d.ts

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
// tslint:disable:interface-name
2+
declare module "react-dropdown-tree-select" {
3+
import * as React from "react";
4+
5+
export type TreeData = Object | TreeNodeProps[];
6+
7+
export interface DropdownTreeSelectProps {
8+
data: TreeData;
9+
/** Clear the input search if a node has been selected/unselected */
10+
clearSearchOnChange?: boolean;
11+
/** Displays search results as a tree instead of flattened results */
12+
keepTreeOnSearch?: boolean;
13+
/** Displays children of found nodes to allow searching for a parent node on
14+
* then selecting any child node of the found node. Defaults to false
15+
* NOTE this works only in combination with keepTreeOnSearch
16+
*/
17+
keepChildrenOnSearch?: boolean;
18+
/** The text to display as placeholder on the search box. Defaults to Choose... */
19+
placeholderText?: string;
20+
/** If set to true, shows the dropdown when rendered.
21+
* This can be used to render the component with the dropdown open as its initial state
22+
*/
23+
showDropdown?: boolean;
24+
/** Additional classname for container.
25+
* The container renders with a default classname of react-dropdown-tree-select
26+
*/
27+
className?: string;
28+
/** Fires when a node change event occurs. Currently the following actions trigger a node change:
29+
* Checkbox click which checks/unchecks the item
30+
* Closing of pill (which unchecks the corresponding checkbox item)
31+
*
32+
* Calls the handler with the current node object and all selected nodes (if any)
33+
*/
34+
onChange?: (currentNode: TreeNode, selectedNodes: TreeNode[]) => void;
35+
/** Fired on click of the action */
36+
onAction?: (event: ActionEvent) => void;
37+
/** Fires when a node is expanded or collapsed.
38+
* Calls the handler with the current node object
39+
*/
40+
onNodeToggle?: (currentNode: TreeNode) => void;
41+
/** Fires when input box receives focus or the dropdown arrow is clicked.
42+
* This is helpful for setting dirty or touched flags with forms
43+
*/
44+
onFocus?: () => void;
45+
/** Fires when input box loses focus or the dropdown arrow is clicked again (and the dropdown collapses).
46+
* This is helpful for setting dirty or touched flags with forms
47+
*/
48+
onBlur?: () => void;
49+
/** Turns the dropdown into a simple, single select dropdown.
50+
* If you pass tree data, only immediate children are picked, grandchildren nodes are ignored.
51+
* Defaults to false
52+
*/
53+
simpleSelect?: boolean;
54+
/** The text to display when the search does not find results in the content list. Defaults to No matches found */
55+
noMatchesText?: string;
56+
/** If set to true, shows checkboxes in a partial state when one, but not all of their children are selected.
57+
* Allows styling of partially selected nodes as well, by using :indeterminate pseudo class.
58+
* Simply add desired styles to .node.partial .checkbox-item:indeterminate { ... } in your CSS
59+
*/
60+
showPartiallySelected?: boolean;
61+
/** disabled=true disables the dropdown completely. This is useful during form submit events */
62+
disabled?: boolean;
63+
/** readOnly=true makes the dropdown read only,
64+
* which means that the user can still interact with it but cannot change any of its values.
65+
* This can be useful for display only forms
66+
*/
67+
readOnly?: boolean;
68+
hierarchical?: boolean;
69+
/** Specific id for container. The container renders with a default id of `rdtsN` where N is count of the current component rendered
70+
* Use to ensure a own unique id when a simple counter is not sufficient, e.g in a partial server render (SSR)
71+
*/
72+
id?: string;
73+
}
74+
75+
export interface DropdownTreeSelectState {
76+
showDropdown: boolean;
77+
searchModeOn: boolean;
78+
allNodesHidden: boolean;
79+
tree: TreeNode[];
80+
tags: TreeNode[];
81+
}
82+
83+
export default class DropdownTreeSelect extends React.Component<DropdownTreeSelectProps, DropdownTreeSelectState> {
84+
node: HTMLDivElement;
85+
searchInput: HTMLInputElement;
86+
keepDropdownActive: boolean;
87+
handleClick(): void;
88+
}
89+
90+
export interface TreeNode {
91+
/** Checkbox label */
92+
label: string;
93+
/** Checkbox value */
94+
value: string;
95+
/** Initial state of checkbox. if true, checkbox is selected and corresponding pill is rendered. */
96+
checked?: boolean;
97+
/** Selectable state of checkbox. if true, the checkbox is disabled and the node is not selectable. */
98+
disabled?: boolean;
99+
/** If true, the node is expanded
100+
* (children of children nodes are not expanded by default unless children nodes also have expanded: true).
101+
*/
102+
expanded?: boolean;
103+
/** Additional css class for the node. This is helpful to style the nodes your way */
104+
className?: string;
105+
/** Css class for the corresponding tag. Use this to add custom style the pill corresponding to the node. */
106+
tagClassName?: string;
107+
/** An array of extra action on the node (such as displaying an info icon or any custom icons/elements) */
108+
actions?: NodeAction[];
109+
/** Allows data-* attributes to be set on the node and tag elements */
110+
dataset?: NodeDataSet;
111+
/** Indicate if a node is a default value.
112+
* When true, the dropdown will automatically select the node(s) when there is no other selected node.
113+
* Can be used on more than one node.
114+
*/
115+
isDefaultValue?: boolean;
116+
/** Any extra properties that you'd like to receive during `onChange` event */
117+
[property: string]: any;
118+
}
119+
120+
export interface TreeNodeProps extends TreeNode {
121+
/** Array of child objects */
122+
children?: Node[];
123+
}
124+
125+
export interface NodeAction {
126+
/** CSS class for the node. e.g. `fa fa-info` */
127+
className: string;
128+
/** HTML tooltip text */
129+
title?: string;
130+
/** Any text to be displayed. This is helpful to pass ligatures if you're using ligature fonts */
131+
text?: string;
132+
/** Any extra properties that you'd like to receive during `onChange` event */
133+
[property: string]: any;
134+
}
135+
136+
export interface ActionEvent {
137+
action: string;
138+
id: string;
139+
}
140+
141+
export interface NodeDataSet {
142+
[property: string]: any;
143+
}
144+
}

webpack.config.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const path = require('path')
22
const webpack = require('webpack')
33
const MiniCssExtractPlugin = require('mini-css-extract-plugin')
4+
const CopyPlugin = require('copy-webpack-plugin')
45
const { BundleAnalyzerPlugin } = require('webpack-bundle-analyzer')
56

67
module.exports = {
@@ -34,7 +35,10 @@ module.exports = {
3435
analyzerMode: 'static',
3536
openAnalyzer: false,
3637
generateStatsFile: true
37-
})
38+
}),
39+
new CopyPlugin([
40+
{ from: path.join(__dirname, 'types'), to: path.join(__dirname, 'dist') }
41+
])
3842
],
3943
module: {
4044
rules: [

0 commit comments

Comments
 (0)