Skip to content

Commit

Permalink
FIx: Jump on click and input bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgefilipecosta committed Sep 9, 2019
1 parent e8146d8 commit 52882ea
Showing 1 changed file with 60 additions and 26 deletions.
86 changes: 60 additions & 26 deletions packages/block-library/src/cover/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,49 @@ function retrieveFastAverageColor() {
return retrieveFastAverageColor.fastAverageColor;
}

const CoverHeightInput = withInstanceId(
function( { value = '', instanceId, onChange } ) {
const [ temporaryInput, setTemporaryInput ] = useState( null );
const onChangeEvent = useCallback(
( event ) => {
const unprocessedValue = event.target.value;
const inputValue = unprocessedValue !== '' ?
parseInt( event.target.value, 10 ) :
undefined;
if ( ( isNaN( inputValue ) || inputValue < COVER_MIN_HEIGHT ) && inputValue !== undefined ) {
setTemporaryInput( event.target.value );
return;
}
setTemporaryInput( null );
onChange( inputValue );
},
[ onChange, setTemporaryInput ]
);
const onBlurEvent = useCallback(
() => {
if ( temporaryInput !== null ) {
setTemporaryInput( null );
}
},
[ temporaryInput, setTemporaryInput ]
);
const inputId = `block-cover-height-input-${ instanceId }`;
return (
<BaseControl label={ __( 'Height in pixels' ) } id={ inputId }>
<input
type="number"
id={ inputId }
onChange={ onChangeEvent }
onBlur={ onBlurEvent }
value={ temporaryInput !== null ? temporaryInput : value }
min={ COVER_MIN_HEIGHT }
step="10"
/>
</BaseControl>
);
}
);

const RESIZABLE_BOX_ENABLE_OPTION = {
top: false,
right: false,
Expand All @@ -94,14 +137,17 @@ function ResizableCover( {
const onResizeEvent = useCallback(
( event, direction, elt ) => {
onResize( elt.clientHeight );
if ( ! isResizing ) {
setIsResizing( true );
}
},
[ onResize ],
[ onResize, setIsResizing ],
);
const onResizeStartEvent = useCallback(
() => {
setIsResizing( true );
( event, direction, elt ) => {
onResize( elt.clientHeight );
},
[ setIsResizing ]
[ onResize ]
);
const onResizeStopEvent = useCallback(
( event, direction, elt ) => {
Expand Down Expand Up @@ -159,7 +205,6 @@ class CoverEdit extends Component {

render() {
const {
instanceId,
attributes,
setAttributes,
isSelected,
Expand Down Expand Up @@ -230,13 +275,13 @@ class CoverEdit extends Component {
{}
),
backgroundColor: overlayColor.color,
minHeight: ( temporaryMinHeight || minHeight ) + 'px',
minHeight: ( temporaryMinHeight || minHeight ),
};

if ( focalPoint ) {
style.backgroundPosition = `${ focalPoint.x * 100 }% ${ focalPoint.y * 100 }%`;
}
const inputId = `block-cover-height-input-${ instanceId }`;

const controls = (
<>
<BlockControls>
Expand Down Expand Up @@ -280,27 +325,16 @@ class CoverEdit extends Component {
onChange={ ( value ) => setAttributes( { focalPoint: value } ) }
/>
) }
<BaseControl label={ __( 'Height in pixels' ) } id={ inputId }>
<input
type="number"
id={ inputId }
onChange={ ( event ) => {
const coverMinHeight = parseInt( event.target.value, 10 );
if ( isNaN( coverMinHeight ) ) {
setAttributes( { minHeight: undefined } );
return;
}
<CoverHeightInput
value={ temporaryMinHeight || minHeight }
onChange={
( value ) => {
setAttributes( {
minHeight: coverMinHeight > COVER_MIN_HEIGHT ?
coverMinHeight :
COVER_MIN_HEIGHT,
minHeight: value,
} );
} }
value={ temporaryMinHeight || minHeight }
min={ COVER_MIN_HEIGHT }
step="10"
/>
</BaseControl>
}
}
/>
<PanelRow>
<Button
isDefault
Expand Down

0 comments on commit 52882ea

Please sign in to comment.