-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Improve reusable block creation flow #5428
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
* WordPress dependencies | ||
*/ | ||
import { Button } from '@wordpress/components'; | ||
import { Component, Fragment } from '@wordpress/element'; | ||
import { __ } from '@wordpress/i18n'; | ||
import { keycodes } from '@wordpress/utils'; | ||
|
||
|
@@ -10,66 +11,97 @@ import { keycodes } from '@wordpress/utils'; | |
*/ | ||
import './style.scss'; | ||
|
||
/** | ||
* Module constants | ||
*/ | ||
const { ESCAPE } = keycodes; | ||
|
||
function ReusableBlockEditPanel( props ) { | ||
const { isEditing, title, isSaving, onEdit, onChangeTitle, onSave, onCancel } = props; | ||
class ReusableBlockEditPanel extends Component { | ||
constructor() { | ||
super( ...arguments ); | ||
|
||
this.bindTitleRef = this.bindTitleRef.bind( this ); | ||
this.handleFormSubmit = this.handleFormSubmit.bind( this ); | ||
this.handleTitleChange = this.handleTitleChange.bind( this ); | ||
this.handleTitleKeyDown = this.handleTitleKeyDown.bind( this ); | ||
} | ||
|
||
componentDidMount() { | ||
if ( this.props.isEditing ) { | ||
this.titleRef.select(); | ||
} | ||
} | ||
|
||
bindTitleRef( ref ) { | ||
this.titleRef = ref; | ||
} | ||
|
||
return [ | ||
( ! isEditing && ! isSaving ) && ( | ||
<div key="view" className="reusable-block-edit-panel"> | ||
<span className="reusable-block-edit-panel__info"> | ||
<b>{ title }</b> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Of course not relevant to this pull request, but was
https://developer.mozilla.org/en-US/docs/Web/HTML/Element/b Further, do we need both the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Eliminated the unnecessary |
||
</span> | ||
<Button | ||
isLarge | ||
className="reusable-block-edit-panel__button" | ||
onClick={ onEdit }> | ||
{ __( 'Edit' ) } | ||
</Button> | ||
</div> | ||
), | ||
( isEditing || isSaving ) && ( | ||
<form | ||
key="edit" | ||
className="reusable-block-edit-panel" | ||
onSubmit={ ( event ) => { | ||
event.preventDefault(); | ||
onSave(); | ||
} }> | ||
<input | ||
type="text" | ||
disabled={ isSaving } | ||
className="reusable-block-edit-panel__title" | ||
value={ title } | ||
onChange={ ( event ) => onChangeTitle( event.target.value ) } | ||
onKeyDown={ ( event ) => { | ||
if ( event.keyCode === ESCAPE ) { | ||
event.stopPropagation(); | ||
onCancel(); | ||
} | ||
} } /> | ||
<Button | ||
type="submit" | ||
isPrimary | ||
isLarge | ||
isBusy={ isSaving } | ||
disabled={ ! title || isSaving } | ||
className="reusable-block-edit-panel__button" | ||
onClick={ onSave }> | ||
{ __( 'Save' ) } | ||
</Button> | ||
<Button | ||
isLarge | ||
disabled={ isSaving } | ||
className="reusable-block-edit-panel__button" | ||
onClick={ onCancel }> | ||
{ __( 'Cancel' ) } | ||
</Button> | ||
</form> | ||
), | ||
]; | ||
handleFormSubmit( event ) { | ||
event.preventDefault(); | ||
this.props.onSave(); | ||
} | ||
|
||
handleTitleChange( event ) { | ||
this.props.onChangeTitle( event.target.value ); | ||
} | ||
|
||
handleTitleKeyDown( event ) { | ||
if ( event.keyCode === ESCAPE ) { | ||
event.stopPropagation(); | ||
this.props.onCancel(); | ||
} | ||
} | ||
|
||
render() { | ||
const { isEditing, title, isSaving, onEdit, onSave, onCancel } = this.props; | ||
|
||
return ( | ||
<Fragment> | ||
{ ( ! isEditing && ! isSaving ) && ( | ||
<div className="reusable-block-edit-panel"> | ||
<b className="reusable-block-edit-panel__info"> | ||
{ title } | ||
</b> | ||
<Button isLarge className="reusable-block-edit-panel__button" onClick={ onEdit }> | ||
{ __( 'Edit' ) } | ||
</Button> | ||
</div> | ||
) } | ||
{ ( isEditing || isSaving ) && ( | ||
<form className="reusable-block-edit-panel" onSubmit={ this.handleFormSubmit }> | ||
<input | ||
ref={ this.bindTitleRef } | ||
type="text" | ||
disabled={ isSaving } | ||
className="reusable-block-edit-panel__title" | ||
value={ title } | ||
onChange={ this.handleTitleChange } | ||
onKeyDown={ this.handleTitleKeyDown } | ||
/> | ||
<Button | ||
type="submit" | ||
isPrimary | ||
isLarge | ||
isBusy={ isSaving } | ||
disabled={ ! title || isSaving } | ||
className="reusable-block-edit-panel__button" | ||
onClick={ onSave } | ||
> | ||
{ __( 'Save' ) } | ||
</Button> | ||
<Button | ||
isLarge | ||
disabled={ isSaving } | ||
className="reusable-block-edit-panel__button" | ||
onClick={ onCancel } | ||
> | ||
{ __( 'Cancel' ) } | ||
</Button> | ||
</form> | ||
) } | ||
</Fragment> | ||
); | ||
} | ||
} | ||
|
||
export default ReusableBlockEditPanel; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FWIW I don't think it's so bad in this case, because it's fairly obvious what
ESCAPE
is, but I dislike the idea of a "Module constants" docblock in general, because it normalizes the idea that we don't need to document variables we define, and can instead apply a blanket grouping to them.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll leave this for now since we do this in a few places and I'd prefer we're somewhat consistent. It might be worth going through and auditing our usage of this pattern, since you raise a really good point.