import React, { useState } from 'react';
import { action } from 'storybook/actions';
import { States } from '../../utilities';
import { Textarea } from '../Textarea';
import { Select } from '../Select';
import { Field, FieldProps } from './Field';
export default { title: 'Components/Field' };
export const Default = () => (
states={[
{},
{ focus: true },
{ disabled: true },
{ defaultValue: 'With value' },
{ error: 'Something is wrong', defaultValue: 'With value' },
{ label: 'Name of input' },
{ label: 'Name of input', error: 'Something went wrong' }
]}
>
);
export const AribitraryChildren = () => (
states={[
{},
{ focus: true },
{ disabled: true },
{ defaultValue: 'With value' },
{ error: 'Something is wrong', defaultValue: 'With value' },
{ label: 'Name of input' },
{ label: 'Name of input', error: 'Something went wrong' }
]}
>
);
export const SelectChildren = () => {
const OPTIONS = [
{ value: 'featured', label: 'Featured' },
{
value: 'recency',
label: "What's New",
disabled: true
},
{
value: 'high',
label: 'Price High to Low'
},
{
value: 'low',
label: 'Price Low to High'
}
];
return (
states={[
{},
{ value: 'high' },
{ label: 'Sort — ' },
{ disabled: true, label: 'Sort' },
{ label: '' },
{ label: '', error: 'Error' }
]}
>
);
};
export const AccessibilityErrorState = () => (
Inspect the input to see: aria-invalid="true", aria-describedby linking to the
error, and role="alert" on the error message for screen reader announcements.
);
export const AccessibilityDynamicValidation = () => {
const [value, setValue] = useState('');
const [touched, setTouched] = useState(false);
const isValidEmail = (email: string) => /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email);
const error =
touched && value && !isValidEmail(value) ? 'Please enter a valid email address' : undefined;
return (
setTouched(true)}
/>
Type an invalid email and blur the field. Screen readers will announce the error via
role="alert" when it appears.
);
};