|
| 1 | +import * as React from 'react'; |
| 2 | +import { ScrollView } from 'react-native'; |
| 3 | +import { |
| 4 | + Space, |
| 5 | + Card, |
| 6 | + Cell, |
| 7 | + TextInput, |
| 8 | + Blank, |
| 9 | + Button, |
| 10 | + Selector, |
| 11 | +} from '@fruits-chain/react-native-xiaoshu'; |
| 12 | +import PrinterImin, { |
| 13 | + IminPrintAlign, |
| 14 | + IminBarcodeType, |
| 15 | + IminBarcodeTextPos, |
| 16 | +} from 'react-native-printer-imin'; |
| 17 | +export default function PrintBarCode() { |
| 18 | + |
| 19 | + //声明属性要和接口里面的属性保持一致 |
| 20 | + const [barCodeContent, setBarCodeContent] = React.useState(''); |
| 21 | + const [barCodeWidth, setBarCodeWidth] = React.useState(''); |
| 22 | + const [widthPlaceholder, setWidthPlaceholder] = React.useState('Please enter'); |
| 23 | + const [barCodeHeight, setBarCodeHeight] = React.useState(''); |
| 24 | + const [heightPlaceholder, setHeightPlaceholder] = React.useState('Please enter'); |
| 25 | + const [align, setAlign] = React.useState<number>(IminPrintAlign.left); |
| 26 | + const [position, setBarCodeHriPosition] = React.useState<number>(IminBarcodeTextPos.none); |
| 27 | + const [barCodeType, setBarCodeType] = React.useState<number>( |
| 28 | + IminBarcodeType.code39 |
| 29 | + ); |
| 30 | + |
| 31 | + const barCodeTypeMap: { [key: number]: string } = { |
| 32 | + [IminBarcodeType.upcA]: 'upcA', |
| 33 | + [IminBarcodeType.upcE]: 'upcE', |
| 34 | + [IminBarcodeType.jan13]: 'jan13', |
| 35 | + [IminBarcodeType.jan8]: 'jan8', |
| 36 | + [IminBarcodeType.code39]: 'code39', |
| 37 | + [IminBarcodeType.itf]: 'itf', |
| 38 | + [IminBarcodeType.codabar]: 'codabar', |
| 39 | + [IminBarcodeType.code93]: 'code93', |
| 40 | + [IminBarcodeType.code128]: 'code128', |
| 41 | + }; |
| 42 | + |
| 43 | + return ( |
| 44 | + <ScrollView> |
| 45 | + <Space direction="vertical"> |
| 46 | + <Card> |
| 47 | + <Cell |
| 48 | + title="BarCode Content" |
| 49 | + vertical |
| 50 | + value={ |
| 51 | + <TextInput |
| 52 | + value={barCodeContent} |
| 53 | + onChange={(value) => setBarCodeContent(value)} |
| 54 | + placeholder="Please enter " |
| 55 | + textAlign="left" |
| 56 | + /> |
| 57 | + } |
| 58 | + /> |
| 59 | + <Cell |
| 60 | + title="BarCode Width" |
| 61 | + vertical |
| 62 | + value={ |
| 63 | + <TextInput |
| 64 | + value={barCodeWidth} |
| 65 | + onChange={(value) => { |
| 66 | + if (value === '') { |
| 67 | + setBarCodeWidth(''); |
| 68 | + return; |
| 69 | + } |
| 70 | + const newValue = parseInt(value, 10); |
| 71 | + if (!isNaN(newValue) && newValue >= 1 && newValue <= 6) { |
| 72 | + setBarCodeWidth(newValue.toString()); |
| 73 | + } |
| 74 | + |
| 75 | + }} |
| 76 | + placeholder={widthPlaceholder} |
| 77 | + onFocus={() => setWidthPlaceholder('(1 <= width <= 6)')} |
| 78 | + onBlur={() => setWidthPlaceholder('Please enter')} |
| 79 | + textAlign="left" |
| 80 | + /> |
| 81 | + } |
| 82 | + /> |
| 83 | + <Cell |
| 84 | + title="BarCode Height" |
| 85 | + vertical |
| 86 | + value={ |
| 87 | + <TextInput |
| 88 | + value={barCodeHeight} |
| 89 | + onChange={(value) => { |
| 90 | + if (value === '') { |
| 91 | + setBarCodeHeight(''); |
| 92 | + return; |
| 93 | + } |
| 94 | + const newValue = parseInt(value, 10); |
| 95 | + if (!isNaN(newValue) && newValue <= 255) { |
| 96 | + setBarCodeHeight(newValue.toString()); |
| 97 | + } |
| 98 | + }} |
| 99 | + placeholder={heightPlaceholder} |
| 100 | + onFocus={() => setHeightPlaceholder('(24 <= height <= 255)')} |
| 101 | + onBlur={() => setHeightPlaceholder('Please enter')} |
| 102 | + textAlign="left" |
| 103 | + /> |
| 104 | + } |
| 105 | + /> |
| 106 | + |
| 107 | + <Cell |
| 108 | + title="BarCode Type" |
| 109 | + vertical |
| 110 | + value={barCodeTypeMap[barCodeType] || 'code39'} |
| 111 | + isLink |
| 112 | + onPress={() => { |
| 113 | + Selector({ |
| 114 | + title: 'Please select', |
| 115 | + options: [ |
| 116 | + { |
| 117 | + label: 'upcA', |
| 118 | + value: IminBarcodeType.upcA, |
| 119 | + }, |
| 120 | + { |
| 121 | + label: 'upcE', |
| 122 | + value: IminBarcodeType.upcE, |
| 123 | + }, |
| 124 | + { |
| 125 | + label: 'jan13', |
| 126 | + value: IminBarcodeType.jan13, |
| 127 | + }, |
| 128 | + { |
| 129 | + label: 'jan8', |
| 130 | + value: IminBarcodeType.jan8, |
| 131 | + }, |
| 132 | + { |
| 133 | + label: 'code39', |
| 134 | + value: IminBarcodeType.code39, |
| 135 | + }, |
| 136 | + { |
| 137 | + label: 'itf', |
| 138 | + value: IminBarcodeType.itf, |
| 139 | + }, |
| 140 | + { |
| 141 | + label: 'codabar', |
| 142 | + value: IminBarcodeType.codabar, |
| 143 | + }, |
| 144 | + { |
| 145 | + label: 'code93', |
| 146 | + value: IminBarcodeType.code93, |
| 147 | + }, |
| 148 | + { |
| 149 | + label: 'code128', |
| 150 | + value: IminBarcodeType.code128, |
| 151 | + }, |
| 152 | + ], |
| 153 | + value: barCodeType, |
| 154 | + cancellable: true, |
| 155 | + }).then((k) => { |
| 156 | + setBarCodeType(k as number); |
| 157 | + }); |
| 158 | + }} |
| 159 | + /> |
| 160 | + <Cell |
| 161 | + title="BarCode HRI Position" |
| 162 | + vertical |
| 163 | + value={ |
| 164 | + position |
| 165 | + ? position === IminBarcodeTextPos.aboveText |
| 166 | + ? 'aboveText' |
| 167 | + : position === IminBarcodeTextPos.belowText |
| 168 | + ? 'belowText' |
| 169 | + : 'both' |
| 170 | + : 'None' |
| 171 | + } |
| 172 | + isLink |
| 173 | + onPress={() => { |
| 174 | + Selector({ |
| 175 | + title: 'Please select', |
| 176 | + options: [ |
| 177 | + { |
| 178 | + label: 'None', |
| 179 | + value: IminBarcodeTextPos.none, |
| 180 | + }, |
| 181 | + { |
| 182 | + label: 'aboveText', |
| 183 | + value: IminBarcodeTextPos.aboveText, |
| 184 | + }, |
| 185 | + { |
| 186 | + label: 'belowText', |
| 187 | + value: IminBarcodeTextPos.belowText, |
| 188 | + }, |
| 189 | + { |
| 190 | + label: 'both', |
| 191 | + value: IminBarcodeTextPos.both, |
| 192 | + }, |
| 193 | + ], |
| 194 | + value: position, |
| 195 | + cancellable: true, |
| 196 | + }).then((k) => { |
| 197 | + setBarCodeHriPosition(k as number); |
| 198 | + }); |
| 199 | + }} |
| 200 | + /> |
| 201 | + |
| 202 | + <Cell |
| 203 | + title="Align" |
| 204 | + vertical |
| 205 | + value={ |
| 206 | + align |
| 207 | + ? align === IminPrintAlign.center |
| 208 | + ? 'center' |
| 209 | + : 'right' |
| 210 | + : 'left' |
| 211 | + } |
| 212 | + isLink |
| 213 | + onPress={() => { |
| 214 | + Selector({ |
| 215 | + title: 'Please select', |
| 216 | + options: [ |
| 217 | + { |
| 218 | + label: 'Left', |
| 219 | + value: IminPrintAlign.left, |
| 220 | + }, |
| 221 | + { |
| 222 | + label: 'Center', |
| 223 | + value: IminPrintAlign.center, |
| 224 | + }, |
| 225 | + { |
| 226 | + label: 'Right', |
| 227 | + value: IminPrintAlign.right, |
| 228 | + }, |
| 229 | + ], |
| 230 | + value: align, |
| 231 | + cancellable: true, |
| 232 | + }).then((k) => { |
| 233 | + setAlign(k as number); |
| 234 | + }); |
| 235 | + }} |
| 236 | + /> |
| 237 | + |
| 238 | + |
| 239 | + <Blank top={10} bottom={10} /> |
| 240 | + <Button |
| 241 | + type="primary" |
| 242 | + text="Print BarCode" |
| 243 | + onPress={() => { |
| 244 | + const width = barCodeWidth ? Number(barCodeWidth) : 2; |
| 245 | + const height = barCodeHeight ? Number(barCodeHeight) : 70; |
| 246 | + PrinterImin.setBarCodeWidth(width); |
| 247 | + PrinterImin.setBarCodeHeight(height); |
| 248 | + |
| 249 | + PrinterImin.printBarCode(barCodeType,barCodeContent,{ |
| 250 | + position, |
| 251 | + align, |
| 252 | + }); |
| 253 | + |
| 254 | + PrinterImin.printAndFeedPaper(70); |
| 255 | + }} |
| 256 | + /> |
| 257 | + </Card> |
| 258 | + </Space> |
| 259 | + </ScrollView> |
| 260 | + ); |
| 261 | +} |
0 commit comments