File "index.js"

Full Path: /home/vantageo/public_html/cache/cache/cache/.wp-cli/wp-content/plugins/woocommerce/packages/woocommerce-blocks/assets/js/base/components/country-input/stories/index.js
File size: 1.3 KB
MIME-type: text/x-java
Charset: utf-8

/**
 * External dependencies
 */
import { text } from '@storybook/addon-knobs';
import { useState, useEffect } from '@wordpress/element';
import {
	ValidationContextProvider,
	useValidationContext,
} from '@woocommerce/base-context';

/**
 * Internal dependencies
 */
import { CountryInput } from '../';
import { countries as exampleCountries } from './countries-filler';

export default {
	title: 'WooCommerce Blocks/@base-components/CountryInput',
	component: CountryInput,
};

const StoryComponent = ( { label, errorMessage } ) => {
	const [ selectedCountry, selectCountry ] = useState();
	const {
		setValidationErrors,
		clearValidationError,
	} = useValidationContext();
	useEffect( () => {
		setValidationErrors( { country: errorMessage } );
	}, [ errorMessage, setValidationErrors ] );
	const updateCountry = ( country ) => {
		clearValidationError( 'country' );
		selectCountry( country );
	};
	return (
		<CountryInput
			countries={ exampleCountries }
			label={ label }
			value={ selectedCountry }
			onChange={ updateCountry }
		/>
	);
};

export const Default = () => {
	const label = text( 'Input Label', 'Countries:' );
	const errorMessage = text( 'Error Message', '' );
	return (
		<ValidationContextProvider>
			<StoryComponent label={ label } errorMessage={ errorMessage } />
		</ValidationContextProvider>
	);
};