Skip to content

Commit 407f084

Browse files
committed
Use arrow fn. syntax
1 parent e128c1f commit 407f084

File tree

10 files changed

+53
-80
lines changed

10 files changed

+53
-80
lines changed

src/Cursor.tsx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import type {FC} from 'react';
12
import {useCallback, useState} from 'react';
23

34
import {useInterval} from './useTimer';
@@ -10,20 +11,20 @@ export interface CursorProps {
1011
delay?: number;
1112
}
1213

13-
export function Cursor({
14+
export const Cursor: FC<CursorProps> = ({
1415
cursorSymbol = DEFAULT_CURSOR_SYMBOL,
1516
delay = DEFAULT_BLINKING_SPEED,
16-
}: CursorProps) {
17+
}) => {
1718
'use memo';
1819
const [visibleFlag, setFlag] = useState(true);
1920

20-
const toggleVisibility = useCallback(function toggleCursorVisibility() {
21+
const toggleVisibility = useCallback(() => {
2122
setFlag((prev) => !prev);
2223
}, []);
2324

2425
useInterval(toggleVisibility, delay);
2526

2627
return <>{visibleFlag ? cursorSymbol : ''}</>;
27-
}
28+
};
2829

2930
Cursor.displayName = 'Cursor';

src/MaskSymbol.tsx

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type {ReactNode} from 'react';
1+
import type {FC, PropsWithChildren, ReactNode} from 'react';
22
import {useCallback, useEffect, useState} from 'react';
33

44
import {useTimeout} from './useTimer';
@@ -7,21 +7,20 @@ export const DEFAULT_BLINKING_SPEED = 500;
77

88
interface MaskSymbolProps {
99
delay?: number;
10-
children: ReactNode;
1110
maskSymbol: ReactNode;
1211
isLastFilledCell: boolean;
1312
}
1413

15-
export function MaskSymbol({
14+
export const MaskSymbol: FC<PropsWithChildren<MaskSymbolProps>> = ({
1615
isLastFilledCell,
1716
children: symbol,
1817
maskSymbol,
1918
delay = DEFAULT_BLINKING_SPEED,
20-
}: MaskSymbolProps) {
19+
}) => {
2120
'use memo';
2221
const [visibleFlag, setFlag] = useState(true);
2322

24-
const toggleVisibility = useCallback(function toggleMaskVisibility() {
23+
const toggleVisibility = useCallback(() => {
2524
setFlag((prev) => !prev);
2625
}, []);
2726

@@ -35,6 +34,6 @@ export function MaskSymbol({
3534
}, [isLastFilledCell]);
3635

3736
return <>{visibleFlag ? symbol : maskSymbol}</>;
38-
}
37+
};
3938

4039
MaskSymbol.displayName = 'MaskSymbol';

src/__tests__/CodeField.test.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,8 @@ it('should invoke renderCell for third cell when isFocused and it empty', () =>
109109
);
110110

111111
console.log(
112-
// @ts-expect-error - number is not function
113112
<CodeField
113+
// @ts-expect-error - number is not function
114114
onChangeText={123}
115115
renderCell={() => null}
116116
ref={(ref: TextInput | null) => {

src/useBlurOnFulfill.ts

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import type {RefObject} from 'react';
12
import {useEffect, useRef} from 'react';
23
import type {TextInput} from 'react-native';
34

@@ -6,21 +7,18 @@ interface Options {
67
cellCount: number;
78
}
89

9-
export function useBlurOnFulfill<TInput extends TextInput>({
10+
export const useBlurOnFulfill = <TInput extends TextInput>({
1011
value,
1112
cellCount,
12-
}: Options) {
13+
}: Options): RefObject<TInput | null> => {
1314
'use memo';
1415
const inputRef = useRef<TInput | null>(null);
1516

16-
useEffect(
17-
function blurOnFulfillEffect() {
18-
if (value?.length === cellCount) {
19-
inputRef.current?.blur();
20-
}
21-
},
22-
[value, cellCount],
23-
);
17+
useEffect(() => {
18+
if (value?.length === cellCount) {
19+
inputRef.current?.blur();
20+
}
21+
}, [value, cellCount]);
2422

2523
return inputRef;
26-
}
24+
};

src/useClearByFocusCell.common.ts

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ interface Coords {
1616
locationY: number;
1717
}
1818

19-
function findIndex({locationX, locationY}: Coords, map: LayoutsMap): number {
19+
const findIndex = ({locationX, locationY}: Coords, map: LayoutsMap): number => {
2020
for (const index in map) {
2121
const layout = map[index];
2222
if (
@@ -29,7 +29,7 @@ function findIndex({locationX, locationY}: Coords, map: LayoutsMap): number {
2929
}
3030
}
3131
return -1;
32-
}
32+
};
3333

3434
export interface Params {
3535
setValue(text: string): void;
@@ -47,9 +47,7 @@ export const useClearByFocusCellCommon = (params: Params) => {
4747
paramsRef.current = params;
4848
}, [params]);
4949

50-
const clearCodeByCoords = useCallback(function clearCodeByCoords(
51-
coords: Coords,
52-
): void {
50+
const clearCodeByCoords = useCallback((coords: Coords): void => {
5351
const index = findIndex(coords, cellsLayouts.current);
5452
if (index !== -1) {
5553
const {value, setValue} = paramsRef.current;
@@ -58,13 +56,9 @@ export const useClearByFocusCellCommon = (params: Params) => {
5856
}
5957
}, []);
6058

61-
const getCellOnLayoutHandler = useCallback(function getCellOnLayoutHandler(
62-
index: number,
63-
) {
59+
const getCellOnLayoutHandler = useCallback((index: number) => {
6460
if (!callbackRef.current[index]) {
65-
callbackRef.current[index] = function onCellLayout(
66-
event: LayoutChangeEvent,
67-
) {
61+
callbackRef.current[index] = (event: LayoutChangeEvent): void => {
6862
const {width, height, x, y} = event.nativeEvent.layout;
6963
cellsLayouts.current[`${index}`] = {
7064
x,

src/useClearByFocusCell.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,7 @@ export const useClearByFocusCell = (params: Params) => {
1212

1313
const inputProps = useMemo(
1414
() => ({
15-
onPressOut: function onInputPressOut(
16-
event: NativeSyntheticEvent<NativeTouchEvent>,
17-
): void {
15+
onPressOut: (event: NativeSyntheticEvent<NativeTouchEvent>): void => {
1816
clearCodeByCoords(event.nativeEvent);
1917
},
2018
}),

src/useClearByFocusCell.web.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export const useClearByFocusCell = (params: Params) => {
1414
(): {
1515
onClick: ComponentProps<'input'>['onClick'];
1616
} => ({
17-
onClick: function onInputClick(event): void {
17+
onClick: (event): void => {
1818
// @ts-expect-error - getClientRects have to be defined on the target
1919
const [offset] = event.target.getClientRects() as [
2020
{left: number; top: number},

src/useFocusState.ts

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,34 @@
11
import {useCallback, useDebugValue, useState} from 'react';
2-
import type {
3-
NativeSyntheticEvent,
4-
TextInputFocusEventData,
5-
TextInputProps,
6-
} from 'react-native';
2+
import type {BlurEvent, FocusEvent, TextInputProps} from 'react-native';
73

8-
export function useFocusState(
4+
export const useFocusState = (
95
onBlurFromProps?: TextInputProps['onBlur'],
106
onFocusFromProps?: TextInputProps['onFocus'],
11-
) {
7+
) => {
128
'use memo';
139

1410
const [isFocused, setFocusFlag] = useState(false);
1511

1612
const onBlur = useCallback(
17-
function onInputBlur(
18-
blurEvent: NativeSyntheticEvent<TextInputFocusEventData>,
19-
) {
13+
(blurEvent: BlurEvent) => {
2014
setFocusFlag(false);
2115
onBlurFromProps?.(blurEvent);
2216
},
2317
[onBlurFromProps],
2418
);
2519

2620
const onFocus = useCallback(
27-
function onInputFocus(
28-
focusEvent: NativeSyntheticEvent<TextInputFocusEventData>,
29-
) {
21+
(focusEvent: FocusEvent) => {
3022
setFocusFlag(true);
3123
onFocusFromProps?.(focusEvent);
3224
},
3325
[onFocusFromProps],
3426
);
3527

36-
useDebugValue(isFocused ? 'Focused' : 'Unfocused');
28+
if (__DEV__) {
29+
// eslint-disable-next-line react-hooks/rules-of-hooks
30+
useDebugValue(isFocused ? 'Focused' : 'Unfocused');
31+
}
3732

3833
return [isFocused, onFocus, onBlur] as const;
39-
}
34+
};

src/useTimer.ts

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,17 @@
11
import {useEffect} from 'react';
22

3-
export function useInterval(callback: () => void, delay: number): void {
3+
export const useInterval = (callback: () => void, delay: number): void => {
44
'use memo';
5-
useEffect(
6-
function setIntervalEffect() {
7-
const timer = setInterval(callback, delay);
8-
return function cleanupIntervalTimer() {
9-
clearInterval(timer);
10-
};
11-
},
12-
[callback, delay],
13-
);
14-
}
5+
useEffect(() => {
6+
const timer = setInterval(callback, delay);
7+
return () => clearInterval(timer);
8+
}, [callback, delay]);
9+
};
1510

16-
export function useTimeout(callback: () => void, delay: number): void {
11+
export const useTimeout = (callback: () => void, delay: number): void => {
1712
'use memo';
18-
useEffect(
19-
function setTimeoutEffect() {
20-
const timer = setTimeout(callback, delay);
21-
return function cleanupTimeoutTimer() {
22-
clearTimeout(timer);
23-
};
24-
},
25-
[callback, delay],
26-
);
27-
}
13+
useEffect(() => {
14+
const timer = setTimeout(callback, delay);
15+
return () => clearTimeout(timer);
16+
}, [callback, delay]);
17+
};

src/utils.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
1-
export function isLastFilledCell(
1+
export const isLastFilledCell = (
22
value: string | null | undefined,
33
index: number,
4-
): boolean {
5-
return value != null && value.length - 2 === index;
6-
}
4+
): boolean => value != null && value.length - 2 === index;
75

8-
export function getSymbols(codeValue: string, codeLength: number): string[] {
6+
export const getSymbols = (codeValue: string, codeLength: number): string[] => {
97
const symbols = new Array<string>(codeLength);
108
for (let i = 0; i < codeLength; i++) {
119
symbols[i] = codeValue[i] ?? '';
1210
}
1311
return symbols;
14-
}
12+
};

0 commit comments

Comments
 (0)