/
index.tsx
125 lines (116 loc) · 3.59 KB
/
index.tsx
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
"use client";
import * as React from "react";
import cx from "clsx";
import Minus from "../../icons/Minus";
import Plus from "../../icons/Plus";
import ButtonPrimitive from "../../primitives/ButtonPrimitive";
import useTheme from "../../hooks/useTheme";
import type { Props } from "./types";
const getMaxWidth = ({ maxWidth }: { maxWidth: string | number }) => {
if (typeof maxWidth === "string") return maxWidth;
return `${parseInt(maxWidth.toString(), 10)}px`;
};
const stepperButtonMixin = ({ disabled, active }: { disabled: boolean; active?: boolean }) =>
cx(
"[&_svg]:p-xxxs",
"de:[&_svg]:h-icon-medium de:[&_svg]:w-icon-medium [&_svg]:h-icon-large [&_svg]:w-icon-large",
"[&_svg]:rounded-circle",
"focus:outline-0",
"[&_svg]:focus:outline-blue-normal [&_svg]:focus:outline [&_svg]:focus:outline-2",
active ? "[&_svg]:bg-blue-normal" : "[&_svg]:bg-cloud-normal",
!disabled && [
active
? "hover:[&_svg]:bg-blue-normal-hover [&_svg]:active:focus:shadow-[inset_0_0_0_2px_var(--palette-blue-light-active)]"
: "hover:[&_svg]:bg-cloud-normal-hover [&_svg]:active:focus:shadow-[inset_0_0_0_2px_var(--palette-cloud-normal-active)]",
],
);
const StepperStateless = ({
maxWidth = "120px",
disabled,
dataTest,
id,
value,
active,
name,
minValue = -Infinity,
maxValue = Infinity,
onKeyDown,
onBlur,
onFocus,
onIncrement,
onDecrement,
titleIncrement,
titleDecrement,
disabledIncrement,
disabledDecrement,
}: Props) => {
const theme = useTheme();
const iconStyles = {
foreground: active ? theme.orbit.paletteWhite : theme.orbit.paletteInkDark,
};
const isMinusDisabled =
disabled || disabledDecrement || (typeof value === "number" && value <= +minValue);
const isPlusDisabled =
disabled || disabledIncrement || (typeof value === "number" && value >= +maxValue);
return (
<div
data-test={dataTest}
id={id}
className="flex w-full flex-auto"
style={{ maxWidth: getMaxWidth({ maxWidth }) }}
>
<ButtonPrimitive
width="44px"
className={stepperButtonMixin({ disabled: isMinusDisabled, active })}
disabled={isMinusDisabled}
iconLeft={<Minus size="small" />}
onClick={ev => {
if (onDecrement && !disabled) {
onDecrement(ev);
}
}}
title={titleDecrement}
icons={iconStyles}
/>
<input
className={cx(
"h-form-box-normal w-full min-w-0 border-0",
"p-0 text-center",
"text-large text-form-element-filled-foreground font-medium",
"[&::-webkit-inner-spin-button]:appearance-none [&::-webkit-outer-spin-button]:appearance-none",
"[&::-webkit-inner-spin-button]:m-0 [&::-webkit-outer-spin-button]:m-0",
disabled ? "bg-transparent opacity-50" : "opacity-100",
"focus:outline-0",
)}
name={name}
disabled={disabled}
type="text"
value={Number(value) || 0}
min={minValue}
max={maxValue}
onKeyDown={ev => {
if (onKeyDown) {
onKeyDown(ev);
}
}}
onBlur={onBlur}
onFocus={onFocus}
readOnly
/>
<ButtonPrimitive
width="44px"
className={stepperButtonMixin({ disabled: isPlusDisabled, active })}
disabled={isPlusDisabled}
iconLeft={<Plus size="small" />}
onClick={ev => {
if (onIncrement && !disabled) {
onIncrement(ev);
}
}}
title={titleIncrement}
icons={iconStyles}
/>
</div>
);
};
export default StepperStateless;