Skip to content

Commit 418858f

Browse files
authored
Merge pull request #209 from skyflowapi/sk-235-readme-and-sample-for-input-formatting
SK-235 Add Readme and Sample for Input formatting
2 parents afe16e3 + 145c5ec commit 418858f

3 files changed

Lines changed: 191 additions & 11 deletions

File tree

README.md

Lines changed: 44 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,8 @@ Along with Collect Element we can define other options which takes a object of o
323323
const options = {
324324
required: false, // Optional, indicates whether the field is marked as required. Defaults to 'false'.
325325
enableCardIcon: true, // Optional, indicates whether card icon should be enabled (only applicable for CARD_NUMBER ElementType).
326-
format: String, // Optional, format for the element (only applicable currently for EXPIRATION_DATE ElementType).
326+
format: String, // Optional, format for the element.
327+
translation: {}, // Optional, indicates the allowed data type value for format.
327328
enableCopy: false, // Optional, enables the copy icon in collect and reveal elements to copy text to clipboard. Defaults to 'false').
328329
allowedFileType: string[], // Optional, allowed extensions for the file to be uploaded.
329330
cardMetadata: {}, // Optional, metadata to control card number element behavior. (only applicable for CARD_NUMBER ElementType).
@@ -336,25 +337,57 @@ const options = {
336337

337338
- `enableCardIcon` parameter indicates whether the icon is visible for the CARD_NUMBER element, defaults to true
338339

339-
- `format` parameter takes string value and indicates the format pattern applicable to the element type, It's currently only applicable to `EXPIRATION_DATE` and `EXPIRATION_YEAR` element types.
340340

341341
- `enableCopy` parameter indicates whether the copy icon is visible in collect and reveal elements.
342342

343343
- `allowedFileType` parameter indicates the allowedFileType extensions to be uploaded.
344344

345-
The values that are accepted for `EXPIRATION_DATE` are
345+
- `format`: A string value that indicates the format pattern applicable to the element type.
346+
Only applicable to EXPIRATION_DATE, CARD_NUMBER, EXPIRATION_YEAR, and INPUT_FIELD elements.
346347

347-
- `MM/YY` (default)
348-
- `MM/YYYY`
349-
- `YY/MM`
350-
- `YYYY/MM`
348+
- For INPUT_FIELD elements,
349+
- the length of `format` determines the expected length of the user input.
350+
- if `translation` isn't specified, the `format` value is considered a string literal.
351351

352-
The values that are accepted for `EXPIRATION_YEAR` are
352+
`translation`: An object of key value pairs, where the key is a character that appears in `format` and the value is a simple regex pattern of acceptable inputs for that character. Each key can only appear once. Only applicable for INPUT_FIELD elements.
353353

354-
- `YY` (default)
355-
- `YYYY`
354+
Accepted values by element type:
355+
356+
| Element type | `format` and `translation` values | Examples |
357+
| --------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | ------------------------------------------------------------------------------------------------------------------------------------------ |
358+
| EXPIRATION_DATE | <li>`format`</li> <ul><li>`mm/yy` (default)</li><li>`mm/yyyy`</li><li>`yy/mm`</li><li>`yyyy/mm`</li></ul> | <ul><li>12/27</li><li>12/2027</li> <li>27/12</li> <li> 2027/12</li></ul></ul> |
359+
| EXPIRATION_YEAR | <li>`format`</li> <ul><li>`yy` (default)</li><li>`yyyy`</li></ul> | <ul><li>27</li><li>2027</li></ul> |
360+
| CARD_NUMBER | <li>`format`</li> <ul><li>`XXXX XXXX XXXX XXXX` (default)</li><li>`XXXX-XXXX-XXXX-XXXX`</li></ul> | <ul><li>1234 5678 9012 3456</li><li>1234-5678-9012-3456</li></ul> |
361+
| INPUT_FIELD | <li>`format`: A string that matches the desired output, with placeholder characters of your choice.</li><li>`translation`: An object of key/value pairs. Defaults to `{"X": "[0-9]"}`</li> | With a `format` of `+91 XXXX-XX-XXXX` and a `translation` of `{"X": "[0-9]"}`, user input of "1234121234" displays as "+91 1234-12-1234". |
362+
363+
364+
**Note:** If `format` isn't specified or receives an invalid value, it uses the element type's default value.
365+
366+
**Collect Element Options examples for INPUT_FIELD**
367+
368+
Example 1
369+
```js
370+
const options = {
371+
format:'+91 XXXX-XX-XXXX',
372+
translation: { 'X': '[0-9]' }
373+
}
374+
```
375+
376+
User input: "1234121234"
377+
Value displayed in INPUT_FIELD: "+91 1234-12-1234"
378+
379+
Example 2
380+
```js
381+
const options = {
382+
required: true,
383+
enableCardIcon: true,
384+
format: 'AY XX-XXX-XXXX',
385+
translation: { 'X': '[0-9]', 'Y': '[A-Z]' }
386+
}
387+
```
388+
User input: "B123412124"
389+
Value displayed in INPUT_FIELD: "AB 12-341-2124"
356390

357-
`NOTE`: If not specified or invalid value is passed to the `format` then it takes default value.
358391

359392
- `cardMetadata`: An object of metadata keys to control card number element behavior. It supports an optional key called `scheme`, which accepts an array of Skyflow accept card types based on which SDK will display card brand choice dropdown in the card number element. `CardType` is an enum with all skyflow supported card schemes.
360393

samples/SkyflowElements/src/App.tsx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import CardBrandChoice from './components/CardBrandChoice';
1616
import ThreeDSHelperFunctions from './components/3DSHelperFunctions';
1717
import OverrideDefaultErrors from './components/OverrideDefaultErrors'
1818
import Masking from './components/Masking';
19+
import InputFormatting from './components/InputFormatting';
1920

2021
const App = () => {
2122

@@ -67,6 +68,13 @@ const App = () => {
6768
<CustomValidations />
6869
</div>
6970
<br />
71+
<div id='Sample for Input Formatting'>
72+
<p>
73+
<b>Sample for Input Formatting</b>
74+
</p>
75+
<InputFormatting />
76+
</div>
77+
<br />
7078
<div id='Sample for Composable Elements In Modal'>
7179
<p>
7280
<b>Sample Composable Elements Dynamic properties update</b>
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
import React from 'react';
2+
import {
3+
useCollectContainer,
4+
InputFieldElement,
5+
useMakeSkyflowStyles,
6+
CardNumberElement,
7+
} from 'skyflow-react-js';
8+
9+
const InputFormatting = () => {
10+
const container = useCollectContainer();
11+
12+
const handleCollect = () => {
13+
const response = container.collect();
14+
response
15+
.then((res: unknown) => {
16+
console.log(JSON.stringify(res));
17+
})
18+
.catch((e: unknown) => {
19+
console.log(e);
20+
});
21+
};
22+
23+
const useStyles = useMakeSkyflowStyles({
24+
inputStyles: {
25+
base: {
26+
border: '1px solid black',
27+
borderRadius: '4px',
28+
color: '#1d1d1d',
29+
padding: '10px 16px',
30+
fontFamily: '"Roboto", sans-serif'
31+
},
32+
complete: {
33+
color: '#4caf50',
34+
},
35+
empty: {},
36+
focus: {},
37+
invalid: {
38+
color: '#f44336',
39+
},
40+
global: {
41+
'@import': 'url("https://fonts.googleapis.com/css2?family=Roboto&display=swap")',
42+
}
43+
},
44+
labelStyles: {
45+
base: {
46+
fontSize: '16px',
47+
fontWeight: 'bold',
48+
fontFamily: '"Roboto", sans-serif'
49+
},
50+
global: {
51+
'@import': 'url("https://fonts.googleapis.com/css2?family=Roboto&display=swap")',
52+
},
53+
requiredAsterisk: {
54+
color: 'red'
55+
}
56+
},
57+
errorTextStyles: {
58+
base: {
59+
color: 'red',
60+
fontFamily: '"Roboto", sans-serif'
61+
},
62+
global: {
63+
'@import': 'url("https://fonts.googleapis.com/css2?family=Roboto&display=swap")',
64+
},
65+
},
66+
});
67+
68+
const classes = useStyles();
69+
70+
// Phone Number (+91 XXXXX-XXXXX)
71+
const numberOptions = {
72+
format: '+91 XXXXX-XXXXX',
73+
translation: { 'X': '[0-9]' }
74+
};
75+
76+
// Email (XXXXX@XXXXX.XXX)
77+
const emailOptions = {
78+
format: 'XXXXXXXXXX@XXXXXXXXXX.XXX',
79+
translation: { 'X': '[A-Za-z0-9._%+-]' }
80+
};
81+
82+
// OTP spaced format: "XXX-XXX"
83+
const otpOptions = {
84+
format: 'XXX-XXX',
85+
translation: { 'X': '[0-9]' }
86+
};
87+
88+
return (
89+
<div className='CollectElements' style={{ width: '300px' }}>
90+
{/* Card Number (Default Format) */}
91+
<CardNumberElement
92+
id={'collectCardNumber2'}
93+
container={container}
94+
table={'table1'}
95+
classes={classes}
96+
column={'card_number'}
97+
label={'Collect Card Number'}
98+
skyflowID={'<SKYFLOW_ID>'}
99+
/>
100+
101+
{/* Number Field */}
102+
<InputFieldElement
103+
id='number'
104+
container={container}
105+
classes={classes}
106+
table={'table1'}
107+
column={'number'}
108+
label={'Number'}
109+
options={numberOptions}
110+
/>
111+
112+
{/* Email Field */}
113+
<InputFieldElement
114+
id='email'
115+
container={container}
116+
classes={classes}
117+
table={'table1'}
118+
column={'email'}
119+
label={'Email'}
120+
options={emailOptions}
121+
/>
122+
123+
124+
{/* OTP Field */}
125+
<InputFieldElement
126+
id='otp'
127+
container={container}
128+
classes={classes}
129+
table={'table1'}
130+
column={'otp'}
131+
label={'OTP'}
132+
options={otpOptions}
133+
/>
134+
<button onClick={handleCollect}>Collect</button>
135+
</div>
136+
);
137+
};
138+
139+
export default InputFormatting;

0 commit comments

Comments
 (0)