Skip to content

Commit

Permalink
New: Introduce Section field type and replace current use of Groups (i…
Browse files Browse the repository at this point in the history
…mpress-org#4)

* feature: add Section type and update existing group references

* feature: create addFields method for more fluent Section type api

* refactor: replace append with addFields

* refactor: use append instead of addFields

Co-authored-by: Jon Waldstein <jonwaldstein@jons-air.mynetworksettings.com>
  • Loading branch information
jonwaldstein and Jon Waldstein committed May 11, 2022
1 parent 7b53b49 commit 14af62f
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 12 deletions.
14 changes: 14 additions & 0 deletions src/Framework/FieldsAPI/Section.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Give\Framework\FieldsAPI;

/**
* @unreleased
*/
class Section extends Group
{
/**
* @unreleased
*/
const TYPE = 'section';
}
14 changes: 7 additions & 7 deletions src/NextGen/DonationForm/Blocks/DonationFormBlock/Block.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
use Give\Framework\FieldsAPI\Email;
use Give\Framework\FieldsAPI\Exceptions\EmptyNameException;
use Give\Framework\FieldsAPI\Form;
use Give\Framework\FieldsAPI\Group;
use Give\Framework\FieldsAPI\Hidden;
use Give\Framework\FieldsAPI\Radio;
use Give\Framework\FieldsAPI\Section;
use Give\Framework\FieldsAPI\Text;
use Give\Framework\PaymentGateways\PaymentGateway;
use Give\Framework\PaymentGateways\PaymentGatewayRegister;
Expand Down Expand Up @@ -53,7 +53,7 @@ public function register()
* @return string
* @throws EmptyNameException
*/
public function render($attributes)
public function render(array $attributes): string
{
$donationForm = $this->createForm($attributes);

Expand Down Expand Up @@ -94,7 +94,7 @@ public function render($attributes)
* @return Form
* @throws EmptyNameException
*/
private function createForm($attributes)
private function createForm($attributes): Form
{
$gatewayOptions = [];
foreach ($this->getEnabledPaymentGateways($attributes['formId']) as $gateway) {
Expand All @@ -104,7 +104,7 @@ private function createForm($attributes)
$donationForm = new Form('DonationForm');

$donationForm->append(
Group::make('donationDetails')
Section::make('donationDetails')
->label(__('Donation Details', 'give'))
->append(
Text::make('amount')
Expand All @@ -113,7 +113,7 @@ private function createForm($attributes)
->required()
),

Group::make('donorDetails')
Section::make('donorDetails')
->label(__('Donor Details', 'give'))
->append(
Text::make('firstName')
Expand All @@ -130,7 +130,7 @@ private function createForm($attributes)
->emailTag('email')
),

Group::make('paymentDetails')
Section::make('paymentDetails')
->label(__('Payment Details', 'give'))
->append(...$gatewayOptions),

Expand All @@ -153,7 +153,7 @@ private function createForm($attributes)
/**
* @return PaymentGateway[]
*/
public function getEnabledPaymentGateways($formId)
public function getEnabledPaymentGateways($formId): array
{
$gateways = [];

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import {string} from 'joi';
import FieldInterface from '../types/FieldInterface';
import Field from './Field';

Expand All @@ -8,7 +7,7 @@ type Props = {
fields: FieldInterface[];
};

export default function FieldGroup({name, label, fields}: Props) {
export default function FieldSection({name, label, fields}: Props) {
return (
<fieldset aria-labelledby={name}>
<div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import Joi from 'joi';

import Field from '../fields/Field';
import getFieldErrorMessages from '../utilities/getFieldErrorMessages';
import FieldGroup from '../fields/FieldGroup';
import FieldSection from '../fields/FieldSection';
import axios from 'axios';
import getWindowData from '../utilities/getWindowData';
import PaymentDetails from '../fields/PaymentDetails';
Expand Down Expand Up @@ -97,8 +97,8 @@ export default function Form({fields, defaultValues, gateways}: PropTypes) {
return <PaymentDetails fields={nodes} name={name} label={label} key={name} />;
}

if (type === 'group' && nodes) {
return <FieldGroup fields={nodes} name={name} label={label} key={name} />;
if (type === 'section' && nodes) {
return <FieldSection fields={nodes} name={name} label={label} key={name} />;
}

return (
Expand Down

0 comments on commit 14af62f

Please sign in to comment.