Advertisement
RupeshAcharya60

kff

Sep 25th, 2024
15
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.31 KB | None | 0 0
  1. import React from 'react'
  2.  
  3. interface RadioButtonProps {
  4. color: 'black' | 'white' | 'grey'
  5. isSelected: boolean
  6. onChange: (color: string) => void
  7. }
  8.  
  9. export default function RadioButton({ color, isSelected, onChange }: RadioButtonProps) {
  10. const bgColors = {
  11. black: 'bg-black',
  12. white: 'bg-white',
  13. grey: 'bg-gray-500',
  14. }
  15.  
  16. const borderColors = {
  17. black: 'border-black',
  18. white: 'border-gray-300',
  19. grey: 'border-gray-500',
  20. }
  21.  
  22. return (
  23. <label className="relative inline-block cursor-pointer">
  24. <input
  25. type="radio"
  26. className="absolute opacity-0 w-0 h-0"
  27. checked={isSelected}
  28. onChange={() => onChange(color)}
  29. />
  30. <span
  31. className={`block w-10 h-10 rounded-full border-2 ${borderColors[color]} ${bgColors[color]}`}
  32. >
  33. {isSelected && (
  34. <svg
  35. className={`absolute inset-0 w-full h-full ${color === 'white' ? 'text-black' : 'text-white'}`}
  36. viewBox="0 0 24 24"
  37. fill="none"
  38. stroke="currentColor"
  39. strokeWidth="2"
  40. strokeLinecap="round"
  41. strokeLinejoin="round"
  42. >
  43. <polyline points="20 6 9 17 4 12"></polyline>
  44. </svg>
  45. )}
  46. </span>
  47. </label>
  48. )
  49. }
  50.  
  51.  
  52.  
  53.  
  54.  
  55. ---------------------------------------------------------------------
  56. import React, { useState } from 'react'
  57. import RadioButton from './radio-button'
  58.  
  59. export default function ColorSelector() {
  60. const [selectedColor, setSelectedColor] = useState<string>('')
  61.  
  62. const handleColorChange = (color: string) => {
  63. setSelectedColor(color)
  64. }
  65.  
  66. return (
  67. <div className="flex flex-col items-center space-y-4">
  68. <h2 className="text-2xl font-bold mb-2">Select a Color</h2>
  69. <div className="flex space-x-4">
  70. {['black', 'white', 'grey'].map((color) => (
  71. <RadioButton
  72. key={color}
  73. color={color as 'black' | 'white' | 'grey'}
  74. isSelected={selectedColor === color}
  75. onChange={handleColorChange}
  76. />
  77. ))}
  78. </div>
  79. {selectedColor && (
  80. <p className="mt-4 text-lg">
  81. Selected color: <span className="font-semibold capitalize">{selectedColor}</span>
  82. </p>
  83. )}
  84. </div>
  85. )
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement