Move `TextInput`, `ValidatedTextInput` and `ValidationInputError` to the `@woocommerce/blocks-components` package. (https://github.com/woocommerce/woocommerce-blocks/pull/11654)

This commit is contained in:
Thomas Roberts 2023-11-14 14:52:14 +00:00 committed by GitHub
parent 22bc80536f
commit cb468c8ade
20 changed files with 76 additions and 75 deletions

View File

@ -7,7 +7,7 @@ import { SelectControl } from 'wordpress-components';
import type { SelectControl as SelectControlType } from '@wordpress/components'; import type { SelectControl as SelectControlType } from '@wordpress/components';
import { useEffect } from '@wordpress/element'; import { useEffect } from '@wordpress/element';
import classnames from 'classnames'; 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 { VALIDATION_STORE_KEY } from '@woocommerce/block-data';
import { useDispatch, useSelect } from '@wordpress/data'; import { useDispatch, useSelect } from '@wordpress/data';

View File

@ -1,11 +1,11 @@
/** /**
* External dependencies * External dependencies
*/ */
import { isPostcode } from '@woocommerce/blocks-checkout';
import { import {
ValidatedTextInput, ValidatedTextInput,
isPostcode,
type ValidatedTextInputHandle, type ValidatedTextInputHandle,
} from '@woocommerce/blocks-checkout'; } from '@woocommerce/blocks-components';
import { import {
BillingCountryInput, BillingCountryInput,
ShippingCountryInput, ShippingCountryInput,

View File

@ -9,11 +9,11 @@ import { withInstanceId } from '@wordpress/compose';
import { import {
ValidatedTextInput, ValidatedTextInput,
ValidationInputError, ValidationInputError,
} from '@woocommerce/blocks-checkout'; } from '@woocommerce/blocks-components';
import { useSelect } from '@wordpress/data'; import { useSelect } from '@wordpress/data';
import { VALIDATION_STORE_KEY } from '@woocommerce/block-data'; import { VALIDATION_STORE_KEY } from '@woocommerce/block-data';
import classnames from 'classnames'; import classnames from 'classnames';
import type { MouseEvent } from 'react'; import type { MouseEvent, MouseEventHandler } from 'react';
/** /**
* Internal dependencies * Internal dependencies
@ -62,18 +62,18 @@ export const TotalsCoupon = ( {
validationErrorId: store.getValidationErrorId( textInputId ), validationErrorId: store.getValidationErrorId( textInputId ),
}; };
} ); } );
const handleCouponAnchorClick = ( const handleCouponAnchorClick: MouseEventHandler< HTMLAnchorElement > = (
e: MouseEvent< HTMLAnchorElement, MouseEvent > e: MouseEvent< HTMLAnchorElement >
) => { ) => {
e.preventDefault(); e.preventDefault();
setIsCouponFormHidden( false ); setIsCouponFormHidden( false );
}; };
const handleCouponSubmit = ( const handleCouponSubmit: MouseEventHandler< HTMLButtonElement > = (
e: MouseEvent< HTMLButtonElement, MouseEvent > e: MouseEvent< HTMLButtonElement >
) => { ) => {
e.preventDefault(); e.preventDefault();
if ( onSubmit !== undefined ) { if ( typeof onSubmit !== 'undefined' ) {
onSubmit( couponValue ).then( ( result ) => { onSubmit( couponValue )?.then( ( result ) => {
if ( result ) { if ( result ) {
setCouponValue( '' ); setCouponValue( '' );
setIsCouponFormHidden( true ); setIsCouponFormHidden( true );

View File

@ -6,7 +6,7 @@ import { __ } from '@wordpress/i18n';
import { useEffect, useRef } from '@wordpress/element'; import { useEffect, useRef } from '@wordpress/element';
import { withInstanceId } from '@wordpress/compose'; import { withInstanceId } from '@wordpress/compose';
import { ComboboxControl } from 'wordpress-components'; import { ComboboxControl } from 'wordpress-components';
import { ValidationInputError } from '@woocommerce/blocks-checkout'; import { ValidationInputError } from '@woocommerce/blocks-components';
import { isObject } from '@woocommerce/types'; import { isObject } from '@woocommerce/types';
import { useDispatch, useSelect } from '@wordpress/data'; import { useDispatch, useSelect } from '@wordpress/data';
import { VALIDATION_STORE_KEY } from '@woocommerce/block-data'; import { VALIDATION_STORE_KEY } from '@woocommerce/block-data';

View File

@ -5,7 +5,7 @@ import { __ } from '@wordpress/i18n';
import { decodeEntities } from '@wordpress/html-entities'; import { decodeEntities } from '@wordpress/html-entities';
import { useCallback, useMemo, useEffect, useRef } from '@wordpress/element'; import { useCallback, useMemo, useEffect, useRef } from '@wordpress/element';
import classnames from 'classnames'; import classnames from 'classnames';
import { ValidatedTextInput } from '@woocommerce/blocks-checkout'; import { ValidatedTextInput } from '@woocommerce/blocks-components';
/** /**
* Internal dependencies * Internal dependencies

View File

@ -16,7 +16,7 @@ import {
PAYMENT_STORE_KEY, PAYMENT_STORE_KEY,
CART_STORE_KEY, CART_STORE_KEY,
} from '@woocommerce/block-data'; } from '@woocommerce/block-data';
import { ValidationInputError } from '@woocommerce/blocks-checkout'; import { ValidationInputError } from '@woocommerce/blocks-components';
/** /**
* Internal dependencies * Internal dependencies

View File

@ -10,17 +10,13 @@ import {
import { getSetting } from '@woocommerce/settings'; import { getSetting } from '@woocommerce/settings';
import { import {
CheckboxControl, CheckboxControl,
ValidatedTextInput,
StoreNoticesContainer, StoreNoticesContainer,
} from '@woocommerce/blocks-checkout'; } from '@woocommerce/blocks-checkout';
import { ValidatedTextInput } from '@woocommerce/blocks-components';
import { useDispatch, useSelect } from '@wordpress/data'; import { useDispatch, useSelect } from '@wordpress/data';
import { CHECKOUT_STORE_KEY } from '@woocommerce/block-data'; import { CHECKOUT_STORE_KEY } from '@woocommerce/block-data';
import { isEmail } from '@wordpress/url'; import { isEmail } from '@wordpress/url';
/**
* Internal dependencies
*/
const Block = (): JSX.Element => { const Block = (): JSX.Element => {
const { customerId, shouldCreateAccount } = useSelect( ( select ) => { const { customerId, shouldCreateAccount } = useSelect( ( select ) => {
const store = select( CHECKOUT_STORE_KEY ); const store = select( CHECKOUT_STORE_KEY );

View File

@ -2,7 +2,7 @@
* External dependencies * External dependencies
*/ */
import { __ } from '@wordpress/i18n'; import { __ } from '@wordpress/i18n';
import { ValidatedTextInput } from '@woocommerce/blocks-checkout'; import { ValidatedTextInput } from '@woocommerce/blocks-components';
/** /**
* Renders a phone number input. * Renders a phone number input.

View File

@ -5,7 +5,7 @@
import type PaymentMethodLabel from '@woocommerce/base-components/cart-checkout/payment-method-label'; 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 PaymentMethodIcons from '@woocommerce/base-components/cart-checkout/payment-method-icons';
import type LoadingMask from '@woocommerce/base-components/loading-mask'; 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 * Internal dependencies

View File

@ -9,10 +9,6 @@ export { default as Button } from './button';
export { default as Label } from './label'; export { default as Label } from './label';
export { default as StoreNoticesContainer } from './store-notices-container'; export { default as StoreNoticesContainer } from './store-notices-container';
export { default as CheckboxControl } from './checkbox-control'; export { default as CheckboxControl } from './checkbox-control';
export { export { ValidationInputError } from './validation-input-error';
default as ValidatedTextInput, export { ValidatedTextInput, TextInput } from './text-input';
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 { default as StoreNotice } from './store-notice'; export { default as StoreNotice } from './store-notice';

View File

@ -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';

View File

@ -1,46 +1 @@
/** export { ValidationInputError } from '../../../components/validation-input-error/';
* 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;

View File

@ -14,4 +14,10 @@ export { default as RadioControlAccordion } from './radio-control-accordion';
export { default as SortSelect } from './sort-select'; export { default as SortSelect } from './sort-select';
export { default as Spinner } from './spinner'; export { default as Spinner } from './spinner';
export { default as Textarea } from './textarea'; 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 Title } from './title';
export { default as ValidationInputError } from './validation-input-error';

View File

@ -11,7 +11,7 @@ export interface ValidatedTextInputProps
// id to use for the input. If not provided, an id will be generated. // id to use for the input. If not provided, an id will be generated.
id?: string; id?: string;
// Unique instance ID. id will be used instead if provided. // Unique instance ID. id will be used instead if provided.
instanceId: string; instanceId?: string | undefined;
// Class name to add to the input. // Class name to add to the input.
className?: string | undefined; className?: string | undefined;
// aria-describedby attribute to add to the input. // aria-describedby attribute to add to the input.

View File

@ -22,7 +22,7 @@ import { useInstanceId } from '@wordpress/compose';
import TextInput from './text-input'; import TextInput from './text-input';
import './style.scss'; import './style.scss';
import { ValidationInputError } from '../validation-input-error'; import { ValidationInputError } from '../validation-input-error';
import { getValidityMessageForInput } from '../../utils'; import { getValidityMessageForInput } from '../../checkout/utils';
import { ValidatedTextInputProps } from './types'; import { ValidatedTextInputProps } from './types';
export type ValidatedTextInputHandle = { export type ValidatedTextInputHandle = {

View File

@ -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;