Skip to content

Commit 0c476c4

Browse files
committed
migrate to TypeScript 4
1 parent 041557f commit 0c476c4

File tree

14 files changed

+145
-68
lines changed

14 files changed

+145
-68
lines changed

.DS_Store

6 KB
Binary file not shown.

.size.json

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
[
2+
{
3+
"name": "dist/es2015/index.js",
4+
"passed": true,
5+
"size": 380
6+
},
7+
{
8+
"name": "dist/es2015/useRef.js",
9+
"passed": true,
10+
"size": 125
11+
},
12+
{
13+
"name": "dist/es2015/useMergeRef.js",
14+
"passed": true,
15+
"size": 174
16+
}
17+
]

CHANGELOG.md

Whitespace-only changes.

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2017 Anton Korzunov
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

__tests__/index.tsx

Lines changed: 56 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,19 @@
1+
import { configure, mount } from 'enzyme';
2+
import Adapter from 'enzyme-adapter-react-16';
13
import * as React from 'react';
2-
import {createRef} from "react";
3-
import {mount} from 'enzyme';
4-
import {createCallbackRef, mergeRefs, useTransformRef, useCallbackRef, useMergeRefs} from "../src";
5-
import {refToCallback, useRefToCallback} from "../src/refToCallback";
4+
import { createRef } from 'react';
65

6+
import {
7+
refToCallback,
8+
useRefToCallback,
9+
createCallbackRef,
10+
mergeRefs,
11+
useTransformRef,
12+
useCallbackRef,
13+
useMergeRefs,
14+
} from '../src';
15+
16+
configure({ adapter: new Adapter() });
717

