-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
481 additions
and
497 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 54 additions & 34 deletions
88
packages/components/src/box-control/all-input-control.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,86 +1,106 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useInstanceId } from '@wordpress/compose'; | ||
/** | ||
* Internal dependencies | ||
*/ | ||
import type { UnitControlProps } from '../unit-control/types'; | ||
import { | ||
FlexedRangeControl, | ||
StyledUnitControl, | ||
} from './styles/box-control-styles'; | ||
import { HStack } from '../h-stack'; | ||
import type { BoxControlInputControlProps } from './types'; | ||
import UnitControl from './unit-control'; | ||
import { parseQuantityAndUnitFromRawValue } from '../unit-control'; | ||
import { | ||
LABELS, | ||
applyValueToSides, | ||
getAllValue, | ||
isValuesMixed, | ||
isValuesDefined, | ||
CUSTOM_VALUE_SETTINGS, | ||
} from './utils'; | ||
|
||
const noop = () => {}; | ||
|
||
export default function AllInputControl( { | ||
onChange = noop, | ||
onFocus = noop, | ||
onHoverOn = noop, | ||
onHoverOff = noop, | ||
values, | ||
sides, | ||
selectedUnits, | ||
setSelectedUnits, | ||
...props | ||
}: BoxControlInputControlProps ) { | ||
const inputId = useInstanceId( AllInputControl, 'box-control-input-all' ); | ||
|
||
const allValue = getAllValue( values, selectedUnits, sides ); | ||
const hasValues = isValuesDefined( values ); | ||
const isMixed = hasValues && isValuesMixed( values, selectedUnits, sides ); | ||
const allPlaceholder = isMixed ? LABELS.mixed : undefined; | ||
|
||
const [ parsedQuantity, parsedUnit ] = | ||
parseQuantityAndUnitFromRawValue( allValue ); | ||
|
||
const handleOnFocus: React.FocusEventHandler< HTMLInputElement > = ( | ||
event | ||
) => { | ||
onFocus( event, { side: 'all' } ); | ||
}; | ||
|
||
const handleOnChange: UnitControlProps[ 'onChange' ] = ( next ) => { | ||
const onValueChange = ( next?: string ) => { | ||
const isNumeric = next !== undefined && ! isNaN( parseFloat( next ) ); | ||
const nextValue = isNumeric ? next : undefined; | ||
const nextValues = applyValueToSides( values, nextValue, sides ); | ||
|
||
onChange( nextValues ); | ||
}; | ||
|
||
const sliderOnChange = ( next?: number ) => { | ||
onValueChange( | ||
next !== undefined ? [ next, parsedUnit ].join( '' ) : undefined | ||
); | ||
}; | ||
|
||
// Set selected unit so it can be used as fallback by unlinked controls | ||
// when individual sides do not have a value containing a unit. | ||
const handleOnUnitChange: UnitControlProps[ 'onUnitChange' ] = ( unit ) => { | ||
const newUnits = applyValueToSides( selectedUnits, unit, sides ); | ||
setSelectedUnits( newUnits ); | ||
}; | ||
|
||
const handleOnHoverOn = () => { | ||
onHoverOn( { | ||
top: true, | ||
bottom: true, | ||
left: true, | ||
right: true, | ||
} ); | ||
}; | ||
|
||
const handleOnHoverOff = () => { | ||
onHoverOff( { | ||
top: false, | ||
bottom: false, | ||
left: false, | ||
right: false, | ||
} ); | ||
}; | ||
|
||
return ( | ||
<UnitControl | ||
{ ...props } | ||
disableUnits={ isMixed } | ||
isOnly | ||
value={ allValue } | ||
onChange={ handleOnChange } | ||
onUnitChange={ handleOnUnitChange } | ||
onFocus={ handleOnFocus } | ||
onHoverOn={ handleOnHoverOn } | ||
onHoverOff={ handleOnHoverOff } | ||
placeholder={ allPlaceholder } | ||
/> | ||
<HStack> | ||
<StyledUnitControl | ||
{ ...props } | ||
className="component-box-control__unit-control" | ||
disableUnits={ isMixed } | ||
id={ inputId } | ||
isPressEnterToChange | ||
value={ allValue } | ||
onChange={ onValueChange } | ||
onUnitChange={ handleOnUnitChange } | ||
onFocus={ handleOnFocus } | ||
placeholder={ allPlaceholder } | ||
label={ LABELS.all } | ||
hideLabelFromVision | ||
/> | ||
|
||
<FlexedRangeControl | ||
__nextHasNoMarginBottom | ||
aria-controls={ inputId } | ||
label={ LABELS.all } | ||
hideLabelFromVision | ||
onChange={ sliderOnChange } | ||
min={ 0 } | ||
max={ CUSTOM_VALUE_SETTINGS[ parsedUnit ?? 'px' ]?.max ?? 10 } | ||
step={ | ||
CUSTOM_VALUE_SETTINGS[ parsedUnit ?? 'px' ]?.step ?? 0.1 | ||
} | ||
value={ parsedQuantity ?? 0 } | ||
withInputField={ false } | ||
/> | ||
</HStack> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.