Skip to content
This repository has been archived by the owner on Feb 23, 2024. It is now read-only.

Commit

Permalink
Add validation to local pickup fields (#8007)
Browse files Browse the repository at this point in the history
* add form validation for admin screen

* add types

* add validation to fields

* restore form ref
  • Loading branch information
senadir authored Dec 22, 2022
1 parent 09a3988 commit 1bbd7ab
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { __ } from '@wordpress/i18n';
* Internal dependencies
*/
import formStepAttributes from '../../form-step/attributes';
import { defaultShippingText, defaultLocalPickupText } from './constants';

export default {
...formStepAttributes( {
Expand All @@ -30,11 +31,11 @@ export default {
},
localPickupText: {
type: 'string',
default: __( 'Local Pickup', 'woo-gutenberg-products-block' ),
default: defaultLocalPickupText,
},
shippingText: {
type: 'string',
default: __( 'Shipping', 'woo-gutenberg-products-block' ),
default: defaultShippingText,
},
lock: {
type: 'object',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { Icon, store, shipping } from '@wordpress/icons';
import './style.scss';
import { RatePrice, getLocalPickupPrices, getShippingPrices } from './shared';
import type { minMaxPrices } from './shared';
import { defaultLocalPickupText, defaultShippingText } from './constants';

const LocalPickupSelector = ( {
checked,
Expand Down Expand Up @@ -144,7 +145,7 @@ const Block = ( {
rate={ getShippingPrices( shippingRates[ 0 ]?.shipping_rates ) }
showPrice={ showPrice }
showIcon={ showIcon }
toggleText={ shippingText }
toggleText={ shippingText || defaultShippingText }
/>
<LocalPickupSelector
checked={ checked }
Expand All @@ -154,7 +155,7 @@ const Block = ( {
multiple={ shippingRates.length > 1 }
showPrice={ showPrice }
showIcon={ showIcon }
toggleText={ localPickupText }
toggleText={ localPickupText || defaultLocalPickupText }
/>
</RadioGroup>
);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* External dependencies
*/
import { __ } from '@wordpress/i18n';

export const defaultLocalPickupText = __(
'Local Pickup',
'woo-gutenberg-products-block'
);

export const defaultShippingText = __(
'Shipping',
'woo-gutenberg-products-block'
);
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import {
import { RatePrice, getLocalPickupPrices, getShippingPrices } from './shared';
import type { minMaxPrices } from './shared';
import './style.scss';
import { defaultShippingText, defaultLocalPickupText } from './constants';

const LocalPickupSelector = ( {
checked,
Expand Down Expand Up @@ -71,6 +72,7 @@ const LocalPickupSelector = ( {
) }
<RichText
value={ toggleText }
placeholder={ defaultLocalPickupText }
tagName="span"
className="wc-block-checkout__shipping-method-option-title"
onChange={ ( value ) =>
Expand Down Expand Up @@ -133,6 +135,7 @@ const ShippingSelector = ( {
) }
<RichText
value={ toggleText }
placeholder={ defaultShippingText }
tagName="span"
className="wc-block-checkout__shipping-method-option-title"
onChange={ ( value ) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ const Form = ( {
value={ values.name }
onChange={ setLocationField( 'name' ) }
autoComplete="off"
required={ true }
/>
<TextControl
label={ __( 'Address', 'woo-gutenberg-products-block' ) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,12 @@ const EditLocation = ( {
<Button
variant="primary"
onClick={ () => {
onSave( values );
onClose();
const form =
formRef?.current as unknown as HTMLFormElement;
if ( form.reportValidity() ) {
onSave( values );
onClose();
}
} }
>
{ __( 'Done', 'woo-gutenberg-products-block' ) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ const GeneralSettings = () => {
onChange={ setSettingField( 'title' ) }
disabled={ false }
autoComplete="off"
required={ true }
/>
<CheckboxControl
checked={ showCosts }
Expand Down
10 changes: 9 additions & 1 deletion assets/js/extensions/shipping-methods/pickup-location/save.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,15 @@ const SaveSettings = () => {
variant="primary"
isBusy={ isSaving }
disabled={ isSaving }
onClick={ save }
onClick={ (
event: React.MouseEvent< HTMLButtonElement, MouseEvent >
) => {
const target = event.target as HTMLButtonElement;
if ( target?.form?.reportValidity() ) {
save();
}
} }
type="submit"
>
{ __( 'Save changes', 'woo-gutenberg-products-block' ) }
</Button>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import LocationSettings from './location-settings';
import SaveSettings from './save';
import { SettingsProvider } from './settings-context';

const SettingsWrapper = styled.div`
const SettingsWrapper = styled.form`
margin: 48px auto 0;
max-width: 1032px;
display: flex;
Expand Down

0 comments on commit 1bbd7ab

Please sign in to comment.