818
describe('Specs', () => {
919
describe('useCallbackRef', () => {
@@ -16,7 +26,11 @@ describe('Specs', () => {
1626
it('shall work as createRef', () => {
1727
const spy = jest.fn();
1828
const ref = createCallbackRef<HTMLDivElement>(spy);
19-
mount(<div ref={ref}>test</div>).setProps({}).update();
29+
30+
mount(<div ref={ref}>test</div>)
31+
.setProps({})
32+
.update();
33+
2034
expect(spy).toBeCalledWith(ref.current, null);
2135
expect(spy).toHaveBeenCalledTimes(1);
2236
expect(ref.current).not.toBe(null);
@@ -26,51 +40,49 @@ describe('Specs', () => {
2640
const spy1 = jest.fn();
2741
const spy2 = jest.fn();
2842
let counter = 0;
43+
2944
const Test = () => {
3045
const x = counter++;
3146
const ref = useCallbackRef<HTMLDivElement>(null, () => spy1(x));
3247
const mref = useMergeRefs([ref, spy2]);
33-
return <div key={x < 2 ? '1' : '2'} ref={mref}>test</div>;
48+
49+
return (
50+
<div key={x < 2 ? '1' : '2'} ref={mref}>
51+
test
52+
</div>
53+
);
3454
};
35-
const wrapper = mount(<Test/>);
55+
56+
const wrapper = mount(<Test />);
3657
expect(spy1).toBeCalledWith(0);
3758
expect(spy1).toHaveBeenCalledTimes(1);
3859
expect(spy2).not.toBeCalledWith(null);
3960
expect(spy2).toHaveBeenCalledTimes(1);
4061

41-
wrapper.setProps({x: 42});
62+
wrapper.setProps({ x: 42 });
4263
expect(spy1).toBeCalledWith(0);
4364
expect(spy1).toHaveBeenCalledTimes(1);
4465
expect(spy2).not.toBeCalledWith(null);
4566

46-
wrapper.setProps({x: 24});
67+
wrapper.setProps({ x: 24 });
4768
expect(spy1).toBeCalledWith(2);
4869
expect(spy1).toHaveBeenCalledTimes(3);
4970
expect(spy2).toBeCalledWith(null);
5071
});
5172
});
5273

5374
describe('mergeRef', () => {
54-
it("merges two refs", () => {
75+
it('merges two refs', () => {
5576
const spy1 = jest.fn();
5677
const ref1 = createCallbackRef<HTMLDivElement>(spy1);
5778
const spy2 = jest.fn();
5879
const ref2 = createCallbackRef<HTMLDivElement>(spy2);
5980
const ref3 = createRef() as React.MutableRefObject<any>;
6081
const ref4 = jest.fn();
6182

62-
const TestComponent = () => (
63-
<div
64-
ref={mergeRefs([
65-
ref1,
66-
ref2,
67-
ref3,
68-
ref4,
69-
])}
70-
>test</div>
71-
);
83+
const TestComponent = () => <div ref={mergeRefs([ref1, ref2, ref3, ref4])}>test</div>;
7284

73-
mount(<TestComponent/>);
85+
mount(<TestComponent />);
7486

7587
const ref = ref1.current;
7688
expect(ref).not.toBe(null);
@@ -85,21 +97,28 @@ describe('Specs', () => {
8597
const spy = jest.fn();
8698
const ref = createRef<HTMLDivElement>();
8799
let counter = 0;
100+
88101
const Test = () => {
89102
const x = counter++;
90103
const mref = mergeRefs<HTMLDivElement>([spy, ref]);
91-
return <div key={x < 1 ? '1' : '2'} ref={mref}>test</div>;
104+
105+
return (
106+
<div key={x < 1 ? '1' : '2'} ref={mref}>
107+
test
108+
</div>
109+
);
92110
};
93-
const wrapper = mount(<Test/>);
111+
112+
const wrapper = mount(<Test />);
94113
expect(spy).not.toBeCalledWith(null);
95114
expect(spy).toBeCalledWith(ref.current);
96115
expect(spy).toHaveBeenCalledTimes(1);
97116

98-
wrapper.setProps({x: 42});
117+
wrapper.setProps({ x: 42 });
99118
expect(spy).toBeCalledWith(null);
100119
expect(spy).toBeCalledWith(ref.current);
101120
expect(spy).toHaveBeenCalledTimes(3);
102-
})
121+
});
103122
});
104123

105124
describe('transformRef', () => {
@@ -117,28 +136,21 @@ describe('Specs', () => {
117136
const ref4 = refToCallback<HTMLDivElement>(spy4);
118137
const ref4s = useRefToCallback<HTMLDivElement>(ref4t);
119138

120-
return (
121-
<div
122-
ref={useMergeRefs([
123-
ref1,
124-
ref3,
125-
ref4,
126-
ref4s
127-
])}
128-
>test</div>
129-
);
139+
return <div ref={useMergeRefs([ref1, ref3, ref4, ref4s])}>test</div>;
130140
};
131141

132-
mount(<TestComponent/>).setProps({x: 1}).update();
142+
mount(<TestComponent />)
143+
.setProps({ x: 1 })
144+
.update();
133145

134146
const ref = ref1.current;
135147
expect(ref).not.toBe(null);
136148

137149
expect(spy1).toBeCalledWith(ref, null);
138-
expect(spy2).toBeCalledWith("test", null);
150+
expect(spy2).toBeCalledWith('test', null);
139151
expect(ref4t.current).toBe(ref);
140152
expect(spy4).toBeCalledWith(ref);
141-
})
153+
});
142154
});
143155

144156
describe('edge cases', () => {
@@ -150,13 +162,16 @@ describe('Specs', () => {
150162

151163
it('merging null refs', () => {
152164
const ref1 = createRef();
165+
153166
const TestComponent = () => {
154167
const ref = useMergeRefs([null, ref1]);
155168
ref.current = 'xx';
169+
156170
return null;
157171
};
158-
mount(<TestComponent/>);
159-
expect(ref1.current).toBe('xx')
172+
173+
mount(<TestComponent />);
174+
expect(ref1.current).toBe('xx');
160175
});
161-
})
176+
});
162177
});

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@
3131
},
3232
"devDependencies": {
3333
"@theuiteam/lib-builder": "^0.1.4",
34-
"@size-limit/preset-small-lib": "^2.1.6"
34+
"@size-limit/preset-small-lib": "^2.1.6",
35+
"@types/enzyme-adapter-react-16": "^1.0.6",
36+
"enzyme-adapter-react-16": "^1.15.6"
3537
},
3638
"peerDependencies": {
3739
"react": "^16.8.0 || ^17.0.0 || ^18.0.0",

src/createRef.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export function createCallbackRef<T>(callback: (newValue: T | null, lastValue: T
1515
get current() {
1616
return current;
1717
},
18-
set current(value: T) {
18+
set current(value: T | null) {
1919
const last = current;
2020

2121
if (last !== value) {

src/refToCallback.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ReactRef, RefCallback } from './types';
1+
import { DefinedReactRef, ReactRef, RefCallback } from './types';
22

33
/**
44
* Unmemoized version of {@link useRefToCallback}
@@ -18,13 +18,15 @@ export function refToCallback<T>(ref: ReactRef<T>): RefCallback<T> {
1818
const nullCallback = (): any => null;
1919
// lets maintain a weak ref to, well, ref :)
2020
// not using `kashe` to keep this package small
21-
const weakMem = new WeakMap<ReactRef<any>, RefCallback<any>>();
21+
const weakMem = new WeakMap<DefinedReactRef<any>, RefCallback<any>>();
2222

23-
const weakMemoize = (ref: ReactRef<any>) => {
23+
const weakMemoize = (ref: ReactRef<any>): RefCallback<any> => {
2424
const usedRef = ref || nullCallback;
2525

26-
if (weakMem.has(usedRef)) {
27-
return weakMem.get(usedRef);
26+
const storedRef = weakMem.get(usedRef);
27+
28+
if (storedRef) {
29+
return storedRef;
2830
}
2931

3032
const cb = refToCallback(usedRef);

src/transformRef.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,15 @@ import { assignRef } from './assignRef';
22
import { createCallbackRef } from './createRef';
33
import { ReactRef, RefObject } from './types';
44

5-
export function transformRef<T, K>(ref: ReactRef<K>, transformer: (original: T) => K): RefObject<T> {
5+
/**
6+
* Transforms one ref to another
7+
* @example
8+
* ```tsx
9+
* const ResizableWithRef = forwardRef((props, ref) =>
10+
* <Resizable {...props} ref={transformRef(ref, i => i ? i.resizable : null)}/>
11+
* );
12+
* ```
13+
*/
14+
export function transformRef<T, K>(ref: ReactRef<K>, transformer: (original: T | null) => K): RefObject<T> {
615
return createCallbackRef<T>((value) => assignRef(ref, transformer(value)));
716
}

src/types.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ import * as React from 'react';
33
export type RefCallback<T> = (newValue: T | null) => void;
44
export type RefObject<T> = React.MutableRefObject<T | null>;
55

6-
export type ReactRef<T> = RefCallback<T> | RefObject<T> | null;
6+
export type DefinedReactRef<T> = RefCallback<T> | RefObject<T>;
7+
export type ReactRef<T> = DefinedReactRef<T> | null;

0 commit comments

Comments
 (0)