Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion packages/core/src/components/Grid/GridCellHueHeader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,11 @@ const AngleInput = memo(function AngleInput({ hueId }: HueComponentProps) {
kind="ghost"
label={LABEL_HUE}
placeholder={PLACEHOLDER_HUE}
inputMode="numeric"
inputMode="decimal"
min={HUE_MIN_ANGLE}
max={HUE_MAX_ANGLE}
precision={1}
trimTrailingZeros
loopControls
value={angle}
title={HINT_DEGREE}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,21 @@ type WithNumericIncrementControlsProps = {
* Whether to loop the value when reaching the min/max bounds.
*/
loopControls?: boolean;
/**
* When true, trailing zeros after the decimal point are removed.
* E.g., with precision=1: 180 → "180" instead of "180.0", but 180.5 → "180.5".
*/
trimTrailingZeros?: boolean;
};

function formatWithPrecision(value: string | number, precision: number): string {
function formatWithPrecision(
value: string | number,
precision: number,
trimTrailingZeros: boolean,
): string {
const number = Number(value);
return (Number.isNaN(number) ? 0 : number).toFixed(precision);
const formatted = (Number.isNaN(number) ? 0 : number).toFixed(precision);
return trimTrailingZeros ? formatted.replace(/\.?0+$/, "") : formatted;
}

function isNumericInputMode(inputMode: string) {
Expand Down Expand Up @@ -115,6 +125,7 @@ export function withNumericIncrementControls<P extends InputProps>(
baseValue,
precision = -Math.log10(step),
loopControls,
trimTrailingZeros = false,
...props
}: WithNumericIncrementControlsProps & P) => {
const { onChange, onBlur } = props;
Expand Down Expand Up @@ -150,10 +161,10 @@ export function withNumericIncrementControls<P extends InputProps>(
return;
}

input.value = formatWithPrecision(newValue, precision);
input.value = formatWithPrecision(newValue, precision, trimTrailingZeros);
onChange?.(createChangeEvent(input));
},
[onChange, precision, loopControls, baseValue, step],
[onChange, precision, loopControls, baseValue, step, trimTrailingZeros],
);

useEffect(() => {
Expand Down Expand Up @@ -231,15 +242,15 @@ export function withNumericIncrementControls<P extends InputProps>(

if (!input?.value) return;

const formattedValue = formatWithPrecision(input.value, precision);
const formattedValue = formatWithPrecision(input.value, precision, trimTrailingZeros);
if (input.value !== formattedValue) {
input.value = formattedValue;
onChange?.(createChangeEvent(input));
}

onBlur?.(e);
},
[onBlur, onChange, precision],
[onBlur, onChange, precision, trimTrailingZeros],
);

return (
Expand Down
Loading