REF: QRCodeComponent from js to tsx

This commit is contained in:
Marcos Rodriguez Velez 2023-03-23 22:42:56 -04:00
parent 56a8c2028d
commit 77daaf50e5

View file

@ -4,12 +4,58 @@ import QRCode from 'react-native-qrcode-svg';
import ToolTipMenu from './TooltipMenu'; import ToolTipMenu from './TooltipMenu';
import Share from 'react-native-share'; import Share from 'react-native-share';
import loc from '../loc'; import loc from '../loc';
import PropTypes from 'prop-types';
import Clipboard from '@react-native-clipboard/clipboard'; import Clipboard from '@react-native-clipboard/clipboard';
import { useTheme } from '@react-navigation/native'; import { useTheme } from '@react-navigation/native';
const QRCodeComponent = ({ interface QRCodeComponentProps {
value, value: string;
isLogoRendered?: boolean;
isMenuAvailable?: boolean;
logoSize?: number;
size?: number;
ecl?: 'H' | 'Q' | 'M' | 'L';
onError?: () => void;
}
interface ActionIcons {
iconType: 'SYSTEM';
iconValue: string;
}
interface ActionType {
Share: 'share';
Copy: 'copy';
}
interface Action {
id: string;
text: string;
icon: ActionIcons;
}
const actionKeys: ActionType = {
Share: 'share',
Copy: 'copy',
};
interface ActionIcons {
iconType: 'SYSTEM';
iconValue: string;
}
const actionIcons: { [key: string]: ActionIcons } = {
Share: {
iconType: 'SYSTEM',
iconValue: 'square.and.arrow.up',
},
Copy: {
iconType: 'SYSTEM',
iconValue: 'doc.on.doc',
},
};
const QRCodeComponent: React.FC<QRCodeComponentProps> = ({
value = '',
isLogoRendered = true, isLogoRendered = true,
isMenuAvailable = true, isMenuAvailable = true,
logoSize = 90, logoSize = 90,
@ -17,39 +63,39 @@ const QRCodeComponent = ({
ecl = 'H', ecl = 'H',
onError = () => {}, onError = () => {},
}) => { }) => {
const qrCode = useRef(); const qrCode = useRef<any>();
const { colors } = useTheme(); const { colors } = useTheme();
const handleShareQRCode = () => { const handleShareQRCode = () => {
qrCode.current.toDataURL(data => { qrCode.current.toDataURL((data: string) => {
const shareImageBase64 = { const shareImageBase64 = {
url: `data:image/png;base64,${data}`, url: `data:image/png;base64,${data}`,
}; };
Share.open(shareImageBase64).catch(error => console.log(error)); Share.open(shareImageBase64).catch((error: any) => console.log(error));
}); });
}; };
const onPressMenuItem = id => { const onPressMenuItem = (id: string) => {
if (id === QRCodeComponent.actionKeys.Share) { if (id === actionKeys.Share) {
handleShareQRCode(); handleShareQRCode();
} else if (id === QRCodeComponent.actionKeys.Copy) { } else if (id === actionKeys.Copy) {
qrCode.current.toDataURL(Clipboard.setImage); qrCode.current.toDataURL(Clipboard.setImage);
} }
}; };
const menuActions = () => { const menuActions = (): Action[] => {
const actions = []; const actions: Action[] = [];
if (Platform.OS === 'ios' || Platform.OS === 'macos') { if (Platform.OS === 'ios' || Platform.OS === 'macos') {
actions.push({ actions.push({
id: QRCodeComponent.actionKeys.Copy, id: actionKeys.Copy,
text: loc.transactions.details_copy, text: loc.transactions.details_copy,
icon: QRCodeComponent.actionIcons.Copy, icon: actionIcons.Copy,
}); });
} }
actions.push({ actions.push({
id: QRCodeComponent.actionKeys.Share, id: actionKeys.Share,
text: loc.receive.details_share, text: loc.receive.details_share,
icon: QRCodeComponent.actionIcons.Share, icon: actionIcons.Share,
}); });
return actions; return actions;
}; };
@ -61,10 +107,11 @@ const QRCodeComponent = ({
size={size} size={size}
logoSize={logoSize} logoSize={logoSize}
color="#000000" color="#000000"
// @ts-ignore: logoBackgroundColor is not in the type definition
logoBackgroundColor={colors.brandingColor} logoBackgroundColor={colors.brandingColor}
backgroundColor="#FFFFFF" backgroundColor="#FFFFFF"
ecl={ecl} ecl={ecl}
getRef={qrCode} getRef={(c: any) => (qrCode.current = c)}
onError={onError} onError={onError}
/> />
); );
@ -87,29 +134,3 @@ export default QRCodeComponent;
const styles = StyleSheet.create({ const styles = StyleSheet.create({
qrCodeContainer: { borderWidth: 6, borderRadius: 8, borderColor: '#FFFFFF' }, qrCodeContainer: { borderWidth: 6, borderRadius: 8, borderColor: '#FFFFFF' },
}); });
QRCodeComponent.actionKeys = {
Share: 'share',
Copy: 'copy',
};
QRCodeComponent.actionIcons = {
Share: {
iconType: 'SYSTEM',
iconValue: 'square.and.arrow.up',
},
Copy: {
iconType: 'SYSTEM',
iconValue: 'doc.on.doc',
},
};
QRCodeComponent.propTypes = {
value: PropTypes.string.isRequired,
isMenuAvailable: PropTypes.bool,
size: PropTypes.number,
ecl: PropTypes.string,
isLogoRendered: PropTypes.bool,
onError: PropTypes.func,
logoSize: PropTypes.number,
};