Skip to content

Commit

Permalink
All UIElements can receive defaultProps based on regulations
Browse files Browse the repository at this point in the history
  • Loading branch information
sponglord committed Feb 9, 2024
1 parent 6cb28d4 commit cd6b003
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 2 deletions.
6 changes: 5 additions & 1 deletion packages/lib/src/components/internal/UIElement/UIElement.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,11 @@ export abstract class UIElement<P extends UIElementProps = UIElementProps> exten
...componentProps
};

this.props = this.formatProps({ ...this.constructor['defaultProps'], ...finalProps });
this.props = this.formatProps({
...this.constructor['defaultProps'], // component defaults
...this.core.getRegulatoryDefaults(), // regulatory defaults
...finalProps // the rest (inc. merchant defined config)
});
}

protected storeElementRefOnCore(props?: P) {
Expand Down
6 changes: 5 additions & 1 deletion packages/lib/src/core/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { resolveEnvironment, resolveCDNEnvironment, resolveAnalyticsEnvironment
import Analytics from './Analytics';
import { AdditionalDetailsStateData, PaymentAction, PaymentResponseData } from '../types/global-types';
import { CoreConfiguration, ICore } from './types';
import { processGlobalOptions } from './utils';
import { getDefaultPropsByCountryCode, processGlobalOptions } from './utils';
import Session from './CheckoutSession';
import { hasOwnProperty } from '../utils/hasOwnProperty';
import { Resources } from './Context/Resources';
Expand Down Expand Up @@ -264,6 +264,10 @@ class Core implements ICore {
};
}

public getRegulatoryDefaults(): Record<string, any> {
return getDefaultPropsByCountryCode(this.options.countryCode);
}

public storeElementReference(element: UIElement) {
if (element) {
this.components.push(element);
Expand Down
2 changes: 2 additions & 0 deletions packages/lib/src/core/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ export interface ICore {

storeElementReference(element: UIElement): void;

getRegulatoryDefaults(): Record<string, any>;

options: CoreConfiguration;
paymentMethodsResponse: PaymentMethods;
session?: Session;
Expand Down
13 changes: 13 additions & 0 deletions packages/lib/src/core/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,16 @@ export function processGlobalOptions(globalOptions) {
return r;
}, {});
}

export function getDefaultPropsByCountryCode(countryCode: string): Record<string, any> {
switch (countryCode) {
// Finnish regulations state that no payment method can be open by default
case 'FI':
return {
openFirstPaymentMethod: false,
openFirstStoredPaymentMethod: false
};
default:
return {};
}
}

0 comments on commit cd6b003

Please sign in to comment.