woocommerce/plugins/woocommerce-blocks/packages/components/validation-input-error/index.tsx

51 lines
1.2 KiB
TypeScript
Raw Normal View History

/**
* External dependencies
*/
import { useSelect } from '@wordpress/data';
import { VALIDATION_STORE_KEY } from '@woocommerce/block-data';
import { Icon, warning } from '@wordpress/icons';
/**
* Internal dependencies
*/
import './style.scss';
export 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 }>
<Icon icon={ warning } />
<span>{ errorMessage }</span>
</p>
</div>
);
};
export default ValidationInputError;