Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/mb way international phone number #1601

Merged
merged 22 commits into from
May 23, 2022
Merged
Show file tree
Hide file tree
Changes from 21 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
589f581
First draft of new PhoneInput
sponglord May 10, 2022
a349049
Starting to sort out styling
sponglord May 10, 2022
e57cbf8
Mapping country flags, handling onChange & showValidation
sponglord May 11, 2022
6fac6a1
Correctly adding placeholders and labels to PhoneInput comp. Removed …
sponglord May 11, 2022
aa3784f
MBWayInput - removing unnecessary useForm fny. MBWay - expect data pr…
sponglord May 11, 2022
2960502
Allow parent comp to pass in keys for error messages
sponglord May 11, 2022
b4e015c
Set styling related heights to 35px
sponglord May 11, 2022
d38a9e0
Moved isEmpty util to validator-utils.ts and added some other util fns
sponglord May 12, 2022
047e244
Don't show validation error on an empty phone input
sponglord May 12, 2022
d24f70f
Styling: better selector names. (Added comments)
sponglord May 12, 2022
aaaa3a4
Changed some class names. Show valid and error icons
sponglord May 12, 2022
41374fe
Removed duplicated class fro FormFields.scss
sponglord May 13, 2022
e1de229
Adjusted width of dropdown and removed inappropriate 'field' string f…
sponglord May 13, 2022
849e8c1
Adding more new look BEM class names
sponglord May 13, 2022
688240d
Concatenate pref and number into one value for paymentMethod data
sponglord May 13, 2022
0a39fcb
Fully validate a portuguese mobile number
sponglord May 13, 2022
31b00d1
Remove console.log
sponglord May 13, 2022
f2730f0
Fixing types
sponglord May 13, 2022
ddcb24c
Increased sf version to 3.10.2
sponglord May 16, 2022
71fcd4c
Show spinner whilst phonenumbers dataset loads
sponglord May 16, 2022
a61ce2f
Turned mechanism for fetching phonePrefixes into a custom hook
sponglord May 23, 2022
afa3321
handling error differently
sponglord May 23, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ValidatorRules } from '../../../../utils/Validator/types';
import { formatCPFCNPJ } from '../../../internal/SocialSecurityNumberBrazil/utils';
import validateSSN from '../../../internal/SocialSecurityNumberBrazil/validate';
import { isEmpty } from '../../../internal/SecuredFields/lib/utilities/commonUtils';
import { isEmpty } from '../../../../utils/validator-utils';

