@janiscommerce/ui-native 1.12.0 → 1.13.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,8 +1,26 @@
1
1
  import React from 'react';
2
2
  import { TextInput } from 'react-native';
3
3
  import { BaseInputProps } from '../../atoms/BaseInput';
4
- interface InputProps extends BaseInputProps {
5
- type: 'currency' | 'number' | 'text' | 'email' | 'phone';
4
+ export type InputVariant = 'default' | 'weightable' | 'amountTotal' | 'currency' | 'numeric';
5
+ interface BaseInputPropsExtended extends BaseInputProps {
6
+ type?: 'currency' | 'number' | 'text' | 'email' | 'phone';
7
+ variant?: InputVariant;
8
+ onChangeText?: (text: string) => void;
9
+ totalValue?: number;
6
10
  }
11
+ type AmountTotalProps = BaseInputPropsExtended & {
12
+ variant: 'amountTotal';
13
+ totalValue: number;
14
+ };
15
+ type OtherVariantProps = BaseInputPropsExtended & {
16
+ variant: Exclude<InputVariant, 'amountTotal'>;
17
+ totalValue?: never;
18
+ };
19
+ type DefaultProps = BaseInputPropsExtended & {
20
+ variant?: never;
21
+ type: 'currency' | 'number' | 'text' | 'email' | 'phone';
22
+ totalValue?: never;
23
+ };
24
+ export type InputProps = AmountTotalProps | OtherVariantProps | DefaultProps;
7
25
  declare const Input: React.ForwardRefExoticComponent<InputProps & React.RefAttributes<TextInput>>;
8
26
  export default Input;
@@ -1,29 +1,80 @@
1
- import React, { forwardRef } from 'react';
2
- import { StyleSheet } from 'react-native';
1
+ import React, { forwardRef, useState, useRef } from 'react';
2
+ import { StyleSheet, View, TouchableWithoutFeedback, Keyboard } from 'react-native';
3
3
  import BaseInput from '../../atoms/BaseInput';
4
4
  import { palette } from '../../../theme/palette';
5
5
  import { moderateScale, scaledForDevice } from '../../../scale';
6
+ import handleChangeText from './utils/handleChangeText';
7
+ import Typography from '../../atoms/Typography';
6
8
  var InputType;
7
9
  (function (InputType) {
8
10
  InputType["currency"] = "numeric";
9
11
  InputType["number"] = "numeric";
12
+ InputType["default"] = "default";
10
13
  InputType["text"] = "default";
11
14
  InputType["email"] = "email-address";
12
15
  InputType["phone"] = "phone-pad";
16
+ InputType["weightable"] = "numeric";
17
+ InputType["amountTotal"] = "numeric";
18
+ InputType["numeric"] = "numeric";
13
19
  })(InputType || (InputType = {}));
14
- const Input = forwardRef(({ style, type, ...props }, ref) => {
20
+ const Input = forwardRef(({ style, type, variant = 'default', totalValue, onChangeText, ...props }, ref) => {
21
+ const [value, setValue] = useState('');
22
+ const isAmountTotalVariant = variant === 'amountTotal';
23
+ const internalRef = useRef(null);
24
+ const inputRef = ref ?? internalRef;
25
+ if (isAmountTotalVariant && typeof totalValue !== 'number') {
26
+ return null;
27
+ }
15
28
  const styles = StyleSheet.create({
16
- input: {
29
+ container: {
17
30
  padding: 0,
18
31
  height: scaledForDevice(70, moderateScale),
19
32
  borderColor: palette.primary.main,
20
33
  borderWidth: 2,
21
34
  borderRadius: 8,
22
- fontSize: scaledForDevice(42, moderateScale),
23
35
  backgroundColor: palette.white.light,
36
+ justifyContent: 'center',
37
+ alignItems: 'center',
38
+ flexDirection: 'row',
39
+ },
40
+ input: {
24
41
  color: palette.black.main,
42
+ fontSize: scaledForDevice(42, moderateScale),
43
+ },
44
+ totalValue: {
45
+ color: palette.primary.main,
25
46
  },
26
47
  });
27
- return (<BaseInput style={[styles.input, style]} ref={ref} keyboardType={InputType[type]} {...props}/>);
48
+ const changeTextCb = (text) => {
49
+ const transformedText = handleChangeText(text, variant);
50
+ setValue(transformedText);
51
+ if (onChangeText) {
52
+ onChangeText(transformedText);
53
+ }
54
+ };
55
+ const handlePress = () => {
56
+ // istanbul ignore next
57
+ if (inputRef && 'current' in inputRef && inputRef.current) {
58
+ Keyboard.dismiss();
59
+ inputRef.current.focus();
60
+ }
61
+ };
62
+ const resolvedKeyboardType = (() => {
63
+ if (type && type in InputType) {
64
+ return InputType[type];
65
+ }
66
+ if (variant && variant in InputType) {
67
+ return InputType[variant];
68
+ }
69
+ return InputType.default;
70
+ })();
71
+ return (<TouchableWithoutFeedback onPress={handlePress}>
72
+ <View style={styles.container}>
73
+ <BaseInput style={[styles.input, style]} ref={inputRef} value={value} keyboardType={resolvedKeyboardType} onChangeText={changeTextCb} {...props}/>
74
+ {isAmountTotalVariant && (<Typography style={styles.totalValue} type="display" size="medium">
75
+ {`/${totalValue?.toString()}`}
76
+ </Typography>)}
77
+ </View>
78
+ </TouchableWithoutFeedback>);
28
79
  });
29
80
  export default Input;
@@ -0,0 +1,3 @@
1
+ import { InputVariant } from '../../../Input';
2
+ declare const handleChangeText: (text: string, variant: InputVariant) => string;
3
+ export default handleChangeText;
@@ -0,0 +1,16 @@
1
+ const variantLogicMapper = {
2
+ default: (value) => value,
3
+ weightable: (value) => value.replace(',', '.').replace(/[^0-9.]/g, ''),
4
+ amountTotal: (value) => value.replace(',', '.').replace(/[^0-9.]/g, ''),
5
+ currency: (value) => value.replace(/[^0-9.,]/g, ''),
6
+ numeric: (value) => value.replace(/[^0-9.,]/g, ''),
7
+ };
8
+ const transformText = (text, variant) => {
9
+ const transform = variantLogicMapper[variant] || variantLogicMapper.default;
10
+ return transform(text);
11
+ };
12
+ const handleChangeText = (text, variant) => {
13
+ const transformedText = transformText(text, variant);
14
+ return transformedText;
15
+ };
16
+ export default handleChangeText;
@@ -1,28 +1,29 @@
1
+ import { moderateScale, scaledForDevice } from '../scale';
1
2
  const typography = {
2
- display: { weight: '400', size: 42, lineHeight: 50 },
3
+ display: { weight: '400', size: scaledForDevice(42, moderateScale), lineHeight: 50 },
3
4
  heading: {
4
- large: { weight: '500', size: 34, lineHeight: 40 },
5
- medium: { weight: '500', size: 26, lineHeight: 32 },
6
- small: { weight: '400', size: 24, lineHeight: 28 },
5
+ large: { weight: '500', size: scaledForDevice(34, moderateScale), lineHeight: 40 },
6
+ medium: { weight: '500', size: scaledForDevice(26, moderateScale), lineHeight: 32 },
7
+ small: { weight: '400', size: scaledForDevice(24, moderateScale), lineHeight: 28 },
7
8
  },
8
9
  title: {
9
- large: { weight: '400', size: 20, lineHeight: 24 },
10
- medium: { weight: '700', size: 18, lineHeight: 22 },
11
- small: { weight: '700', size: 14, lineHeight: 16 },
10
+ large: { weight: '400', size: scaledForDevice(20, moderateScale), lineHeight: 24 },
11
+ medium: { weight: '700', size: scaledForDevice(18, moderateScale), lineHeight: 22 },
12
+ small: { weight: '700', size: scaledForDevice(14, moderateScale), lineHeight: 16 },
12
13
  },
13
14
  label: {
14
- large: { weight: '500', size: 16, lineHeight: 18 },
15
- medium: { weight: '500', size: 14, lineHeight: 16 },
16
- small: { weight: '500', size: 12, lineHeight: 14 },
15
+ large: { weight: '500', size: scaledForDevice(16, moderateScale), lineHeight: 18 },
16
+ medium: { weight: '500', size: scaledForDevice(14, moderateScale), lineHeight: 16 },
17
+ small: { weight: '500', size: scaledForDevice(12, moderateScale), lineHeight: 14 },
17
18
  },
18
19
  body: {
19
- large: { weight: '400', size: 16, lineHeight: 20 },
20
- medium: { weight: '400', size: 14, lineHeight: 18 },
21
- small: { weight: '400', size: 12, lineHeight: 16 },
20
+ large: { weight: '400', size: scaledForDevice(16, moderateScale), lineHeight: 20 },
21
+ medium: { weight: '400', size: scaledForDevice(14, moderateScale), lineHeight: 18 },
22
+ small: { weight: '400', size: scaledForDevice(12, moderateScale), lineHeight: 16 },
22
23
  },
23
24
  overline: {
24
- large: { weight: '500', size: 14, lineHeight: 16, spacing: 1 },
25
- small: { weight: '500', size: 12, lineHeight: 14, spacing: 0.7 },
25
+ large: { weight: '500', size: scaledForDevice(14, moderateScale), lineHeight: 16, spacing: 1 },
26
+ small: { weight: '500', size: scaledForDevice(12, moderateScale), lineHeight: 14, spacing: 0.7 },
26
27
  },
27
28
  };
28
29
  export default typography;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@janiscommerce/ui-native",
3
- "version": "1.12.0",
3
+ "version": "1.13.1",
4
4
  "description": "components library for Janis app",
5
5
  "main": "dist/index.js",
6
6
  "typings": "dist/index.d.ts",