Skip to content

Commit

Permalink
Fixed typescript types not showing
Browse files Browse the repository at this point in the history
Breaking change: Use useRef<CameraApi>() instead of useRef<Camera>()
Fixed incorrect type of errorMessage
  • Loading branch information
scarlac committed May 16, 2024
1 parent a23e4d8 commit ca2bcc3
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 37 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
"main": "dist/index.js",
"module": "dist/index.js",
"source": "./src/",
"types": "dist/index.d.ts",
"react-native": "src/index",
"dependencies": {},
"license": "MIT",
"devDependencies": {
Expand Down
10 changes: 5 additions & 5 deletions src/Camera.android.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import React from 'react';
import { requireNativeComponent, findNodeHandle, NativeModules, processColor } from 'react-native';
import { CameraApi } from './types';
import { CameraProps } from './Camera';
import type { CameraApi } from './types';
import type { CameraProps } from './CameraProps';

const { RNCameraKitModule } = NativeModules;
const NativeCamera = requireNativeComponent('CKCameraManager');

const Camera = React.forwardRef((props: CameraProps, ref) => {
const nativeRef = React.useRef();
const Camera = React.forwardRef<CameraApi, CameraProps>((props, ref) => {
const nativeRef = React.useRef(null);

React.useImperativeHandle<any, CameraApi>(ref, () => ({
React.useImperativeHandle(ref, () => ({
capture: async (options = {}) => {
// Because RN doesn't support return types for ViewManager methods
// we must use the general module and tell it what View it's supposed to be using
Expand Down
18 changes: 8 additions & 10 deletions src/Camera.ios.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
import React from 'react';
import { requireNativeComponent, NativeModules } from 'react-native';
import { CameraApi } from './types';
import { CameraProps } from './Camera';
import type { CameraApi } from './types';
import type { CameraProps } from './CameraProps';

const { CKCameraManager } = NativeModules;
const NativeCamera = requireNativeComponent('CKCamera');

const Camera = React.forwardRef((props: CameraProps, ref: any) => {
const nativeRef = React.useRef();
const Camera = React.forwardRef<CameraApi, CameraProps>((props, ref) => {
const nativeRef = React.useRef(null);

React.useImperativeHandle<any, CameraApi>(ref, () => ({
props.resetFocusTimeout = props.resetFocusTimeout ?? 0;
props.resetFocusWhenMotionDetected = props.resetFocusWhenMotionDetected ?? true;

React.useImperativeHandle(ref, () => ({
capture: async () => {
return await CKCameraManager.capture({});
},
Expand All @@ -24,9 +27,4 @@ const Camera = React.forwardRef((props: CameraProps, ref: any) => {
return <NativeCamera style={{ minWidth: 100, minHeight: 100 }} ref={nativeRef} {...props} />;
});

Camera.defaultProps = {
resetFocusTimeout: 0,
resetFocusWhenMotionDetected: true,
};

export default Camera;
10 changes: 10 additions & 0 deletions src/Camera.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { lazy } from 'react';
import { Platform } from 'react-native';

const Camera = lazy(() =>
Platform.OS === 'ios'
? import('./Camera.ios')
: import('./Camera.android'),
);

export default Camera;
23 changes: 13 additions & 10 deletions src/Camera.d.ts → src/CameraProps.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
import { CameraApi, FlashMode, FocusMode, ZoomMode, TorchMode, CameraType, CodeFormat, ResizeMode } from './types';
import { type ViewProps } from 'react-native';
import {
CameraType,
type FlashMode,
type FocusMode,
type ZoomMode,
type TorchMode,
type ResizeMode,
type CodeFormat,
} from './types';
import { Orientation } from './index';

export type OnReadCodeData = {
Expand All @@ -10,7 +19,7 @@ export type OnReadCodeData = {

export type OnOrientationChangeData = {
nativeEvent: {
orientation: Orientation;
orientation: typeof Orientation;
};
};

Expand All @@ -20,9 +29,7 @@ export type OnZoom = {
};
}

export interface CameraProps {
ref?: LegacyRef<Component<CameraApi, {}, any>>;
style?: StyleProp<ViewStyle>;
export interface CameraProps extends ViewProps {
// Behavior
flashMode?: FlashMode;
focusMode?: FocusMode;
Expand Down Expand Up @@ -84,7 +91,7 @@ export interface CameraProps {
*/
onZoom?: (event: OnZoom) => void;
/** **Android only**. Triggered when camera fails to initialize */
onError?: (event: { nativeEvent: { errorMessage: number } }) => void;
onError?: (event: { nativeEvent: { errorMessage: string } }) => void;
// Barcode only
scanBarcode?: boolean;
showFrame?: boolean;
Expand All @@ -104,7 +111,3 @@ export interface CameraProps {
onCaptureButtonPressIn?: ({ nativeEvent: {} }) => void;
onCaptureButtonPressOut?: ({ nativeEvent: {} }) => void;
}

declare const Camera: React.FC<CameraProps>;

export default Camera;
14 changes: 12 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
import { NativeModules } from 'react-native';

import Camera from './Camera';
import { CameraApi, CameraType, CaptureData, FlashMode, FocusMode, TorchMode, ZoomMode, ResizeMode } from './types';
import {
CameraType,
type CameraApi,
type CaptureData,
type FlashMode,
type FocusMode,
type TorchMode,
type ZoomMode,
type ResizeMode,
} from './types';

const { CameraKit } = NativeModules;

Expand All @@ -15,4 +24,5 @@ export const Orientation = {

export default CameraKit;

export type { Camera, CameraType, TorchMode, FlashMode, FocusMode, ZoomMode, CameraApi, CaptureData, ResizeMode };
export { Camera, CameraType };
export type { TorchMode, FlashMode, FocusMode, ZoomMode, CameraApi, CaptureData, ResizeMode };
17 changes: 14 additions & 3 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,20 @@ export enum CameraType {
Back = 'back',
}

export type CodeFormat = 'code-128' | 'code-39' | 'code-93' | 'codabar' | 'ean-13' | 'ean-8' | 'itf' | 'upc-e' | 'qr' | 'pdf-417' | 'aztec' | 'data-matrix' | 'unknown';
export type CodeFormat =
| 'code-128'
| 'code-39'
| 'code-93'
| 'codabar'
| 'ean-13'
| 'ean-8'
| 'itf'
| 'upc-e'
| 'qr'
| 'pdf-417'
| 'aztec'
| 'data-matrix'
| 'unknown';

export type TorchMode = 'on' | 'off';

Expand Down Expand Up @@ -32,5 +45,3 @@ export type CameraApi = {
requestDeviceCameraAuthorization: () => Promise<boolean>;
checkDeviceCameraAuthorizationStatus: () => Promise<boolean>;
};


15 changes: 8 additions & 7 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,26 +1,27 @@
{
"extends": "@react-native/typescript-config/tsconfig.json",
"compilerOptions": {
"rootDir": "./src",
"declaration": true,
"declarationDir": "./dist/",
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"importHelpers": true,
"jsx": "react-native",
"lib": ["ES6"],
"module": "ES6",
"moduleResolution": "Node16",
"lib": ["esnext"],
"module": "esnext",
"moduleResolution": "node",
"noImplicitAny": true,
"noImplicitReturns": true,
"noImplicitThis": true,
"noUnusedLocals": false,
"outDir": "./dist",
"rootDir": "./src",
"sourceMap": true,
"removeComments": true,
"strictNullChecks": true,
"target": "ES6",
"skipLibCheck": true
"target": "esnext",
"skipLibCheck": true,
"strict": true,
"verbatimModuleSyntax": true
},
"include": ["./src/**/*.ts", "./src/**/*.tsx"],
"exclude": ["**/__tests__/*", "*.test.tsx"]
Expand Down

0 comments on commit ca2bcc3

Please sign in to comment.