({
value,
onChange,
maximum,
quaternary = false,
className,
}: MaxRowCountSelectProps)
| 20 | }; |
| 21 | |
| 22 | export function MaxRowCountSelect({ |
| 23 | value, |
| 24 | onChange, |
| 25 | maximum, |
| 26 | quaternary = false, |
| 27 | className, |
| 28 | }: MaxRowCountSelectProps) { |
| 29 | const { t } = useTranslation(); |
| 30 | const [open, setOpen] = useState(false); |
| 31 | |
| 32 | const rowCountOptions = useMemo(() => { |
| 33 | const list = [1, 100, 500, 1000, 5000, 10000, 100000].filter( |
| 34 | (num) => num <= maximum |
| 35 | ); |
| 36 | if (maximum !== Number.MAX_VALUE && !list.includes(maximum)) { |
| 37 | list.push(maximum); |
| 38 | } |
| 39 | return list; |
| 40 | }, [maximum]); |
| 41 | |
| 42 | const handlePresetClick = (n: number) => { |
| 43 | onChange(n); |
| 44 | setOpen(false); |
| 45 | }; |
| 46 | |
| 47 | const handleInputChange = (e: React.ChangeEvent<HTMLInputElement>) => { |
| 48 | const rawValue = Number(e.target.value); |
| 49 | const clamped = minmax( |
| 50 | Number.isNaN(rawValue) ? 0 : rawValue, |
| 51 | first(rowCountOptions) ?? 1, |
| 52 | last(rowCountOptions) ?? 100000 |
| 53 | ); |
| 54 | onChange(clamped); |
| 55 | }; |
| 56 | |
| 57 | return ( |
| 58 | <Popover open={open} onOpenChange={setOpen}> |
| 59 | <PopoverTrigger |
| 60 | render={ |
| 61 | <Button |
| 62 | variant={quaternary ? "ghost" : "outline"} |
| 63 | size="sm" |
| 64 | className={cn("justify-start", className)} |
| 65 | /> |
| 66 | } |
| 67 | > |
| 68 | {t("sql-editor.result-limit.self")}{" "} |
| 69 | {t("common.rows.n-rows", { n: value })} |
| 70 | <ChevronRight className="size-4 ml-1" /> |
| 71 | </PopoverTrigger> |
| 72 | <PopoverContent side="bottom" align="start" className="p-1 min-w-32"> |
| 73 | <div className="flex flex-col"> |
| 74 | {rowCountOptions.map((n) => ( |
| 75 | <button |
| 76 | key={n} |
| 77 | type="button" |
| 78 | className={cn( |
| 79 | "px-3 py-1.5 text-sm text-left rounded-xs cursor-pointer", |
nothing calls this directly
no test coverage detected