Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UnitControl: preserve order of incoming units #62049

Closed
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -599,6 +599,7 @@ function BackgroundSizeControls( {
size="__unstable-large"
__unstableInputWidth="100px"
min={ 0 }
allowedUnitValues={ [ '%', 'px', 'em', 'rem', 'vw', 'vh' ] }
placeholder={ __( 'Auto' ) }
disabled={
currentValueForToggle !== 'auto' ||
Expand Down
26 changes: 23 additions & 3 deletions packages/components/src/unit-control/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@ import {
} from './utils';
import { useControlledState } from '../utils/hooks';
import { escapeRegExp } from '../utils/strings';
import type { UnitControlProps, UnitControlOnChangeCallback } from './types';
import type {
UnitControlProps,
UnitControlOnChangeCallback,
WPUnitControlUnit,
} from './types';
import { useDeprecated36pxDefaultSizeProp } from '../utils/use-deprecated-props';

function UnforwardedUnitControl(
Expand Down Expand Up @@ -53,6 +57,7 @@ function UnforwardedUnitControl(
size = 'default',
unit: unitProp,
units: unitsProp = CSS_UNITS,
allowedUnitValues,
value: valueProp,
onFocus: onFocusProp,
...props
Expand All @@ -71,11 +76,26 @@ function UnforwardedUnitControl(
// still passes `null` as a `value`.
const nonNullValueProp = valueProp ?? undefined;
const [ units, reFirstCharacterOfUnits ] = useMemo( () => {
const list = getUnitsWithCurrentUnit(
let list = getUnitsWithCurrentUnit(
nonNullValueProp,
unitProp,
unitsProp
);
// If allowedUnitValues is provided, we need to filter the list of units.
if ( allowedUnitValues?.length ) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So this is a huge hack and just here to illustrate the proposal.

The goal is for this component to honor the order of units. For example, if I wanted to have % appear at the top of the unit list.

I'm just not sure whether to do it in the component or as part of useCustomUnits.

#31822 (comment) suggests that useCustomUnits might be the wrong place to do it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cc @ciampo for advice - non-urgent, when you get time 🙇🏻

const filteredUnits: WPUnitControlUnit[] = [];
allowedUnitValues.forEach( ( allowedUnit ) => {
const filteredUnit = list.find(
( unit ) => unit.value === allowedUnit
);
if ( filteredUnit ) {
filteredUnits.push( filteredUnit );
}
} );
if ( filteredUnits.length ) {
list = filteredUnits;
}
}
const [ { value: firstUnitValue = '' } = {}, ...rest ] = list;
const firstCharacters = rest.reduce(
( carry, { value } ) => {
Expand All @@ -87,7 +107,7 @@ function UnforwardedUnitControl(
escapeRegExp( firstUnitValue.substring( 0, 1 ) )
);
return [ list, new RegExp( `^(?:${ firstCharacters })$`, 'i' ) ];
}, [ nonNullValueProp, unitProp, unitsProp ] );
}, [ nonNullValueProp, unitProp, unitsProp, allowedUnitValues ] );
const [ parsedQuantity, parsedUnit ] = getParsedQuantityAndUnit(
nonNullValueProp,
unitProp,
Expand Down
4 changes: 4 additions & 0 deletions packages/components/src/unit-control/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,4 +107,8 @@ export type UnitControlProps = Pick< InputControlProps, 'size' > &
* Callback when either the quantity or the unit inputs gains focus.
*/
onFocus?: FocusEventHandler< HTMLInputElement | HTMLSelectElement >;
/**
* A list of allowed unit values. If provided, only these values will be selectable.
*/
allowedUnitValues?: string[];
};
Loading