@orsetra/shared-ui 1.10.26 → 1.10.27

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,33 +1,7 @@
1
1
  import React from 'react';
2
- import { Input } from '../../ui/input';
2
+ import QuantityInput, { type QuantityInputProps } from '../QuantityInput';
3
+ import { CPU_UNITS } from '../../../lib/quantity';
3
4
 
4
- type Props = {
5
- value?: any;
6
- id: string;
7
- onChange: (value: any) => void;
8
- disabled: boolean;
9
- };
10
-
11
- const CPUNumber: React.FC<Props> = ({ value, id, onChange, disabled }) => {
12
- const initValue = value ? parseFloat(value) : undefined;
13
-
14
- const handleChange = (e: React.ChangeEvent<HTMLInputElement>) => {
15
- onChange(e.target.value);
16
- };
17
-
18
- return (
19
- <div className="flex items-center gap-2">
20
- <Input
21
- id={id}
22
- type="number"
23
- disabled={disabled}
24
- onChange={handleChange}
25
- value={initValue}
26
- className="flex-1"
27
- />
28
- <span className="text-sm text-ibm-gray-70">Core</span>
29
- </div>
30
- );
31
- };
5
+ const CPUNumber: React.FC<QuantityInputProps> = props => <QuantityInput {...props} units={CPU_UNITS} />;
32
6
 
33
7
  export default CPUNumber;
@@ -1,34 +1,7 @@
1
1
  import React from 'react';
2
- import { Input } from '../../ui/input';
2
+ import QuantityInput, { type QuantityInputProps } from '../QuantityInput';
3
+ import { DISK_UNITS } from '../../../lib/quantity';
3
4
 
4
- type Props = {
5
- id: string;
6
- onChange: (value: any) => void;
7
- value?: any;
8
- disabled: boolean;
9
- };
10
-
11
- const DiskNumber: React.FC<Props> = ({ value, id, onChange, disabled }) => {
12
- const initValue = value ? parseInt(value.replace('Gi', ''), 10) : undefined;
13
-
14
- const handleChange = (e: React.ChangeEvent<HTMLInputElement>) => {
15
- onChange(e.target.value + 'Gi');
16
- };
17
-
18
- return (
19
- <div className="flex items-center gap-2">
20
- <Input
21
- id={id}
22
- type="number"
23
- min="0"
24
- disabled={disabled}
25
- onChange={handleChange}
26
- value={initValue}
27
- className="flex-1"
28
- />
29
- <span className="text-sm text-ibm-gray-70">Gi</span>
30
- </div>
31
- );
32
- };
5
+ const DiskNumber: React.FC<QuantityInputProps> = props => <QuantityInput {...props} units={DISK_UNITS} />;
33
6
 
34
7
  export default DiskNumber;
@@ -1,34 +1,7 @@
1
1
  import React from 'react';
2
- import { Input } from '../../ui/input';
2
+ import QuantityInput, { type QuantityInputProps } from '../QuantityInput';
3
+ import { MEMORY_UNITS } from '../../../lib/quantity';
3
4
 
4
- type Props = {
5
- id: string;
6
- onChange: (value: any) => void;
7
- value?: any;
8
- disabled: boolean;
9
- };
10
-
11
- const MemoryNumber: React.FC<Props> = ({ value, id, onChange, disabled }) => {
12
- const initValue = value ? parseInt(value.replace('Mi', ''), 10) : undefined;
13
-
14
- const handleChange = (e: React.ChangeEvent<HTMLInputElement>) => {
15
- onChange(e.target.value + 'Mi');
16
- };
17
-
18
- return (
19
- <div className="flex items-center gap-2">
20
- <Input
21
- id={id}
22
- type="number"
23
- min="0"
24
- disabled={disabled}
25
- onChange={handleChange}
26
- value={initValue}
27
- className="flex-1"
28
- />
29
- <span className="text-sm text-ibm-gray-70">Mi</span>
30
- </div>
31
- );
32
- };
5
+ const MemoryNumber: React.FC<QuantityInputProps> = props => <QuantityInput {...props} units={MEMORY_UNITS} />;
33
6
 
34
7
  export default MemoryNumber;
