UnitControl 是一个实验性的 WordPress 组件,允许用户设置数值和单位(如 px)。它主要用于处理带单位的数值输入,支持自定义单位列表和回调函数。
import { useState } from 'react';
import { __experimentalUnitControl as UnitControl } from '@wordpress/components';
const Example = () => {
const [ value, setValue ] = useState( '10px' );
const units = [
{ value: 'px', label: 'px', default: 0 },
{ value: '%', label: '%', default: 10 },
{ value: 'em', label: 'em', default: 0 },
];
return (
<UnitControl __next40pxDefaultSize onChange={ setValue } value={ value } units={ units } />
);
};UnitControl allows the user to set a numeric quantity as well as a unit (e.g. px).
import { useState } from 'react';
import { __experimentalUnitControl as UnitControl } from '@wordpress/components';
const Example = () => {
const [ value, setValue ] = useState( '10px' );
return <UnitControl __next40pxDefaultSize onChange={ setValue } value={ value } />;
};
disableUnits: booleanIf true, the unit <select> is hidden.
falseisPressEnterToChange: booleanIf true, the ENTER key press is required in order to trigger an onChange. If enabled, a change is also triggered when tabbing away (onBlur).
falseisResetValueOnUnitChange: booleanIf true, and the selected unit provides a default value, this value is set when changing units.
falseisUnitSelectTabbable: booleanDetermines if the unit <select> is tabbable.
truelabel: stringIf this property is added, a label will be generated using label property as the content.
labelPosition: stringThe position of the label (top, side, bottom, or edge).
onBlur: FocusEventHandler< HTMLInputElement | HTMLSelectElement >Callback invoked when either the quantity or unit inputs fire the blur event.
onFocus: FocusEventHandler< HTMLInputElement | HTMLSelectElement >Callback invoked when either the quantity or unit inputs fire the focus event.
onChange: UnitControlOnChangeCallbackCallback when the value changes.
onUnitChange: UnitControlOnChangeCallbackCallback when the unit changes.
size: stringAdjusts the size of the input.
Sizes include: default, small
defaultunit: stringDeprecated: Current unit value.
Instead, provide a unit with a value through the value prop.
Example:
<UnitControl value="50%" />
units: WPUnitControlUnit[]Collection of available units.
Example:
import { useState } from 'react';
import { __experimentalUnitControl as UnitControl } from '@wordpress/components';
const Example = () => {
const [ value, setValue ] = useState( '10px' );
const units = [
{ value: 'px', label: 'px', default: 0 },
{ value: '%', label: '%', default: 10 },
{ value: 'em', label: 'em', default: 0 },
];
return (
<UnitControl __next40pxDefaultSize onChange={ setValue } value={ value } units={ units } />
);
};
A default value (in the example above, 10 for %), if defined, is set as the new value when a unit changes. This is helpful in scenarios where changing a unit may cause drastic results, such as changing from px to vh.
value: number | stringCurrent value. If passed as a string, the current unit will be inferred from this value.
For example, a value of 50% will set the current unit to %.
Example:
<UnitControl __next40pxDefaultSize value="50%" />
__next40pxDefaultSize: booleanStart opting into the larger default height that will become the default size in a future version.
false