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

Move TextInput, ValidatedTextInput and ValidationInputError to the @woocommerce/blocks-components package. #11654

Merged
merged 9 commits into from
Nov 14, 2023
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { SelectControl } from 'wordpress-components';
import type { SelectControl as SelectControlType } from '@wordpress/components';
import { useEffect } from '@wordpress/element';
import classnames from 'classnames';
import { ValidationInputError } from '@woocommerce/blocks-checkout';
import { ValidationInputError } from '@woocommerce/blocks-components';
import { VALIDATION_STORE_KEY } from '@woocommerce/block-data';
import { useDispatch, useSelect } from '@wordpress/data';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/**
* External dependencies
*/
import { isPostcode } from '@woocommerce/blocks-checkout';
import {
ValidatedTextInput,
isPostcode,
type ValidatedTextInputHandle,
} from '@woocommerce/blocks-checkout';
} from '@woocommerce/blocks-components';
import {
BillingCountryInput,
ShippingCountryInput,
Expand Down
16 changes: 8 additions & 8 deletions assets/js/base/components/cart-checkout/totals/coupon/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ import { withInstanceId } from '@wordpress/compose';
import {
ValidatedTextInput,
ValidationInputError,
} from '@woocommerce/blocks-checkout';
} from '@woocommerce/blocks-components';
import { useSelect } from '@wordpress/data';
import { VALIDATION_STORE_KEY } from '@woocommerce/block-data';
import classnames from 'classnames';
import type { MouseEvent } from 'react';
import type { MouseEvent, MouseEventHandler } from 'react';

/**
* Internal dependencies
Expand Down Expand Up @@ -62,18 +62,18 @@ export const TotalsCoupon = ( {
validationErrorId: store.getValidationErrorId( textInputId ),
};
} );
const handleCouponAnchorClick = (
e: MouseEvent< HTMLAnchorElement, MouseEvent >
const handleCouponAnchorClick: MouseEventHandler< HTMLAnchorElement > = (
e: MouseEvent< HTMLAnchorElement >
) => {
e.preventDefault();
setIsCouponFormHidden( false );
};
const handleCouponSubmit = (
e: MouseEvent< HTMLButtonElement, MouseEvent >
const handleCouponSubmit: MouseEventHandler< HTMLButtonElement > = (
e: MouseEvent< HTMLButtonElement >
) => {
e.preventDefault();
if ( onSubmit !== undefined ) {
onSubmit( couponValue ).then( ( result ) => {
if ( typeof onSubmit !== 'undefined' ) {
onSubmit( couponValue )?.then( ( result ) => {
if ( result ) {
setCouponValue( '' );
setIsCouponFormHidden( true );
Expand Down
2 changes: 1 addition & 1 deletion assets/js/base/components/combobox/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { __ } from '@wordpress/i18n';
import { useEffect, useRef } from '@wordpress/element';
import { withInstanceId } from '@wordpress/compose';
import { ComboboxControl } from 'wordpress-components';
import { ValidationInputError } from '@woocommerce/blocks-checkout';
import { ValidationInputError } from '@woocommerce/blocks-components';
import { isObject } from '@woocommerce/types';
import { useDispatch, useSelect } from '@wordpress/data';
import { VALIDATION_STORE_KEY } from '@woocommerce/block-data';
Expand Down
2 changes: 1 addition & 1 deletion assets/js/base/components/state-input/state-input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { __ } from '@wordpress/i18n';
import { decodeEntities } from '@wordpress/html-entities';
import { useCallback, useMemo, useEffect, useRef } from '@wordpress/element';
import classnames from 'classnames';
import { ValidatedTextInput } from '@woocommerce/blocks-checkout';
import { ValidatedTextInput } from '@woocommerce/blocks-components';

/**
* Internal dependencies
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
PAYMENT_STORE_KEY,
CART_STORE_KEY,
} from '@woocommerce/block-data';
import { ValidationInputError } from '@woocommerce/blocks-checkout';
import { ValidationInputError } from '@woocommerce/blocks-components';

/**
* Internal dependencies
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,13 @@ import {
import { getSetting } from '@woocommerce/settings';
import {
CheckboxControl,
ValidatedTextInput,
StoreNoticesContainer,
} from '@woocommerce/blocks-checkout';
import { ValidatedTextInput } from '@woocommerce/blocks-components';
import { useDispatch, useSelect } from '@wordpress/data';
import { CHECKOUT_STORE_KEY } from '@woocommerce/block-data';
import { isEmail } from '@wordpress/url';

/**
* Internal dependencies
*/