@@ -0,0 +1,66 @@
1
+ import React, { useEffect, useRef, useState } from 'react';
2
+ import { Input } from '../../ui/input';
3
+ import { formatQuantity, parseQuantity, unitsFor, type QuantityUnit } from '../../../lib/quantity';
4
+
5
+ export type QuantityInputProps = {
6
+ id: string;
7
+ value?: any;
8
+ onChange: (value: any) => void;
9
+ disabled?: boolean;
10
+ className?: string;
11
+ };
12
+
13
+ type Props = QuantityInputProps & {
14
+ units: QuantityUnit[];
15
+ step?: string;
16
+ };
17
+
18
+ const QuantityInput: React.FC<Props> = ({ id, value, onChange, disabled, className, units, step = 'any' }) => {
19
+ const parsed = parseQuantity(value, units);
20
+ const [amount, setAmount] = useState(parsed.amount);
21
+ const [suffix, setSuffix] = useState(parsed.unit.suffix);
22
+ const lastEmitted = useRef<any>(value);
23
+
24
+ useEffect(() => {
25
+ if (value === lastEmitted.current) return;
26
+ lastEmitted.current = value;
27
+ const next = parseQuantity(value, units);
28
+ setAmount(next.amount);
29
+ if (next.amount !== '') setSuffix(next.unit.suffix);
30
+ // eslint-disable-next-line react-hooks/exhaustive-deps
31
+ }, [value]);
32
+
33
+ const emit = (nextAmount: string, nextSuffix: string) => {
34
+ const quantity = formatQuantity(nextAmount, nextSuffix);
35
+ lastEmitted.current = quantity;
36
+ onChange(quantity);
37
+ };
38
+
39
+ return (
40
+ <div className="flex items-stretch">
41
+ <Input
42
+ id={id}
43
+ type="number"
44
+ min="0"
45
+ step={step}
46
+ disabled={disabled}
47
+ value={amount}
48
+ onChange={event => { setAmount(event.target.value); emit(event.target.value, suffix); }}
49
+ className={`flex-1 rounded-none ${className ?? ''}`}
50
+ />
51
+ <select
52
+ aria-label="Unit"
53
+ disabled={disabled}
54
+ value={suffix}
55
+ onChange={event => { setSuffix(event.target.value); emit(amount, event.target.value); }}
56
+ className="w-32 -ml-px border border-ibm-gray-30 bg-ibm-gray-10 px-2 text-sm text-ibm-gray-100 rounded-none focus:outline-none focus:border-ibm-blue-60 disabled:opacity-50"
57
+ >
58
+ {unitsFor(value, units).map(unit => (
59
+ <option key={unit.suffix} value={unit.suffix}>{unit.label}</option>
60
+ ))}
61
+ </select>
62
+ </div>
63
+ );
64
+ };
65
+
66
+ export default QuantityInput;
@@ -15,6 +15,7 @@ export { default as KV } from './KV'
15
15
  export { default as MemoryNumber } from './MemoryNumber'
16
16
  export { default as Numbers } from './Numbers'
17
17
  export { default as PVCSelect } from './PVCSelect'
18
+ export { default as QuantityInput } from './QuantityInput'
18
19
  export { default as PolicySelect } from './PolicySelect'
19
20
  export { default as SecretKeySelect } from './SecretKeySelect'
20
21
  export { default as SecretSelect } from './SecretSelect'
@@ -0,0 +1,50 @@
1
+ export interface QuantityUnit {
2
+ suffix: string
3
+ label: string
4
+ }
5
+
6
+ export const CPU_UNITS: QuantityUnit[] = [
7
+ { suffix: "m", label: "millicores" },
8
+ { suffix: "", label: "cores" },
9
+ ]
10
+
11
+ export const MEMORY_UNITS: QuantityUnit[] = [
12
+ { suffix: "Mi", label: "Mi" },
13
+ { suffix: "Gi", label: "Gi" },
14
+ ]
15
+
16
+ export const DISK_UNITS: QuantityUnit[] = [
17
+ { suffix: "Gi", label: "Gi" },
18
+ { suffix: "Mi", label: "Mi" },
19
+ { suffix: "Ti", label: "Ti" },
20
+ ]
21
+
22
+ export interface ParsedQuantity {
23
+ amount: string
24
+ unit: QuantityUnit
25
+ }
26
+
27
+ const QUANTITY = /^([0-9]*\.?[0-9]+)\s*([A-Za-z]*)$/
28
+
29
+ export function parseQuantity(value: unknown, units: QuantityUnit[]): ParsedQuantity {
30
+ const fallback = units[0]
31
+ if (value === undefined || value === null || value === "") return { amount: "", unit: fallback }
32
+
33
+ const match = QUANTITY.exec(String(value).trim())
34
+ if (!match) return { amount: "", unit: fallback }
35
+
36
+ const [, amount, suffix] = match
37
+ const unit = units.find(candidate => candidate.suffix === suffix) ?? { suffix, label: suffix }
38
+ return { amount, unit }
39
+ }
40
+
41
+ export function formatQuantity(amount: string, suffix: string): string {
42
+ const text = (amount ?? "").trim()
43
+ if (text === "" || Number.isNaN(Number(text))) return ""
44
+ return `${Number(text)}${suffix}`
45
+ }
46
+
47
+ export function unitsFor(value: unknown, units: QuantityUnit[]): QuantityUnit[] {
48
+ const { unit } = parseQuantity(value, units)
49
+ return units.some(candidate => candidate.suffix === unit.suffix) ? units : [...units, unit]
50
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@orsetra/shared-ui",
3
- "version": "1.10.26",
3
+ "version": "1.10.27",
4
4
  "description": "Shared UI components for Orsetra platform",
5
5
  "main": "./index.ts",
6
6
  "types": "./index.ts",