|
| 1 | +import { useEffect, useState } from 'react'; |
| 2 | + |
| 3 | +import { NumericFormat } from 'react-number-format'; |
| 4 | +import styled, { css } from 'styled-components'; |
| 5 | + |
| 6 | +import { Icon, IconName } from '@/components/Icon'; |
| 7 | + |
| 8 | +type QuickButtonProps = { |
| 9 | + options: string[]; |
| 10 | + onSelect?: (value: string) => void; |
| 11 | + onOptionsEdit?: (options: string[]) => void; |
| 12 | + currentValue?: string; |
| 13 | + disabled?: boolean; |
| 14 | +}; |
| 15 | + |
| 16 | +export const QuickButtons = ({ |
| 17 | + options, |
| 18 | + onSelect, |
| 19 | + onOptionsEdit, |
| 20 | + currentValue, |
| 21 | + disabled, |
| 22 | +}: QuickButtonProps) => { |
| 23 | + const [isEditing, setIsEditing] = useState(false); |
| 24 | + const [editValues, setEditValues] = useState<string[]>(options); |
| 25 | + |
| 26 | + const handleEdit = () => { |
| 27 | + setEditValues(options.map((o) => o.toString())); |
| 28 | + setIsEditing(true); |
| 29 | + }; |
| 30 | + |
| 31 | + const handleConfirmEdit = () => { |
| 32 | + const uniqueValues = editValues.map((value, i) => { |
| 33 | + const isUnique = !options.includes(value) || options.indexOf(value) === i; |
| 34 | + const isValid = !Number.isNaN(parseFloat(value)); |
| 35 | + |
| 36 | + if (isUnique && isValid) { |
| 37 | + return value; |
| 38 | + } |
| 39 | + |
| 40 | + return options[i]!; |
| 41 | + }); |
| 42 | + |
| 43 | + onOptionsEdit?.(uniqueValues); |
| 44 | + setIsEditing(false); |
| 45 | + }; |
| 46 | + |
| 47 | + useEffect(() => { |
| 48 | + setEditValues(options); |
| 49 | + }, [options]); |
| 50 | + |
| 51 | + const handleInputChange = (index: number, value: string) => { |
| 52 | + const newEditValues = [...editValues]; |
| 53 | + newEditValues[index] = value; |
| 54 | + setEditValues(newEditValues); |
| 55 | + }; |
| 56 | + |
| 57 | + return ( |
| 58 | + <$QuickButtonsContainer> |
| 59 | + {options.map((option, i) => |
| 60 | + isEditing ? ( |
| 61 | + <$InputQuickButtonContainer key={option}> |
| 62 | + <$QuickButtonInput |
| 63 | + allowNegative={false} |
| 64 | + decimalScale={2} |
| 65 | + disabled={disabled} |
| 66 | + value={editValues[i]} |
| 67 | + onChange={(e) => handleInputChange(i, e.target.value)} |
| 68 | + /> |
| 69 | + </$InputQuickButtonContainer> |
| 70 | + ) : ( |
| 71 | + <$QuickButtonContainer |
| 72 | + key={option} |
| 73 | + onClick={() => onSelect?.(option)} |
| 74 | + type="button" |
| 75 | + isSelected={currentValue === option} |
| 76 | + disabled={disabled} |
| 77 | + > |
| 78 | + <span tw="truncate">{option}</span> |
| 79 | + </$QuickButtonContainer> |
| 80 | + ) |
| 81 | + )} |
| 82 | + <$QuickButtonContainer |
| 83 | + aria-label={isEditing ? 'Confirm' : 'Edit'} |
| 84 | + type="button" |
| 85 | + onClick={isEditing ? handleConfirmEdit : handleEdit} |
| 86 | + disabled={disabled} |
| 87 | + > |
| 88 | + <Icon iconName={isEditing ? IconName.Check : IconName.Pencil2} size="1rem" /> |
| 89 | + </$QuickButtonContainer> |
| 90 | + </$QuickButtonsContainer> |
| 91 | + ); |
| 92 | +}; |
| 93 | + |
| 94 | +const $QuickButtonInput = styled(NumericFormat)` |
| 95 | + outline: 0; |
| 96 | + border: 0; |
| 97 | + min-width: 0; |
| 98 | + background-color: transparent; |
| 99 | + text-align: center; |
| 100 | +`; |
| 101 | + |
| 102 | +const $QuickButtonsContainer = styled.div` |
| 103 | + display: flex; |
| 104 | + flex-direction: row; |
| 105 | + align-items: center; |
| 106 | + height: 2.3125rem; |
| 107 | + gap: 0.75rem; |
| 108 | +
|
| 109 | + & > *:not(:last-child) { |
| 110 | + flex: 1; |
| 111 | + } |
| 112 | +
|
| 113 | + & > *:last-child { |
| 114 | + aspect-ratio: 1; |
| 115 | + flex-shrink: 0; |
| 116 | + } |
| 117 | +`; |
| 118 | + |
| 119 | +const QuickButtonContainerStyles = css` |
| 120 | + display: inline-flex; |
| 121 | + flex-direction: row; |
| 122 | + justify-content: center; |
| 123 | + align-items: center; |
| 124 | + height: 100%; |
| 125 | + justify-content: center; |
| 126 | + border-radius: 0.5rem; |
| 127 | + border: 1px solid var(--color-layer-4); |
| 128 | + padding: 0 0.5rem; |
| 129 | + font: var(--font-base-medium); |
| 130 | + color: var(--color-text); |
| 131 | + min-width: 0; |
| 132 | +
|
| 133 | + @media (prefers-reduced-motion: no-preference) { |
| 134 | + transition: 0.3s var(--ease-out-expo); |
| 135 | + } |
| 136 | +`; |
| 137 | + |
| 138 | +const $InputQuickButtonContainer = styled.div` |
| 139 | + ${QuickButtonContainerStyles} |
| 140 | +
|
| 141 | + background-color: var(--color-layer-3); |
| 142 | +
|
| 143 | + &:focus-within { |
| 144 | + background-color: var(--color-layer-4); |
| 145 | + } |
| 146 | +`; |
| 147 | + |
| 148 | +const $QuickButtonContainer = styled.button<{ isSelected?: boolean }>` |
| 149 | + ${QuickButtonContainerStyles} |
| 150 | +
|
| 151 | + ${({ isSelected }) => |
| 152 | + isSelected && |
| 153 | + css` |
| 154 | + background-color: var(--color-layer-4); |
| 155 | + `} |
| 156 | +
|
| 157 | + @media (hover: hover) { |
| 158 | + &:hover { |
| 159 | + background-color: var(--color-layer-3); |
| 160 | + } |
| 161 | + } |
| 162 | +`; |
0 commit comments