Skip to content

Commit

Permalink
Add fix to reshow card icon if previously it failed to load (#2955)
Browse files Browse the repository at this point in the history
* Add fix to reshow card icon if previously it failed to load

* Use useState and avoid setting inline styles

* Added changeset file
  • Loading branch information
sponglord authored Nov 22, 2024
1 parent 2ebc8f3 commit b824b29
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 10 deletions.
5 changes: 5 additions & 0 deletions .changeset/strong-boxes-suffer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@adyen/adyen-web': patch
---

Add fix to reshow card icon if previously it had failed to load
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,10 @@
display: none;
}

.adyen-checkout__field--cardNumber .adyen-checkout-card-input__icon--hidden {
display: none;
}

.adyen-checkout__field--securityCode.adyen-checkout__field--error .adyen-checkout__card__cvc__hint,
.adyen-checkout__field--securityCode.adyen-checkout__field--valid .adyen-checkout__card__cvc__hint {
opacity: 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,29 @@ import { h } from 'preact';
import { getCardImageUrl, getFullBrandName } from '../utils';
import { BrandIconProps } from './types';
import useImage from '../../../../../core/Context/useImage';
import { useState } from 'preact/hooks';
import classNames from 'classnames';

export default function BrandIcon({ brand, brandsConfiguration = {} }: BrandIconProps) {
const getImage = useImage();
const imageName = brand === 'card' ? 'nocard' : brand;
const imageUrl = brandsConfiguration[brand]?.icon ?? getCardImageUrl(imageName, getImage);
const handleError = e => {
e.target.style.cssText = 'display: none';

const [hasLoaded, setHasLoaded] = useState(false);

const handleError = () => {
setHasLoaded(false);
};

return (
<img
className="adyen-checkout-card-input__icon adyen-checkout__card__cardNumber__brandIcon"
onError={handleError}
alt={getFullBrandName(brand)}
src={imageUrl}
/>
);
const handleLoad = () => {
setHasLoaded(true);
};

const fieldClassnames = classNames({
'adyen-checkout-card-input__icon': true,
'adyen-checkout__card__cardNumber__brandIcon': true,
'adyen-checkout-card-input__icon--hidden': !hasLoaded
});

return <img className={fieldClassnames} onLoad={handleLoad} onError={handleError} alt={getFullBrandName(brand)} src={imageUrl} />;
}

0 comments on commit b824b29

Please sign in to comment.