All files / web/src/components/practice NumericKeypad.stories.tsx

0% Statements 0/161
0% Branches 0/1
0% Functions 0/1
0% Lines 0/161

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                                                                                                                                                                                                                                                                                                                                   
import type { Meta, StoryObj } from '@storybook/react'
import { useState } from 'react'
import { css } from '../../../styled-system/css'
import { NumericKeypad } from './NumericKeypad'

const meta: Meta<typeof NumericKeypad> = {
  title: 'Practice/NumericKeypad',
  component: NumericKeypad,
  parameters: {
    layout: 'centered',
  },
  tags: ['autodocs'],
}

export default meta
type Story = StoryObj<typeof NumericKeypad>

/**
 * Interactive demo showing the keypad in action
 */
function InteractiveKeypadDemo() {
  const [value, setValue] = useState('')
  const [submissions, setSubmissions] = useState<string[]>([])

  const handleDigit = (digit: string) => {
    setValue((prev) => prev + digit)
  }

  const handleBackspace = () => {
    setValue((prev) => prev.slice(0, -1))
  }

  const handleSubmit = () => {
    if (value) {
      setSubmissions((prev) => [...prev, value])
      setValue('')
    }
  }

  return (
    <div
      className={css({
        display: 'flex',
        flexDirection: 'column',
        alignItems: 'center',
        gap: '1.5rem',
        padding: '2rem',
        backgroundColor: 'gray.50',
        borderRadius: '12px',
        minWidth: '350px',
      })}
    >
      {/* Current input display */}
      <div
        className={css({
          fontSize: '2rem',
          fontWeight: 'bold',
          fontFamily: 'monospace',
          minHeight: '3rem',
          padding: '0.5rem 1rem',
          backgroundColor: 'white',
          borderRadius: '8px',
          border: '2px solid',
          borderColor: 'gray.300',
          minWidth: '200px',
          textAlign: 'center',
        })}
      >
        {value || <span className={css({ color: 'gray.400' })}>Type a number</span>}
      </div>

      {/* Keypad */}
      <NumericKeypad
        onDigit={handleDigit}
        onBackspace={handleBackspace}
        onSubmit={handleSubmit}
        currentValue={value}
      />

      {/* Submission history */}
      {submissions.length > 0 && (
        <div
          className={css({
            width: '100%',
            padding: '1rem',
            backgroundColor: 'white',
            borderRadius: '8px',
            border: '1px solid',
            borderColor: 'gray.200',
          })}
        >
          <div
            className={css({
              fontSize: '0.875rem',
              fontWeight: 'bold',
              color: 'gray.600',
              marginBottom: '0.5rem',
            })}
          >
            Submitted Values:
          </div>
          <div
            className={css({
              display: 'flex',
              flexWrap: 'wrap',
              gap: '0.5rem',
            })}
          >
            {submissions.map((sub, i) => (
              <span
                key={i}
                className={css({
                  padding: '0.25rem 0.5rem',
                  backgroundColor: 'green.100',
                  color: 'green.700',
                  borderRadius: '4px',
                  fontSize: '0.875rem',
                  fontFamily: 'monospace',
                })}
              >
                {sub}
              </span>
            ))}
          </div>
        </div>
      )}
    </div>
  )
}

export const Interactive: Story = {
  render: () => <InteractiveKeypadDemo />,
}

export const Default: Story = {
  args: {
    onDigit: (digit) => console.log('Digit:', digit),
    onBackspace: () => console.log('Backspace'),
    onSubmit: () => console.log('Submit'),
    currentValue: '',
  },
}

export const WithValue: Story = {
  args: {
    onDigit: (digit) => console.log('Digit:', digit),
    onBackspace: () => console.log('Backspace'),
    onSubmit: () => console.log('Submit'),
    currentValue: '42',
  },
}

export const Disabled: Story = {
  args: {
    onDigit: (digit) => console.log('Digit:', digit),
    onBackspace: () => console.log('Backspace'),
    onSubmit: () => console.log('Submit'),
    currentValue: '123',
    disabled: true,
  },
}