Skip to content

Commit cb3eaf6

Browse files
authored
Merge pull request #116 from packetloop/Restore-typescript
Restore typescript
2 parents 05e953f + df8bf7c commit cb3eaf6

File tree

5 files changed

+100
-3
lines changed

5 files changed

+100
-3
lines changed

.eslintignore

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,3 @@
33
/lib/
44
/pub/
55
/reports/
6-
*.ts
7-
*.tsx

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,20 @@ Will result in
154154
className="user-name" />
155155
```
156156

157+
## Typescript
158+
159+
This library has typescript typings, import them the same way as in javascript:
160+
161+
```typescript
162+
import {DebounceInput} from 'react-debounce-input';
163+
```
164+
165+
Also there are helper types `DebounceTextArea` and `Debounced` to provide strict interfaces for wrapping components different from standard `<input />`. Check usage examples in `example/typescript-example.tsx`.
166+
167+
168+
*NOTE* library author is not using Typescript, so if you are using typings and found an issue, please submit a PR with fix. Thanks @iyegoroff for the initial TS support!
169+
170+
157171
## Development and testing
158172

159173
Currently is being developed and tested with the latest stable `Node` on `OSX`.

example/typescript-example.tsx

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import * as React from 'react';
2+
import { DebounceInput, DebounceTextArea, Debounced } from "../src";
3+
4+
// - usage with default 'input' element:
5+
6+
<DebounceInput
7+
className={'some-class'}
8+
onChange={(e: React.ChangeEvent<HTMLInputElement>) => {}}
9+
/>
10+
11+
// - usage with 'textarea':
12+
13+
// DebounceTextArea is just a type, so it should be explicitly defined as value
14+
const DebounceTextArea: DebounceTextArea = DebounceInput;
15+
16+
<DebounceTextArea
17+
element={'textarea'}
18+
rows={1}
19+
cols={2}
20+
onChange={(e: React.ChangeEvent<HTMLTextAreaElement>) => {}}
21+
/>
22+
23+
// - usage with custom element that has 'value' and 'onChange' props:
24+
25+
interface MyComponentProps {
26+
value?: string | number;
27+
onChange: React.ChangeEventHandler<MyComponent>;
28+
29+
myCustomProp: number;
30+
}
31+
32+
class MyComponent extends React.Component<MyComponentProps> {}
33+
34+
const DebouncedMyComponent: Debounced<MyComponent, MyComponentProps> = DebounceInput;
35+
36+
<DebouncedMyComponent
37+
element={MyComponent}
38+
myCustomProp={1} // OK, myCustomProp will be passed down to MyComponent
39+
// myInvalidCustomProp={2} // Error, there is no myInvalidCustomProp in MyComponentProps
40+
onChange={(e: React.ChangeEvent<MyComponent>) => {}}
41+
/>
42+

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
"postversion": "git push --follow-tags",
3333
"nuke": "rm -rf node_modules yarn.lock",
3434
"postnuke": "yarn install",
35-
"deps": "! depcheck --specials=bin,eslint --ignore-dirs=build,lib,pub | grep --invert-match 'No depcheck issue'"
35+
"deps": "! depcheck --specials=bin,eslint --ignore-dirs=build,lib,pub --ignores=@types/* | grep --invert-match 'No depcheck issue'"
3636
},
3737
"repository": {
3838
"type": "git",
@@ -67,6 +67,7 @@
6767
"@babel/preset-env": "^7.0.0",
6868
"@babel/preset-react": "^7.0.0",
6969
"@babel/register": "^7.0.0",
70+
"@types/react": "^16.9.7",
7071
"babel-eslint": "^10.0.3",
7172
"babel-loader": "^8.0.0",
7273
"babel-plugin-transform-react-remove-prop-types": "^0.4.24",

src/index.d.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* check usage examples in ./example/typescript-example.tsx
3+
*/
4+
5+
import * as React from 'react';
6+
7+
interface PropConstraints<T> {
8+
readonly value?: string | number;
9+
readonly onChange: React.ChangeEventHandler<T>;
10+
}
11+
12+
export type DebounceInputProps<WrappedComponent, WrappedComponentProps> = WrappedComponentProps & {
13+
readonly element?: string | React.ComponentType<PropConstraints<WrappedComponent>>;
14+
readonly type?: string;
15+
readonly onChange: React.ChangeEventHandler<WrappedComponent>;
16+
readonly onKeyDown?: React.KeyboardEventHandler<WrappedComponent>;
17+
readonly onBlur?: React.FocusEventHandler<WrappedComponent>;
18+
readonly value?: string | number;
19+
readonly placeholder?: string | number;
20+
readonly minLength?: number;
21+
readonly debounceTimeout?: number;
22+
readonly forceNotifyByEnter?: boolean;
23+
readonly forceNotifyOnBlur?: boolean;
24+
readonly inputRef?: React.Ref<WrappedComponent>;
25+
};
26+
27+
export declare class DebounceInput<
28+
WrappedComponent = HTMLInputElement,
29+
WrappedComponentProps = React.InputHTMLAttributes<HTMLInputElement>
30+
> extends React.PureComponent<DebounceInputProps<WrappedComponent, WrappedComponentProps>> {
31+
32+
}
33+
34+
export type Debounced<
35+
WrappedComponent,
36+
WrappedComponentProps
37+
> = React.ComponentType<DebounceInputProps<WrappedComponent, WrappedComponentProps>>;
38+
39+
export type DebounceTextArea = Debounced<
40+
HTMLTextAreaElement,
41+
React.TextareaHTMLAttributes<HTMLTextAreaElement>
42+
>;

0 commit comments

Comments
 (0)