export const cardInputFormatters = {
socialSecurityNumber: formatCPFCNPJ
Expand Down
7 changes: 4 additions & 3 deletions packages/lib/src/components/MBWay/MBWay.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@ export class MBWayElement extends UIElement {
return {
...props,
data: {
telephoneNumber: data.telephoneNumber || data.phoneNumber
phoneNumber: data.telephoneNumber || data.phoneNumber,
phonePrefix: data.phonePrefix || '+351' // if not specified default to Portuguese country code
},
placeholders: {
telephoneNumber: placeholders.telephoneNumber || placeholders.phoneNumber || '+351 932 123 456'
phoneNumber: placeholders.telephoneNumber || placeholders.phoneNumber || '932123456'
}
};
}
Expand All @@ -29,7 +30,7 @@ export class MBWayElement extends UIElement {
return {
paymentMethod: {
type: MBWayElement.type,
...(this.state.data?.telephoneNumber && { telephoneNumber: this.state.data.telephoneNumber })
...(this.state.data?.phoneNumber && { telephoneNumber: this.state.data.phonePrefix + this.state.data.phoneNumber })
}
};
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,69 +1,50 @@
import { h } from 'preact';
import { useState, useEffect } from 'preact/hooks';
import classNames from 'classnames';
import { useState, useRef } from 'preact/hooks';
import useCoreContext from '../../../../core/Context/useCoreContext';
import Field from '../../../internal/FormFields/Field';
import { renderFormField } from '../../../internal/FormFields';
import { MBWayDataState, MBWayInputProps } from './types';
import { MBWayInputProps } from './types';
import './MBWayInput.scss';
import useForm from '../../../../utils/useForm';
const phoneNumberRegEx = /^[+]*[0-9]{1,4}[\s/0-9]*$/;
import PhoneInput from '../../../internal/PhoneInputNew';
import AdyenCheckoutError from '../../../../core/Errors/AdyenCheckoutError';
import LoadingWrapper from '../../../internal/LoadingWrapper';
import usePhonePrefixes from '../../../internal/PhoneInputNew/usePhonePrefixes';

function MBWayInput(props: MBWayInputProps) {
const { i18n } = useCoreContext();
const { i18n, loadingContext } = useCoreContext();

const { handleChangeFor, triggerValidation, data, valid, errors, isValid } = useForm<MBWayDataState>({
schema: ['telephoneNumber'],
defaultData: props.data,
rules: {
telephoneNumber: {
validate: num => phoneNumberRegEx.test(num) && num.length >= 7,
errorMessage: 'mobileNumber.invalid',
modes: ['blur']
}
},
formatters: {
telephoneNumber: num => num.replace(/[^0-9+\s]/g, '')
}
});
const phoneInputRef = useRef(null);

const [status, setStatus] = useState('ready');
const { allowedCountries = [] } = props;

const [status, setStatus] = useState<string>('ready');

this.setStatus = setStatus;
this.showValidation = triggerValidation;
this.showValidation = phoneInputRef?.current?.triggerValidation;

const { loadingStatus: prefixLoadingStatus, phonePrefixes, error } = usePhonePrefixes({ allowedCountries, loadingContext });

if (error) {
props.onError(new AdyenCheckoutError('ERROR', error));
}
sponglord marked this conversation as resolved.
Show resolved Hide resolved

useEffect(() => {
const onChange = ({ data, valid, errors, isValid }) => {
props.onChange({ data, valid, errors, isValid });
}, [data, valid, errors, isValid]);
};

return (
<div className="adyen-checkout__mb-way">
<Field
errorMessage={!!errors.telephoneNumber && i18n.get('mobileNumber.invalid')}
label={i18n.get('mobileNumber')}
className={classNames('adyen-checkout__input--phone-number')}
isValid={valid.telephoneNumber}
dir={'ltr'}
name={'telephoneNumber'}
>
{renderFormField('tel', {
value: data.telephoneNumber,
className: 'adyen-checkout__pm__phoneNumber__input',
placeholder: props.placeholders.telephoneNumber,
required: true,
autoCorrect: 'off',
onBlur: handleChangeFor('telephoneNumber', 'blur'),
onInput: handleChangeFor('telephoneNumber', 'input')
})}
</Field>
<LoadingWrapper status={prefixLoadingStatus}>
<div className="adyen-checkout__mb-way">
<PhoneInput {...props} items={phonePrefixes} ref={phoneInputRef} onChange={onChange} data={props.data} />

{props.showPayButton && props.payButton({ status, label: i18n.get('confirmPurchase') })}
</div>
{props.showPayButton && props.payButton({ status, label: i18n.get('confirmPurchase') })}
</div>
</LoadingWrapper>
);
}

MBWayInput.defaultProps = {
onChange: () => {}
onChange: () => {},
phoneNumberKey: 'mobileNumber',
phoneNumberErrorKey: 'mobileNumber.invalid'
ribeiroguilherme marked this conversation as resolved.
Show resolved Hide resolved
};

export default MBWayInput;
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,17 @@ import { UIElementProps } from '../../../types';

export interface MBWayInputData {
telephoneNumber?: string;
phoneNumber?: string;
phonePrefix?: string;
}

export interface MBWayInputProps extends UIElementProps {
data?: MBWayInputData;
placeholders?: MBWayInputData;
onChange: (state) => void;
allowedCountries?: string[];
requiredFields?: string[];
phoneNumberKey?: string;
}

export interface MBWayDataState {
Expand Down
2 changes: 1 addition & 1 deletion packages/lib/src/components/internal/Address/validate.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ValidatorRules, ValidatorRule } from '../../../utils/Validator/types';
import { countrySpecificFormatters } from './validate.formats';
import { ERROR_CODES, ERROR_MSG_INCOMPLETE_FIELD } from '../../../core/Errors/constants';
import { isEmpty } from '../SecuredFields/lib/utilities/commonUtils';
import { isEmpty } from '../../../utils/validator-utils';

const createPatternByDigits = (digits: number) => {
return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,10 +136,6 @@
color: $color-alert;
}

.adyen-checkout__input--invalid {
border-color: $color-alert;
}

.adyen-checkout__input--valid {
border-bottom-color: $color-success;
}
Expand Down
82 changes: 82 additions & 0 deletions packages/lib/src/components/internal/PhoneInputNew/PhoneInput.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
.adyen-checkout-phone-input--new {
direction: ltr;

// inherited style (FormFields.scss)
.adyen-checkout__input-wrapper {
width: 100%;

// inherited style (FormFields.scss)
.adyen-checkout__input {
padding: 0;
height: auto;

&:focus-within {
border: 1px solid #06f;

.adyen-checkout-dropdown--countrycode-selector {
border-right: 1px solid #06f;
}
}
}

// inherited style (Select.scss)
.adyen-checkout__dropdown__button {
width: auto;
border: 0;
height: 35px;
border-top-right-radius: 0;
border-bottom-right-radius: 0;

&:after {
box-sizing: revert;
height: 10px;
}
}

// inherited style (Select.scss)
.adyen-checkout__dropdown__button--active {
box-shadow: none;

&:hover {
box-shadow: none;
}
}

// Example of better BEM naming i.e. 'adyen-checkout-input' as the Base
.adyen-checkout-input--phone-number {
height: 35px;
line-height: 35px;
min-height: 35px;
padding-bottom: 0;
padding-top: 0;
border: 1px solid transparent;
padding-left: 15px;

&:focus-within {
border: 1px solid #06f;
box-shadow: 0 0 0 2px #99c2ff;
}
}

.adyen-checkout-dropdown--countrycode-selector {
border-right: 1px solid #dce0e5;
min-width: 134px;
width: 134px;
}

.adyen-checkout-input-holder--phone-input {
align-items: center;
display: flex;
}

.adyen-checkout-phone-number {
align-items: center;
display: flex;
flex: 3;
}
}

.adyen-checkout-phone-input__error-holder {
margin-top: -10px;
}
}
Loading