Repository navigation
Edit in place hook #28924
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
Closed
Closed
Edit in place hook #28924
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
1ad3f18
add component logic
3c1191f
add missing callbacks: onKedyDown and onChange
6d554ca
component story added
044cac4
remove input styles for now
18ef7af
size property added
9793a56
edit in place changed to hook
6b2a39b
component converted into hook
3c30e6d
linter fix
0841aa9
Update packages/components/src/index.js
grzim a584a12
add anotations
e98de96
merge with master
533d7b7
remove mistakenly doubled line
5faf3cc
update types
c8e1ef1
add react synthetic events types
0e19059
Update packages/components/src/inline-edit/hook.js
grzim 0fe9119
Update packages/components/src/inline-edit/hook.js
grzim 5bf4f8e
Update packages/components/src/inline-edit/hook.js
grzim be80620
hooks logic simplified, validation called only on commit
dfbe4d6
hooks logic simplified, validation called only on commit
face180
merged with changes proposed in CR
c9895d0
synch internal value on chnage from the outside
c2cf674
update on initialValue change
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| Hook providing with refs to bind two elements. | ||
|
|
||
| One ref is used to set 'isEdit' state to true upon clicking. The second is used to set 'isEdit' state to false on 'Enter'/onBlur event and triggering onCommit callback with its value. | ||
|
|
||
| The hook was designed to be a component in the first place, but a hook gives more flexibility. |
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,129 @@ | ||
| /** | ||
| * External dependencies | ||
| */ | ||
| import { isUndefined, negate, noop, flow } from 'lodash'; | ||
| /** | ||
| * WordPress dependencies | ||
| */ | ||
| import { useEffect, useState, useRef } from '@wordpress/element'; | ||
|
|
||
| /** | ||
| * @param {import("react").ChangeEvent<HTMLInputElement>} event | ||
| */ | ||
| const cancelEvent = ( event ) => ( | ||
| event.preventDefault(), event.stopPropagation(), event | ||
| ); | ||
|
|
||
| /** | ||
| * @param {import("react").ChangeEvent<HTMLInputElement>} event | ||
| */ | ||
| const getEventValue = ( { target: { value } } ) => value; | ||
|
|
||
| /** | ||
| * @param {Function[]} handlers | ||
| */ | ||
| const mergeEvent = ( ...handlers ) => | ||
| /** | ||
| * @param {import("react").ChangeEvent<HTMLInputElement>} event | ||
| */ | ||
| ( event ) => handlers.forEach( ( handler = noop ) => handler( event ) ); | ||
|
|
||
| /** | ||
| * @typedef Props | ||
| * @property {(value: string) => boolean} validate predicate | ||
| * @property {( value: string ) => void} onWrongInput called when a validate predicate fails | ||
| * @property {( value: string ) => void} onCommit called on enter/blur | ||
| * @property {string} value input value | ||
| */ | ||
|
|
||
| /** | ||
| * @param {Props} props | ||
| */ | ||
| export default function useInlineEdit( { | ||
| validate = negate( isUndefined ), | ||
| onWrongInput = noop, | ||
| onCommit = noop, | ||
| value: propValue, | ||
| } ) { | ||
| const [ isInEditMode, setIsInEditMode ] = useState( false ); | ||
| const [ editingValue, setEditingValue ] = useState( propValue ); | ||
| const value = isInEditMode ? editingValue : propValue; | ||
| /** @type {import('react').RefObject<HTMLInputElement>} */ | ||
| const inputRef = useRef( null ); | ||
| /** @type {import('react').RefObject<HTMLButtonElement>} */ | ||
| const toggleRef = useRef( null ); | ||
| const changeToEditMode = () => setIsInEditMode( true ); | ||
| const changeToToggleMode = () => setIsInEditMode( false ); | ||
|
|
||
| useEffect( () => { | ||
| setEditingValue( propValue ); | ||
| }, [ propValue ] ); | ||
|
|
||
| useEffect( () => { | ||
| if ( isInEditMode ) { | ||
| inputRef.current?.focus(); | ||
| inputRef.current?.select(); | ||
| } else { | ||
| toggleRef.current?.focus(); | ||
| } | ||
| }, [ isInEditMode ] ); | ||
|
|
||
| /** | ||
| * @param {import("react").ChangeEvent<HTMLInputElement>} event | ||
| */ | ||
| const commit = ( event ) => { | ||
| const { value: _value } = event.target; | ||
| cancelEvent( event ); | ||
| if ( validate( _value ) ) { | ||
| changeToToggleMode(); | ||
| onCommit( _value ); | ||
| } else { | ||
| onWrongInput( _value ); | ||
| } | ||
| }; | ||
|
|
||
| /** | ||
| * @param {import("react").KeyboardEvent<HTMLInputElement> & import("react").ChangeEvent<HTMLInputElement>} event | ||
|
sarayourfriend marked this conversation as resolved.
Resolved
|
||
| */ | ||
| const handleInputActions = ( event ) => { | ||
| if ( 'Enter' === event.key ) { | ||
| commit( event ); | ||
| } | ||
| if ( 'Escape' === event.key ) { | ||
| cancelEvent( event ); | ||
| event.target.blur(); | ||
| changeToToggleMode(); | ||
| } else { | ||
| setEditingValue( event.target.value ); | ||
| } | ||
| }; | ||
|
|
||
| const amendInputProps = ( { | ||
| onChange = noop, | ||
| onKeyDown = noop, | ||
| onBlur = noop, | ||
| ...inputProps | ||
| } = {} ) => ( { | ||
| ref: inputRef, | ||
| onChange: mergeEvent( | ||
| flow( [ getEventValue, setEditingValue ] ), | ||
| onChange | ||
| ), | ||
| onKeyDown: mergeEvent( handleInputActions, onKeyDown ), | ||
| onBlur: mergeEvent( commit, onBlur ), | ||
| ...inputProps, | ||
| } ); | ||
|
|
||
| const amendToggleProps = ( { onClick = noop, ...toggleProps } = {} ) => ( { | ||
| ref: toggleRef, | ||
| onClick: mergeEvent( changeToEditMode, onClick ), | ||
| ...toggleProps, | ||
| } ); | ||
|
|
||
| return { | ||
| isEdit: isInEditMode, | ||
| amendInputProps, | ||
| amendToggleProps, | ||
| value, | ||
| }; | ||
| } | ||
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| export { default as useInlineEdit } from './hook'; |
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| /** | ||
| * External dependencies | ||
| */ | ||
| import { text } from '@storybook/addon-knobs'; | ||
| /** | ||
| * WordPress dependencies | ||
| */ | ||
| import { useEffect, useState } from '@wordpress/element'; | ||
| import { pick } from 'lodash'; | ||
|
|
||
| /** | ||
| * Internal dependencies | ||
| */ | ||
| import useInlineEdit from '../hook'; | ||
|
|
||
| export default { | ||
| title: 'Components/EditInPlaceControl', | ||
| component: MyCustomEditInPlaceControl, | ||
| }; | ||
|
|
||
| function MyCustomEditInPlaceControl( props = {} ) { | ||
| const { isEdit, amendInputProps, amendToggleProps, value } = useInlineEdit( | ||
| props | ||
| ); | ||
| return ( | ||
| <> | ||
| { isEdit ? ( | ||
| <input { ...amendInputProps( { value } ) } /> | ||
| ) : ( | ||
| <button { ...amendToggleProps( pick( props, 'onClick' ) ) }> | ||
| { value } | ||
| </button> | ||
| ) } | ||
| </> | ||
| ); | ||
| } | ||
|
|
||
| export const _default = () => { | ||
| const validate = ( _value = '' ) => _value.length > 0; | ||
| const initialValue = text( 'Initial value', 'Input initial value' ); | ||
| const [ onClickCallbacks, setOnClickCallbacks ] = useState( 0 ); | ||
| const [ ocCommitCallbacks, setOnCommitCallbacks ] = useState( 0 ); | ||
| const [ value, setValue ] = useState( initialValue ); | ||
| const [ isInputValid, setIsInputValid ] = useState( true ); | ||
|
|
||
| useEffect( () => { | ||
| setValue( initialValue ); | ||
| }, [ initialValue ] ); | ||
|
|
||
| const incrementOnClickCallbacks = () => | ||
| setOnClickCallbacks( 1 + onClickCallbacks ); | ||
|
|
||
| const incrementOnCommitCallbacks = () => | ||
| setOnCommitCallbacks( 1 + ocCommitCallbacks ); | ||
|
|
||
| const props = { | ||
| value, | ||
| validate, | ||
| onClick: incrementOnClickCallbacks, | ||
| inputValidator: validate, | ||
| onCommit: ( _value ) => ( | ||
| setValue( _value ), | ||
| incrementOnCommitCallbacks(), | ||
| setIsInputValid( true ) | ||
| ), | ||
| onWrongInput: () => setIsInputValid( false ), | ||
| }; | ||
|
|
||
| return ( | ||
| <> | ||
| <MyCustomEditInPlaceControl { ...props } /> | ||
| <ul> | ||
| <li> onClick callbacks: { onClickCallbacks } </li> | ||
| <li> onCommit callback: { ocCommitCallbacks } </li> | ||
| <li> inputValidator: is defined </li> | ||
| <li> is commited input valid: { isInputValid + '' } </li> | ||
| </ul> | ||
| </> | ||
| ); | ||
| }; |
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.