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

Refactor: update Honorific block to only accept labels and add field validation #7564

Merged
merged 7 commits into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
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
14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
"@fortawesome/free-solid-svg-icons": "^5.15.1",
"@fortawesome/react-fontawesome": "^0.1.12",
"@givewp/design-system-foundation": "^1.1.0",
"@givewp/form-builder-library": "^1.6.0",
"@givewp/form-builder-library": "^1.7.0",
"@hookform/error-message": "^2.0.1",
"@hookform/resolvers": "^2.9.10",
"@paypal/paypal-js": "^5.1.4",
Expand Down
13 changes: 10 additions & 3 deletions src/DonationForms/Actions/ConvertDonationFormBlocksToFieldsApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,8 @@ protected function createNodeFromBlockWithUniqueAttributes(BlockModel $block, in
}

/**
* @unreleased updated honorific field with validation
*
* @since 3.0.0
*/
protected function createNodeFromDonorNameBlock(BlockModel $block): Node
Expand Down Expand Up @@ -293,9 +295,14 @@ protected function createNodeFromDonorNameBlock(BlockModel $block): Node


if ($block->hasAttribute('showHonorific') && $block->getAttribute('showHonorific') === true) {
$group->getNodeByName('honorific')
->label('Title')
->options(...array_values($block->getAttribute('honorifics')));
$options = array_filter(array_values((array)$block->getAttribute('honorifics')));

if (!empty($options)){
$group->getNodeByName('honorific')
->label(__('Title', 'give'))
->options(...$options)
->rules('max:255', 'in:' . implode(',', $options));
}
} else {
$group->remove('honorific');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,22 @@ import {getFormBuilderWindowData} from '@givewp/form-builder/common/getWindowDat
const titleLabelTransform = (token = '') => token.charAt(0).toUpperCase() + token.slice(1);
const titleValueTransform = (token = '') => token.trim().toLowerCase();

type Attributes = {
showHonorific: boolean;
useGlobalSettings: boolean;
honorifics: string[];
firstNameLabel: string;
firstNamePlaceholder: string;
lastNameLabel: string;
lastNamePlaceholder: string;
requireLastName: boolean;
}

export default function Edit({
attributes: {
attributes,
setAttributes,
}: BlockEditProps<any>) {
const {
showHonorific,
useGlobalSettings,
honorifics,
Expand All @@ -20,9 +34,7 @@ export default function Edit({
lastNameLabel,
lastNamePlaceholder,
requireLastName,
},
setAttributes,
}: BlockEditProps<any>) {
} = attributes as Attributes;
const [selectedTitle, setSelectedTitle] = useState<string>((Object.values(honorifics)[0] as string) ?? '');
const [honorificOptions, setHonorificOptions] = useState<OptionProps[]>(
Object.values(honorifics).map((token: string) => {
Expand Down Expand Up @@ -51,17 +63,19 @@ export default function Edit({
}

useEffect(() => {
const options = !!useGlobalSettings ? getFormBuilderWindowData().nameTitlePrefixes : ['Mr', 'Ms', 'Mrs'];
if (useGlobalSettings) {
const options = !!useGlobalSettings ? getFormBuilderWindowData().nameTitlePrefixes : ['Mr', 'Ms', 'Mrs'];

setOptions(
Object.values(options).map((token: string) => {
return {
label: titleLabelTransform(token),
value: titleValueTransform(token),
checked: selectedTitle === token,
} as OptionProps;
})
);
setOptions(
Object.values(options).map((token: string) => {
return {
label: titleLabelTransform(token),
value: titleValueTransform(token),
checked: selectedTitle === token,
} as OptionProps;
})
);
}
}, [useGlobalSettings]);

return (
Expand Down Expand Up @@ -122,7 +136,7 @@ export default function Edit({
<SelectControl
label={__('Options', 'give')}
onChange={() => setAttributes({useGlobalSettings: !useGlobalSettings})}
value={useGlobalSettings}
value={useGlobalSettings ? 'true' : 'false'}
options={[
{label: __('Global', 'give'), value: 'true'},
{label: __('Customize', 'give'), value: 'false'},
Expand Down Expand Up @@ -161,6 +175,7 @@ export default function Edit({
selectable={false}
options={honorificOptions}
setOptions={setOptions}
toggleEnabled={false}
defaultControlsTooltip={__('Title Prefixes', 'give')}
/>
</div>
Expand Down
Loading