|
| 1 | +import { useInputNumber } from '@primereact/headless/inputnumber'; |
| 2 | +import type { InputNumberInstance, useInputNumberValueChangeEvent } from '@primereact/types/shared/inputnumber'; |
| 3 | +import { Button } from 'primereact/button'; |
| 4 | +import { IconField } from 'primereact/iconfield'; |
| 5 | +import { InputGroup } from 'primereact/inputgroup'; |
| 6 | +import { InputNumber } from 'primereact/inputnumber'; |
| 7 | +import { Label } from 'primereact/label'; |
| 8 | +import * as React from 'react'; |
| 9 | + |
| 10 | +export default function ButtonsDemo() { |
| 11 | + const [value1, setValue1] = React.useState(20); |
| 12 | + const [value2, setValue2] = React.useState(25); |
| 13 | + const [value3, setValue3] = React.useState(10.25); |
| 14 | + |
| 15 | + const inputRef1 = React.useRef<InputNumberInstance>(null); |
| 16 | + const inputRef2 = React.useRef<InputNumberInstance>(null); |
| 17 | + const inputRef3 = React.useRef<InputNumberInstance>(null); |
| 18 | + |
| 19 | + const inputNumber1 = useInputNumber({ |
| 20 | + target: inputRef1, |
| 21 | + value: value1, |
| 22 | + mode: 'currency', |
| 23 | + currency: 'USD', |
| 24 | + onValueChange: (e: useInputNumberValueChangeEvent) => setValue1(e.value) |
| 25 | + }); |
| 26 | + |
| 27 | + const inputNumber2 = useInputNumber({ |
| 28 | + target: inputRef2, |
| 29 | + value: value2, |
| 30 | + onValueChange: (e: useInputNumberValueChangeEvent) => setValue2(e.value) |
| 31 | + }); |
| 32 | + |
| 33 | + const inputNumber3 = useInputNumber({ |
| 34 | + target: inputRef3, |
| 35 | + value: value3, |
| 36 | + mode: 'currency', |
| 37 | + currency: 'EUR', |
| 38 | + onValueChange: (e: useInputNumberValueChangeEvent) => setValue3(e.value) |
| 39 | + }); |
| 40 | + |
| 41 | + return ( |
| 42 | + <div className="card flex flex-wrap gap-4"> |
| 43 | + <div className="flex-auto"> |
| 44 | + <Label htmlFor="stacked-buttons" className="font-bold block mb-2"> |
| 45 | + Stacked |
| 46 | + </Label> |
| 47 | + <InputGroup> |
| 48 | + <InputNumber ref={inputRef1} value={value1} inputId="stacked-buttons" mode="currency" currency="USD" fluid onValueChange={inputNumber1?.onValueChange} /> |
| 49 | + <InputGroup.Addon className="flex-col"> |
| 50 | + <Button severity="secondary" onPointerDown={(e: React.PointerEvent<HTMLButtonElement>) => inputRef1.current?.increment(e, 1)} onPointerUp={inputRef1.current?.stopSpin} className="py-0 text-[.5rem]"> |
| 51 | + <i className="pi pi-angle-up" /> |
| 52 | + </Button> |
| 53 | + <Button severity="secondary" onPointerDown={(e: React.PointerEvent<HTMLButtonElement>) => inputRef1.current?.decrement(e, -1)} onPointerUp={inputRef1.current?.stopSpin} className="py-0 text-[.5rem]"> |
| 54 | + <i className="pi pi-angle-down" /> |
| 55 | + </Button> |
| 56 | + </InputGroup.Addon> |
| 57 | + </InputGroup> |
| 58 | + </div> |
| 59 | + |
| 60 | + <div className="flex-auto"> |
| 61 | + <Label htmlFor="minmax-buttons" className="font-bold block mb-2"> |
| 62 | + Min-Max |
| 63 | + </Label> |
| 64 | + <InputGroup> |
| 65 | + <InputGroup.Addon as={Button} severity="secondary" iconOnly disabled={value2 === 100} onPointerDown={(e: React.PointerEvent<HTMLButtonElement>) => inputRef2.current?.increment(e, 1)} onPointerUp={inputRef2.current?.stopSpin}> |
| 66 | + <i className="pi pi-plus"></i> |
| 67 | + </InputGroup.Addon> |
| 68 | + <InputNumber ref={inputRef2} value={value2} inputId="minmax-buttons" fluid min={0} max={100} onValueChange={inputNumber2?.onValueChange} /> |
| 69 | + <InputGroup.Addon as={Button} severity="secondary" iconOnly disabled={value2 === 0} onPointerDown={(e: React.PointerEvent<HTMLButtonElement>) => inputRef2.current?.decrement(e, -1)} onPointerUp={inputRef2.current?.stopSpin}> |
| 70 | + <i className="pi pi-minus"></i> |
| 71 | + </InputGroup.Addon> |
| 72 | + </InputGroup> |
| 73 | + </div> |
| 74 | + |
| 75 | + <div className="flex-auto"> |
| 76 | + <Label htmlFor="horizontal-buttons" className="font-bold block mb-2"> |
| 77 | + Horizontal with Step |
| 78 | + </Label> |
| 79 | + <IconField> |
| 80 | + <IconField.Icon onPointerDown={(e: React.PointerEvent<HTMLButtonElement>) => inputRef3.current?.increment(e, 0.25)} onPointerUp={inputRef3.current?.stopSpin}> |
| 81 | + <i className="pi pi-plus"></i> |
| 82 | + </IconField.Icon> |
| 83 | + <InputNumber ref={inputRef3} value={value3} inputId="horizontal-buttons" fluid mode="currency" currency="EUR" onValueChange={inputNumber3?.onValueChange} /> |
| 84 | + <IconField.Icon onPointerDown={(e: React.PointerEvent<HTMLButtonElement>) => inputRef3.current?.decrement(e, -0.25)} onPointerUp={inputRef3.current?.stopSpin}> |
| 85 | + <i className="pi pi-minus"></i> |
| 86 | + </IconField.Icon> |
| 87 | + </IconField> |
| 88 | + </div> |
| 89 | + </div> |
| 90 | + ); |
| 91 | +} |
0 commit comments