Skip to content

Commit

Permalink
feat: introduce flag for preserving the unit
Browse files Browse the repository at this point in the history
  • Loading branch information
Mayank-Tripathi32 committed Jan 16, 2025
1 parent 49eee39 commit 0c178dd
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
9 changes: 8 additions & 1 deletion packages/components/src/unit-control/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ function UnforwardedUnitControl(
value: valueProp,
onFocus: onFocusProp,
__shouldNotWarnDeprecated36pxSize,
shouldPreserveUnit = false,
...props
} = useDeprecated36pxDefaultSizeProp( unitControlProps );

Expand Down Expand Up @@ -136,7 +137,13 @@ function UnforwardedUnitControl(
typeof nextQuantityValue === 'undefined' ||
nextQuantityValue === null
) {
onChangeProp?.( `0${ unit }`, changeProps );
// Add a check if we should preserve the unit when the quantity is empty.
if ( shouldPreserveUnit ) {
onChangeProp?.( `0${ unit }`, changeProps );
return;
}

onChangeProp?.( '', changeProps );
return;
}

Expand Down
6 changes: 6 additions & 0 deletions packages/components/src/unit-control/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,4 +114,10 @@ export type UnitControlProps = Pick< InputControlProps, 'size' > &
* @ignore
*/
__shouldNotWarnDeprecated36pxSize?: boolean;
/**
* Whether to preserve the unit when the value is empty.
*
* @default false
*/
shouldPreserveUnit?: boolean;
};

0 comments on commit 0c178dd

Please sign in to comment.