Skip to content

Commit 76190da

Browse files
beckendctrlplusb
authored andcommitted
typings: withSize add onSize callback (#167)
* typings: withSize add onSize callback * readonly what we can * review points * Update react-sizeme.d.ts
1 parent f030120 commit 76190da

File tree

3 files changed

+37
-14
lines changed

3 files changed

+37
-14
lines changed

react-sizeme.d.ts

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,42 +2,50 @@
22

33
import { Component, ComponentType, ReactNode, ReactElement } from 'react'
44

5-
declare namespace sizeMe {
6-
type Omit<T, K> = Pick<T, Exclude<keyof T, K>>
5+
type Omit<T, K> = Pick<T, Exclude<keyof T, K>>
76

7+
declare namespace sizeMe {
88
export interface SizeMeProps {
9-
size: {
10-
width: number | null
11-
height: number | null
9+
readonly size: {
10+
readonly width: number | null
11+
readonly height: number | null
1212
}
1313
}
1414

1515
export interface SizeMeOptions {
16-
monitorWidth?: boolean
1716
monitorHeight?: boolean
1817
monitorPosition?: boolean
19-
refreshRate?: number
20-
refreshMode?: 'throttle' | 'debounce'
18+
monitorWidth?: boolean
2119
noPlaceholder?: boolean
20+
refreshMode?: 'throttle' | 'debounce'
21+
refreshRate?: number
2222
}
2323

2424
export interface SizeMeRenderProps extends SizeMeOptions {
2525
children: (props: SizeMeProps) => ReactElement
2626
}
27+
2728
export class SizeMe extends Component<SizeMeRenderProps> {}
2829

29-
export const withSize: (
30+
export type WithSizeOnSizeCallback = (size: SizeMeProps['size']) => void
31+
32+
export interface WithSizeProps {
33+
onSize?: WithSizeOnSizeCallback
34+
}
35+
36+
export function withSize(
3037
options?: SizeMeOptions,
31-
) => <P extends object = {}>(
38+
): <P extends object = {}>(
3239
component: ComponentType<P>,
33-
) => ComponentType<Omit<P, 'size'>>
40+
) => ComponentType<Omit<P, 'size'> & WithSizeProps>
3441

3542
export let noPlaceholders: boolean
3643
}
44+
3745
declare function sizeMe(
3846
options?: sizeMe.SizeMeOptions,
3947
): <P extends object = {}>(
4048
component: ComponentType<P>,
41-
) => ComponentType<sizeMe.Omit<P, 'size'>>
49+
) => ComponentType<Omit<P, 'size'> & sizeMe.WithSizeProps>
4250

4351
export = sizeMe

src/__tests__/typescript/hoc.tsx

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as React from 'react'
2-
import { withSize, SizeMeProps } from 'react-sizeme'
2+
import { withSize, SizeMeProps, WithSizeOnSizeCallback } from 'react-sizeme'
33

44
interface MyComponentProps extends SizeMeProps {
55
id: number
@@ -22,4 +22,17 @@ function MyComponent({ id, size }: MyComponentProps) {
2222

2323
const SizedMyComponent = withSize()(MyComponent)
2424

25-
const foo = <SizedMyComponent id={1} />
25+
const onSize: WithSizeOnSizeCallback = ({ height, width }) => {
26+
if (width) {
27+
const foo = width + 1
28+
}
29+
if (height) {
30+
const foo = height + 1
31+
}
32+
// typings:expect-error
33+
const h1 = height + 1
34+
// typings:expect-error
35+
const w1 = width + 1
36+
}
37+
38+
const foo = <SizedMyComponent id={1} onSize={onSize} />

src/__tests__/with-size.test.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,10 +127,12 @@ describe('withSize', () => {
127127

128128
class SizeCallbackWrapper extends React.Component {
129129
state = {
130+
// eslint-disable-next-line react/no-unused-state
130131
size: null,
131132
}
132133
onSize = size =>
133134
this.setState({
135+
// eslint-disable-next-line react/no-unused-state
134136
size,
135137
})
136138
render() {

0 commit comments

Comments
 (0)