Skip to content

Commit

Permalink
Refactor: Update DonationAmountLevels template to support descriptions (
Browse files Browse the repository at this point in the history
  • Loading branch information
pauloiankoski committed Apr 24, 2024
1 parent ffdbe2b commit 0d1ec0c
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ import classNames from 'classnames';
type DonationAmountLevelsProps = {
name: string;
currency: string;
levels: number[];
levels: {label: string; value: number}[];
onLevelClick?: (amount: number) => void;
};

/**
* @unreleased add level descriptions.
* @since 3.0.0
*/
export default function DonationAmountLevels({
Expand All @@ -23,24 +24,51 @@ export default function DonationAmountLevels({
const amount = useWatch({name});
const formatter = useCurrencyFormatter(currency);

const groupedLevels = levels.reduce(
(acc, level) => {
const key = level.label ? 'labeled' : 'unlabeled';
acc[key].push(level);
return acc;
},
{labeled: [], unlabeled: []}
);

const allLevels = [...groupedLevels.labeled, ...groupedLevels.unlabeled];

return (
<div className="givewp-fields-amount__levels-container">
{levels.map((levelAmount, index) => {
const label = formatter.format(levelAmount);
const selected = levelAmount === amount;
<div
className={classNames('givewp-fields-amount__levels-container', {
'givewp-fields-amount__levels-container--has-descriptions': groupedLevels.labeled.length > 0,
})}
>
{allLevels.map((level, index) => {
const label = formatter.format(level.value);
const selected = level.value === amount;
const hasDescription = level.label;

return (
<button
className={classNames('givewp-fields-amount__level', {
'givewp-fields-amount__level--selected': selected,
<div
className={classNames('givewp-fields-amount__level-container', {
'givewp-fields-amount__level-container--col': hasDescription,
})}
type="button"
onClick={() => {
onLevelClick(levelAmount);
}}
key={index}
>
{label}
</button>
<button
className={classNames('givewp-fields-amount__level', {
'givewp-fields-amount__level--selected': selected,
'givewp-fields-amount__level--description': hasDescription,
})}
type="button"
onClick={() => {
onLevelClick(level.value);
}}
key={index}
>
{label}
</button>
{hasDescription && (
<span className={'givewp-fields-amount__level__description'}>{level.label}</span>
)}
</div>
);
})}
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ export default function Amount({
fixedAmountValue,
allowCustomAmount,
messages,
descriptions,
descriptionsEnabled
}: AmountProps) {
const isFixedAmount = !allowLevels;
const [customAmountValue, setCustomAmountValue] = useState<string>(
Expand Down Expand Up @@ -76,6 +78,8 @@ export default function Amount({
resetCustomAmount();
setValue(name, levelAmount);
}}
descriptions={descriptions}
descriptionsEnabled={descriptionsEnabled}
/>
)}

Expand Down
39 changes: 37 additions & 2 deletions src/DonationForms/resources/styles/components/_amount.scss
Original file line number Diff line number Diff line change
Expand Up @@ -114,15 +114,50 @@ $borderColor: #9A9A9A;

&__levels-container {
display: grid;
grid-auto-rows: 1fr;
grid-template-columns: repeat(2, 1fr);
gap: var(--givewp-spacing-2);
grid-template-rows: repeat(2, 1fr);
inline-size: 100%;
list-style: none;

@media screen and (min-width: variables.$givewp-breakpoint-sm) {
grid-template-columns: repeat(3, 1fr);
}

&--has-descriptions {
display: flex;
flex-wrap: wrap;
}

.givewp-fields-amount__level-container {
display: flex;
align-items: center;
flex: 1;
gap: var(--givewp-spacing-4);
min-width: calc((100% - var(--givewp-spacing-2)) / 2);

@media screen and (min-width: variables.$givewp-breakpoint-sm) {
min-width: calc((100% - var(--givewp-spacing-2) * 2) / 3);
}

&--col {
flex-basis: 100%;
}
;

.givewp-fields-amount__level {
height: 100%;

&--description {
max-width: 144px;
}
}

.givewp-fields-amount__description {
margin-bottom: 0;
color: var(--givewp-grey-700);
font-size: 1rem;
}
}
}

&__level {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@ import type {subscriptionPeriod} from '@givewp/forms/registrars/templates/groups
export interface DonationAmountAttributes {
label: string;
levels: OptionProps[];
descriptions: string[];
descriptionsEnabled: boolean;
defaultLevel: number;
priceOption: string;
setPrice: number;
customAmount: boolean;
Expand Down

0 comments on commit 0d1ec0c

Please sign in to comment.