All files / laravel-saas/resources/js/stories/ui Checkbox.stories.tsx

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

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                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   
import type { Meta, StoryObj } from '@storybook/react';
import { Checkbox } from '@/Components/ui/Checkbox';
import { Label } from '@/Components/ui/Label';
import { useState } from 'react';
 
const meta = {
  title: 'UI/Checkbox',
  component: Checkbox,
  parameters: {
    layout: 'centered',
  },
  tags: ['autodocs'],
  argTypes: {
    checked: {
      control: 'boolean',
      description: 'Controlled checked state',
    },
    defaultChecked: {
      control: 'boolean',
      description: 'Default checked state for uncontrolled usage',
    },
    disabled: {
      control: 'boolean',
      description: 'Disabled state',
    },
    className: {
      control: 'text',
      description: 'Additional CSS classes',
    },
  },
} satisfies Meta<typeof Checkbox>;
 
export default meta;
type Story = StoryObj<typeof meta>;
 
export const Default: Story = {
  args: {},
};
 
export const Checked: Story = {
  args: {
    defaultChecked: true,
  },
};
 
export const Disabled: Story = {
  args: {
    disabled: true,
  },
};
 
export const DisabledChecked: Story = {
  args: {
    disabled: true,
    defaultChecked: true,
  },
};
 
export const WithLabel: Story = {
  render: () => (
    <div className="flex items-center space-x-2">
      <Checkbox id="terms" />
      <Label htmlFor="terms" className="text-sm font-medium leading-none peer-disabled:cursor-not-allowed peer-disabled:opacity-70">
        Accept terms and conditions
      </Label>
    </div>
  ),
};
 
export const Controlled: Story = {
  render: () => {
    const [checked, setChecked] = useState(false);
    
    return (
      <div className="space-y-4">
        <div className="flex items-center space-x-2">
          <Checkbox 
            id="controlled" 
            checked={checked}
            onCheckedChange={(checkedState) => setChecked(checkedState as boolean)}
          />
          <Label htmlFor="controlled" className="text-sm font-medium leading-none">
            Controlled checkbox
          </Label>
        </div>
        <p className="text-sm text-muted-foreground">
          Checked state: {checked ? 'true' : 'false'}
        </p>
      </div>
    );
  },
};
 
export const MultipleCheckboxes: Story = {
  render: () => {
    const [checkedItems, setCheckedItems] = useState({
      item1: false,
      item2: true,
      item3: false,
    });
 
    const handleCheckedChange = (item: keyof typeof checkedItems, checked: boolean) => {
      setCheckedItems(prev => ({ ...prev, [item]: checked }));
    };
 
    return (
      <div className="space-y-4">
        <h3 className="text-sm font-medium">Select your preferences:</h3>
        <div className="space-y-2">
          <div className="flex items-center space-x-2">
            <Checkbox 
              id="item1" 
              checked={checkedItems.item1}
              onCheckedChange={(checked) => handleCheckedChange('item1', checked as boolean)}
            />
            <Label htmlFor="item1" className="text-sm">Email notifications</Label>
          </div>
          <div className="flex items-center space-x-2">
            <Checkbox 
              id="item2" 
              checked={checkedItems.item2}
              onCheckedChange={(checked) => handleCheckedChange('item2', checked as boolean)}
            />
            <Label htmlFor="item2" className="text-sm">SMS notifications</Label>
          </div>
          <div className="flex items-center space-x-2">
            <Checkbox 
              id="item3" 
              checked={checkedItems.item3}
              onCheckedChange={(checked) => handleCheckedChange('item3', checked as boolean)}
            />
            <Label htmlFor="item3" className="text-sm">Push notifications</Label>
          </div>
        </div>
        <p className="text-sm text-muted-foreground">
          Selected: {Object.entries(checkedItems).filter(([_, checked]) => checked).map(([key]) => key).join(', ') || 'None'}
        </p>
      </div>
    );
  },
};
 
export const IndeterminateState: Story = {
  render: () => {
    const [checkedItems, setCheckedItems] = useState({
      item1: false,
      item2: true,
      item3: false,
    });
 
    const allChecked = Object.values(checkedItems).every(Boolean);
    const someChecked = Object.values(checkedItems).some(Boolean);
    const indeterminate = someChecked && !allChecked;
 
    const handleParentChange = (checked: boolean) => {
      setCheckedItems({
        item1: checked,
        item2: checked,
        item3: checked,
      });
    };
 
    const handleChildChange = (item: keyof typeof checkedItems, checked: boolean) => {
      setCheckedItems(prev => ({ ...prev, [item]: checked }));
    };
 
    return (
      <div className="space-y-4">
        <div className="flex items-center space-x-2">
          <Checkbox 
            id="parent" 
            checked={allChecked ? true : indeterminate ? "indeterminate" : false}
            onCheckedChange={(checked) => handleParentChange(checked as boolean)}
          />
          <Label htmlFor="parent" className="text-sm font-medium">Select all</Label>
        </div>
        <div className="ml-6 space-y-2">
          <div className="flex items-center space-x-2">
            <Checkbox 
              id="child1" 
              checked={checkedItems.item1}
              onCheckedChange={(checked) => handleChildChange('item1', checked as boolean)}
            />
            <Label htmlFor="child1" className="text-sm">Option 1</Label>
          </div>
          <div className="flex items-center space-x-2">
            <Checkbox 
              id="child2" 
              checked={checkedItems.item2}
              onCheckedChange={(checked) => handleChildChange('item2', checked as boolean)}
            />
            <Label htmlFor="child2" className="text-sm">Option 2</Label>
          </div>
          <div className="flex items-center space-x-2">
            <Checkbox 
              id="child3" 
              checked={checkedItems.item3}
              onCheckedChange={(checked) => handleChildChange('item3', checked as boolean)}
            />
            <Label htmlFor="child3" className="text-sm">Option 3</Label>
          </div>
        </div>
      </div>
    );
  },
};
 
export const FormExample: Story = {
  render: () => (
    <form className="space-y-6" onSubmit={(e) => e.preventDefault()}>
      <div className="space-y-4">
        <h3 className="text-sm font-medium">Notification preferences</h3>
        <div className="space-y-2">
          <div className="flex items-center space-x-2">
            <Checkbox id="marketing" defaultChecked />
            <Label htmlFor="marketing" className="text-sm">
              Receive marketing emails
            </Label>
          </div>
          <div className="flex items-center space-x-2">
            <Checkbox id="security" defaultChecked />
            <Label htmlFor="security" className="text-sm">
              Receive security alerts
            </Label>
          </div>
          <div className="flex items-center space-x-2">
            <Checkbox id="updates" />
            <Label htmlFor="updates" className="text-sm">
              Receive product updates
            </Label>
          </div>
        </div>
      </div>
      <div className="flex items-center space-x-2 pt-4 border-t">
        <Checkbox id="agree" required />
        <Label htmlFor="agree" className="text-sm">
          I agree to the terms and conditions
        </Label>
      </div>
    </form>
  ),
};