const Block = (): JSX.Element => {
const { customerId, shouldCreateAccount } = useSelect( ( select ) => {
const store = select( CHECKOUT_STORE_KEY );
Expand Down
2 changes: 1 addition & 1 deletion assets/js/blocks/checkout/phone-number/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* External dependencies
*/
import { __ } from '@wordpress/i18n';
import { ValidatedTextInput } from '@woocommerce/blocks-checkout';
import { ValidatedTextInput } from '@woocommerce/blocks-components';

/**
* Renders a phone number input.
Expand Down
2 changes: 1 addition & 1 deletion assets/js/types/type-defs/payment-method-interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import type PaymentMethodLabel from '@woocommerce/base-components/cart-checkout/payment-method-label';
import type PaymentMethodIcons from '@woocommerce/base-components/cart-checkout/payment-method-icons';
import type LoadingMask from '@woocommerce/base-components/loading-mask';
import type { ValidationInputError } from '@woocommerce/blocks-checkout';
import type { ValidationInputError } from '@woocommerce/blocks-components';

/**
* Internal dependencies
Expand Down
8 changes: 2 additions & 6 deletions packages/checkout/components/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@ export { default as Button } from './button';
export { default as Label } from './label';
export { default as StoreNoticesContainer } from './store-notices-container';
export { default as CheckboxControl } from './checkbox-control';
export {
default as ValidatedTextInput,
ValidatedTextInputHandle,
} from './text-input/validated-text-input';
export { default as TextInput } from './text-input/text-input';
export { default as ValidationInputError } from './validation-input-error';
export { ValidationInputError } from './validation-input-error';
export { ValidatedTextInput, TextInput } from './text-input';
export { default as StoreNotice } from './store-notice';
2 changes: 2 additions & 0 deletions packages/checkout/components/text-input/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { default as TextInput } from '../../../components/text-input/text-input';
export { default as ValidatedTextInput } from '../../../components/text-input/validated-text-input';
47 changes: 1 addition & 46 deletions packages/checkout/components/validation-input-error/index.tsx
Original file line number Diff line number Diff line change
@@ -1,46 +1 @@
/**
* External dependencies
*/
import { useSelect } from '@wordpress/data';
import { VALIDATION_STORE_KEY } from '@woocommerce/block-data';

/**
* Internal dependencies
*/
import './style.scss';

interface ValidationInputErrorProps {
errorMessage?: string;
propertyName?: string;
elementId?: string;
}

export const ValidationInputError = ( {
errorMessage = '',
propertyName = '',
elementId = '',
}: ValidationInputErrorProps ): JSX.Element | null => {
const { validationError, validationErrorId } = useSelect( ( select ) => {
const store = select( VALIDATION_STORE_KEY );
return {
validationError: store.getValidationError( propertyName ),
validationErrorId: store.getValidationErrorId( elementId ),
};
} );

if ( ! errorMessage || typeof errorMessage !== 'string' ) {
if ( validationError?.message && ! validationError?.hidden ) {
errorMessage = validationError.message;
} else {
return null;
}
}

return (
<div className="wc-block-components-validation-error" role="alert">
<p id={ validationErrorId }>{ errorMessage }</p>
</div>
);
};

export default ValidationInputError;
export { ValidationInputError } from '../../../components/validation-input-error/';
6 changes: 6 additions & 0 deletions packages/components/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,10 @@ export { default as RadioControlAccordion } from './radio-control-accordion';
export { default as SortSelect } from './sort-select';
export { default as Spinner } from './spinner';
export { default as Textarea } from './textarea';
export {
default as ValidatedTextInput,
type ValidatedTextInputHandle,
} from './text-input/validated-text-input';
export { default as TextInput } from './text-input/text-input';
export { default as Title } from './title';
export { default as ValidationInputError } from './validation-input-error';
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export interface ValidatedTextInputProps
// id to use for the input. If not provided, an id will be generated.
id?: string;
// Unique instance ID. id will be used instead if provided.
instanceId: string;
instanceId?: string | undefined;
// Class name to add to the input.
className?: string | undefined;
// aria-describedby attribute to add to the input.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import { useInstanceId } from '@wordpress/compose';
import TextInput from './text-input';
import './style.scss';
import { ValidationInputError } from '../validation-input-error';
import { getValidityMessageForInput } from '../../utils';
import { getValidityMessageForInput } from '../../checkout/utils';
import { ValidatedTextInputProps } from './types';

export type ValidatedTextInputHandle = {
Expand Down
46 changes: 46 additions & 0 deletions packages/components/validation-input-error/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/**
* External dependencies
*/
import { useSelect } from '@wordpress/data';
import { VALIDATION_STORE_KEY } from '@woocommerce/block-data';

/**
* Internal dependencies
*/
import './style.scss';

interface ValidationInputErrorProps {
errorMessage?: string;
propertyName?: string;
elementId?: string;
}

export const ValidationInputError = ( {
errorMessage = '',
propertyName = '',
elementId = '',
}: ValidationInputErrorProps ): JSX.Element | null => {
const { validationError, validationErrorId } = useSelect( ( select ) => {
const store = select( VALIDATION_STORE_KEY );
return {
validationError: store.getValidationError( propertyName ),
validationErrorId: store.getValidationErrorId( elementId ),
};
} );

if ( ! errorMessage || typeof errorMessage !== 'string' ) {
if ( validationError?.message && ! validationError?.hidden ) {
errorMessage = validationError.message;
} else {
return null;
}
}

return (
<div className="wc-block-components-validation-error" role="alert">
<p id={ validationErrorId }>{ errorMessage }</p>
</div>
);
};

export default ValidationInputError;
Loading