2020-03-17 11:45:33 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { useValidationContext } from '@woocommerce/base-context';
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import './style.scss';
|
|
|
|
|
|
|
|
export const ValidationInputError = ( {
|
|
|
|
errorMessage = '',
|
|
|
|
propertyName = '',
|
|
|
|
elementId = '',
|
|
|
|
} ) => {
|
|
|
|
const { getValidationError, getValidationErrorId } = useValidationContext();
|
2020-03-23 11:22:00 +00:00
|
|
|
if ( ! errorMessage ) {
|
|
|
|
const error = getValidationError( propertyName ) || {};
|
|
|
|
if ( error.message && ! error.hidden ) {
|
|
|
|
errorMessage = error.message;
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
2020-03-17 11:45:33 +00:00
|
|
|
}
|
2020-03-23 11:22:00 +00:00
|
|
|
|
2020-03-17 11:45:33 +00:00
|
|
|
return (
|
2020-06-17 09:53:42 +00:00
|
|
|
<div className="wc-block-components-validation-error" role="alert">
|
2020-03-17 11:45:33 +00:00
|
|
|
<p id={ getValidationErrorId( elementId ) }>{ errorMessage }</p>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
ValidationInputError.propTypes = {
|
|
|
|
errorMessage: PropTypes.string,
|
|
|
|
propertyName: PropTypes.string,
|
|
|
|
elementId: PropTypes.string,
|
|
|
|
};
|