forked from jasminmif/react-interactive-paycard
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
154 additions
and
106 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"prettier.singleQuote": true, | ||
"javascript.preferences.quoteStyle": "single", | ||
"prettier.tabWidth": 4 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,109 +1,109 @@ | ||
import React, { useState, useRef } from 'react'; | ||
import CForm from './components/form'; | ||
import Card from './components/card'; | ||
import React, { useState, useRef } from "react"; | ||
import CForm from "./components/form"; | ||
import Card from "./components/card"; | ||
|
||
const defaultCardNo = '#### #### #### ####'; | ||
const defaultCardHolderName = 'FULL NAME'; | ||
const defaultCardMonth = ''; | ||
const defaultCardYear = ''; | ||
const defaultCardCvv = ''; | ||
const defaultCardNo = "#### #### #### ####"; | ||
const defaultCardHolderName = "FULL NAME"; | ||
const defaultCardMonth = ""; | ||
const defaultCardYear = ""; | ||
const defaultCardCvv = ""; | ||
|
||
const MainScreen = () => { | ||
const initialState = { | ||
cardNumber: defaultCardNo, | ||
cardHolder: defaultCardHolderName, | ||
cardMonth: defaultCardMonth, | ||
cardYear: defaultCardYear, | ||
cardCvv: defaultCardCvv, | ||
isCardFlipped: false, | ||
currentFocusedElm: null | ||
}; | ||
const [state, setState] = useState(initialState); | ||
const initialState = { | ||
cardNumber: defaultCardNo, | ||
cardHolder: defaultCardHolderName, | ||
cardMonth: defaultCardMonth, | ||
cardYear: defaultCardYear, | ||
cardCvv: defaultCardCvv, | ||
isCardFlipped: false, | ||
currentFocusedElm: null | ||
}; | ||
const [state, setState] = useState(initialState); | ||
|
||
const updateStateValue = ({ name, value }) => { | ||
setState({ | ||
...state, | ||
[name]: value || initialState[name] | ||
}); | ||
}; | ||
const updateStateValue = ({ name, value }) => { | ||
setState({ | ||
...state, | ||
[name]: value || initialState[name] | ||
}); | ||
}; | ||
|
||
const { | ||
cardNumber, | ||
cardHolder, | ||
cardMonth, | ||
cardYear, | ||
cardCvv, | ||
isCardFlipped | ||
} = state; | ||
let { currentFocusedElm } = state; | ||
const { | ||
cardNumber, | ||
cardHolder, | ||
cardMonth, | ||
cardYear, | ||
cardCvv, | ||
isCardFlipped | ||
} = state; | ||
let { currentFocusedElm } = state; | ||
|
||
// References for the Form Inputs | ||
let formFieldsRefObj = { | ||
cardNumber: useRef(), | ||
cardHolder: useRef(), | ||
cardDate: useRef(), | ||
cardCvv: useRef() | ||
}; | ||
// References for the Form Inputs | ||
let formFieldsRefObj = { | ||
cardNumber: useRef(), | ||
cardHolder: useRef(), | ||
cardDate: useRef(), | ||
cardCvv: useRef() | ||
}; | ||
|
||
let onCardElementClick = key => { | ||
focusFormFieldByKey(key); | ||
}; | ||
let onCardElementClick = key => { | ||
focusFormFieldByKey(key); | ||
}; | ||
|
||
let focusFormFieldByKey = key => { | ||
formFieldsRefObj[key].current.focus(); | ||
}; | ||
let focusFormFieldByKey = key => { | ||
formFieldsRefObj[key].current.focus(); | ||
}; | ||
|
||
// This are the references for the Card DIV elements | ||
let cardElementsRef = { | ||
cardNumber: null, | ||
cardHolder: null, | ||
cardDate: null | ||
}; | ||
// This are the references for the Card DIV elements | ||
let cardElementsRef = { | ||
cardNumber: null, | ||
cardHolder: null, | ||
cardDate: null | ||
}; | ||
|
||
let onCardFormInputFocus = (_event, inputName) => { | ||
setState({ | ||
...state, | ||
currentFocusedElm: cardElementsRef[inputName] | ||
}); | ||
}; | ||
let onCardFormInputFocus = (_event, inputName) => { | ||
setState({ | ||
...state, | ||
currentFocusedElm: cardElementsRef[inputName] | ||
}); | ||
}; | ||
|
||
let onCardInputBlur = event => { | ||
setState({ | ||
...state, | ||
currentFocusedElm: null | ||
}); | ||
}; | ||
let onCardInputBlur = event => { | ||
setState({ | ||
...state, | ||
currentFocusedElm: null | ||
}); | ||
}; | ||
|
||
return ( | ||
<div className="wrapper"> | ||
<CForm | ||
onUpdateStateValue={updateStateValue} | ||
cardNumberRef={formFieldsRefObj.cardNumber} | ||
cardHolderRef={formFieldsRefObj.cardHolder} | ||
cardDateRef={formFieldsRefObj.cardDate} | ||
onCardInputFocus={onCardFormInputFocus} | ||
onCardInputBlur={onCardInputBlur} | ||
> | ||
<Card | ||
cardNumber={cardNumber} | ||
cardHolder={cardHolder} | ||
cardMonth={cardMonth} | ||
cardYear={cardYear} | ||
cardCvv={cardCvv} | ||
isCardFlipped={isCardFlipped} | ||
currentFocusedElm={currentFocusedElm} | ||
onCardElementClick={onCardElementClick} | ||
cardNumberRef={node => | ||
(cardElementsRef['cardNumber'] = node) | ||
} | ||
cardHolderRef={node => | ||
(cardElementsRef['cardHolder'] = node) | ||
} | ||
cardDateRef={node => (cardElementsRef['cardDate'] = node)} | ||
></Card> | ||
</CForm> | ||
</div> | ||
); | ||
return ( | ||
<div className="wrapper"> | ||
<CForm | ||
onUpdateStateValue={updateStateValue} | ||
cardNumberRef={formFieldsRefObj.cardNumber} | ||
cardHolderRef={formFieldsRefObj.cardHolder} | ||
cardDateRef={formFieldsRefObj.cardDate} | ||
onCardInputFocus={onCardFormInputFocus} | ||
onCardInputBlur={onCardInputBlur} | ||
> | ||
<Card | ||
cardNumber={cardNumber} | ||
cardHolder={cardHolder} | ||
cardMonth={cardMonth} | ||
cardYear={cardYear} | ||
cardCvv={cardCvv} | ||
isCardFlipped={isCardFlipped} | ||
currentFocusedElm={currentFocusedElm} | ||
onCardElementClick={onCardElementClick} | ||
cardNumberRef={node => | ||
(cardElementsRef["cardNumber"] = node) | ||
} | ||
cardHolderRef={node => | ||
(cardElementsRef["cardHolder"] = node) | ||
} | ||
cardDateRef={node => (cardElementsRef["cardDate"] = node)} | ||
></Card> | ||
</CForm> | ||
</div> | ||
); | ||
}; | ||
|
||
export default MainScreen; |