2020-03-23 11:22:00 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { __ } from '@wordpress/i18n';
|
|
|
|
import { useEffect } from 'react';
|
|
|
|
import { useValidationContext } from '@woocommerce/base-context';
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import classnames from 'classnames';
|
2020-03-26 12:39:54 +00:00
|
|
|
import { withInstanceId } from '@woocommerce/base-hocs/with-instance-id';
|
2020-03-23 11:22:00 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import { ValidationInputError } from '../validation';
|
|
|
|
import Select from './index';
|
|
|
|
import './style.scss';
|
|
|
|
|
|
|
|
const ValidatedSelect = ( {
|
|
|
|
className,
|
|
|
|
id,
|
|
|
|
value,
|
|
|
|
instanceId,
|
|
|
|
required,
|
|
|
|
errorId,
|
|
|
|
errorMessage = __(
|
|
|
|
'Please select a value.',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
),
|
|
|
|
...rest
|
|
|
|
} ) => {
|
|
|
|
const selectId = id || 'select-' + instanceId;
|
|
|
|
errorId = errorId || selectId;
|
|
|
|
const {
|
|
|
|
getValidationError,
|
|
|
|
setValidationErrors,
|
|
|
|
clearValidationError,
|
|
|
|
} = useValidationContext();
|
|
|
|
const validateSelect = () => {
|
|
|
|
if ( ! required || value ) {
|
|
|
|
clearValidationError( errorId );
|
|
|
|
} else {
|
|
|
|
setValidationErrors( {
|
|
|
|
[ errorId ]: {
|
|
|
|
message: errorMessage,
|
|
|
|
hidden: true,
|
|
|
|
},
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
useEffect( () => {
|
|
|
|
validateSelect();
|
|
|
|
}, [ value ] );
|
|
|
|
|
2020-04-02 09:27:54 +00:00
|
|
|
// Remove validation errors when unmounted.
|
|
|
|
useEffect( () => {
|
|
|
|
return () => {
|
|
|
|
clearValidationError( errorId );
|
|
|
|
};
|
|
|
|
}, [] );
|
|
|
|
|
2020-03-23 11:22:00 +00:00
|
|
|
const error = getValidationError( errorId ) || {};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Select
|
|
|
|
id={ selectId }
|
|
|
|
className={ classnames( className, {
|
|
|
|
'has-error': error.message && ! error.hidden,
|
|
|
|
} ) }
|
|
|
|
feedback={ <ValidationInputError propertyName={ errorId } /> }
|
|
|
|
value={ value }
|
|
|
|
{ ...rest }
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
ValidatedSelect.propTypes = {
|
|
|
|
className: PropTypes.string,
|
|
|
|
errorId: PropTypes.string,
|
|
|
|
errorMessage: PropTypes.string,
|
|
|
|
id: PropTypes.string,
|
|
|
|
required: PropTypes.bool,
|
|
|
|
value: PropTypes.shape( {
|
|
|
|
key: PropTypes.string.isRequired,
|
|
|
|
name: PropTypes.string.isRequired,
|
|
|
|
} ),
|
|
|
|
};
|
|
|
|
|
|
|
|
export default withInstanceId( ValidatedSelect );
|