All files / laravel-saas/resources/js/stories InputError.stories.tsx

0% Statements 0/231
100% Branches 1/1
100% Functions 1/1
0% Lines 0/231

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       
import type { Meta, StoryObj } from '@storybook/react';
import InputError from '@/Components/InputError';
import { Input } from '@/Components/ui/Input';
import { Label } from '@/Components/ui/Label';
import { useState } from 'react';
 
const meta = {
  title: 'Components/InputError',
  component: InputError,
  parameters: {
    layout: 'centered',
  },
  tags: ['autodocs'],
  argTypes: {
    message: {
      control: 'text',
      description: 'Error message to display',
    },
    className: {
      control: 'text',
      description: 'Additional CSS classes',
    },
  },
} satisfies Meta<typeof InputError>;
 
export default meta;
type Story = StoryObj<typeof meta>;
 
export const Default: Story = {
  args: {
    message: 'This field is required.',
  },
};
 
export const NoMessage: Story = {
  args: {
    message: undefined,
  },
  parameters: {
    docs: {
      description: {
        story: 'When no message is provided, the component returns null and renders nothing.',
      },
    },
  },
};
 
export const LongErrorMessage: Story = {
  args: {
    message: 'The password must be at least 8 characters long and contain at least one uppercase letter, one lowercase letter, one number, and one special character.',
  },
  decorators: [
    (Story) => (
      <div className="max-w-sm">
        <Story />
      </div>
    ),
  ],
};
 
export const WithCustomClassName: Story = {
  args: {
    message: 'Custom styled error message',
    className: 'font-bold text-lg text-orange-600',
  },
};
 
export const WithFormField: Story = {
  render: () => {
    const [email, setEmail] = useState('');
    const [error, setError] = useState('');
 
    const validateEmail = (value: string) => {
      if (!value) {
        setError('Email is required');
      } else if (!/\S+@\S+\.\S+/.test(value)) {
        setError('Please enter a valid email address');
      } else {
        setError('');
      }
    };
 
    return (
      <div className="w-80 space-y-2">
        <Label htmlFor="email">Email</Label>
        <Input
          id="email"
          type="email"
          value={email}
          onChange={(e) => {
            setEmail(e.target.value);
            validateEmail(e.target.value);
          }}
          onBlur={() => validateEmail(email)}
          placeholder="Enter your email"
          className={error ? 'border-red-500' : ''}
        />
        <InputError message={error} />
      </div>
    );
  },
};
 
export const MultipleErrors: Story = {
  render: () => (
    <div className="w-80 space-y-4">
      <div className="space-y-2">
        <Label htmlFor="username">Username</Label>
        <Input id="username" placeholder="Choose a username" className="border-red-500" />
        <InputError message="Username is already taken" />
      </div>
      
      <div className="space-y-2">
        <Label htmlFor="email">Email</Label>
        <Input id="email" type="email" placeholder="Enter your email" className="border-red-500" />
        <InputError message="Please enter a valid email address" />
      </div>
      
      <div className="space-y-2">
        <Label htmlFor="password">Password</Label>
        <Input id="password" type="password" placeholder="Create a password" className="border-red-500" />
        <InputError message="Password must be at least 8 characters" />
      </div>
    </div>
  ),
};
 
export const DynamicValidation: Story = {
  render: () => {
    const [password, setPassword] = useState('');
    const [confirmPassword, setConfirmPassword] = useState('');
    
    const getPasswordError = () => {
      if (!password) return '';
      if (password.length < 8) return 'Password must be at least 8 characters';
      if (!/[A-Z]/.test(password)) return 'Password must contain at least one uppercase letter';
      if (!/[a-z]/.test(password)) return 'Password must contain at least one lowercase letter';
      if (!/[0-9]/.test(password)) return 'Password must contain at least one number';
      return '';
    };
    
    const getConfirmError = () => {
      if (!confirmPassword) return '';
      if (password !== confirmPassword) return 'Passwords do not match';
      return '';
    };
 
    return (
      <div className="w-80 space-y-4">
        <div className="space-y-2">
          <Label htmlFor="password">Password</Label>
          <Input
            id="password"
            type="password"
            value={password}
            onChange={(e) => setPassword(e.target.value)}
            placeholder="Create a password"
            className={getPasswordError() ? 'border-red-500' : ''}
          />
          <InputError message={getPasswordError()} />
        </div>
        
        <div className="space-y-2">
          <Label htmlFor="confirmPassword">Confirm Password</Label>
          <Input
            id="confirmPassword"
            type="password"
            value={confirmPassword}
            onChange={(e) => setConfirmPassword(e.target.value)}
            placeholder="Confirm your password"
            className={getConfirmError() ? 'border-red-500' : ''}
          />
          <InputError message={getConfirmError()} />
        </div>
        
        {password && confirmPassword && !getPasswordError() && !getConfirmError() && (
          <p className="text-sm text-green-600">Passwords match and meet all requirements!</p>
        )}
      </div>
    );
  },
};
 
export const InlineWithField: Story = {
  render: () => (
    <div className="w-80">
      <div className="flex items-start space-x-2">
        <div className="flex-1">
          <Input placeholder="Enter coupon code" className="border-red-500" />
        </div>
        <button className="px-4 py-2 bg-primary text-primary-foreground rounded-md">
          Apply
        </button>
      </div>
      <InputError message="Invalid coupon code" className="mt-1" />
    </div>
  ),
};
 
export const FormSubmissionErrors: Story = {
  render: () => {
    const [submitted, setSubmitted] = useState(false);
    const [formData, setFormData] = useState({
      name: '',
      email: '',
      message: '',
    });
 
    const errors = submitted ? {
      name: !formData.name ? 'Name is required' : '',
      email: !formData.email ? 'Email is required' : 
             !/\S+@\S+\.\S+/.test(formData.email) ? 'Invalid email format' : '',
      message: !formData.message ? 'Message is required' : 
               formData.message.length < 10 ? 'Message must be at least 10 characters' : '',
    } : { name: '', email: '', message: '' };
 
    const handleSubmit = (e: React.FormEvent) => {
      e.preventDefault();
      setSubmitted(true);
    };
 
    return (
      <form onSubmit={handleSubmit} className="w-96 space-y-4">
        <div className="space-y-2">
          <Label htmlFor="name">Name</Label>
          <Input
            id="name"
            value={formData.name}
            onChange={(e) => setFormData({ ...formData, name: e.target.value })}
            className={errors.name ? 'border-red-500' : ''}
          />
          <InputError message={errors.name} />
        </div>
 
        <div className="space-y-2">
          <Label htmlFor="email">Email</Label>
          <Input
            id="email"
            type="email"
            value={formData.email}
            onChange={(e) => setFormData({ ...formData, email: e.target.value })}
            className={errors.email ? 'border-red-500' : ''}
          />
          <InputError message={errors.email} />
        </div>
 
        <div className="space-y-2">
          <Label htmlFor="message">Message</Label>
          <textarea
            id="message"
            value={formData.message}
            onChange={(e) => setFormData({ ...formData, message: e.target.value })}
            className={`w-full px-3 py-2 border rounded-md ${errors.message ? 'border-red-500' : 'border-gray-300'}`}
            rows={4}
          />
          <InputError message={errors.message} />
        </div>
 
        <button
          type="submit"
          className="w-full px-4 py-2 bg-primary text-primary-foreground rounded-md"
        >
          Submit
        </button>
      </form>
    );
  },
};