-
-
Notifications
You must be signed in to change notification settings - Fork 755
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3133 from PaulSinghDev/main
Allow the <LinkFloatingToolbar /> component to be submitted with the 'enter' key when the <PlateEditor /> is contained within a <form></form>
- Loading branch information
Showing
9 changed files
with
132 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@udecode/plate-utils": patch | ||
--- | ||
|
||
`useFormInputProps`: Generic form input props inside an editor |
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
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
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
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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
/** | ||
* @file Automatically generated by barrelsby. | ||
*/ | ||
|
||
export * from './setSelectionInlineEquation'; |
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
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
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 |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { useFormInputProps } from './useFormInputProps'; | ||
|
||
describe('useFormInputProps', () => { | ||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('will return an object with a props key regardless of whether the user provides a callback or sets preventDefaultOnEnterKeydown to true', () => { | ||
const output = useFormInputProps(); | ||
expect(output.props).toBeDefined(); | ||
expect(Object.keys(output.props)).toHaveLength(0); | ||
}); | ||
|
||
it('will return a callback for onKeyDownCapture when preventDefaultOnEnterKeydown is true', () => { | ||
const output = useFormInputProps({ preventDefaultOnEnterKeydown: true }); | ||
expect(output.props).toBeDefined(); | ||
expect(Object.keys(output.props)).toHaveLength(1); | ||
expect(output.props.onKeyDownCapture).toBeDefined(); | ||
expect(output.props.onKeyDownCapture instanceof Function).toBe(true); | ||
}); | ||
|
||
it('will call event.preventDefault if the key is enter, and only if the key is enter', () => { | ||
// Define mock for preventdefault | ||
const preventDefaultMock = jest.fn(); | ||
// should trigger preventDefault | ||
const eventWithEKeyEnter = { | ||
key: 'Enter', | ||
preventDefault: preventDefaultMock, | ||
} as any; | ||
// should trigger preventDefault | ||
const eventWithKeyCode13 = { | ||
keyCode: 13, | ||
preventDefault: preventDefaultMock, | ||
} as any; | ||
// should not trigger preventDefault | ||
const eventWithIrrelevantKey = { | ||
keyCode: 30, | ||
preventDefault: preventDefaultMock, | ||
} as any; | ||
|
||
const output = useFormInputProps({ | ||
preventDefaultOnEnterKeydown: true, | ||
}); | ||
|
||
// call with enter key | ||
output.props?.onKeyDownCapture?.(eventWithEKeyEnter); | ||
expect(preventDefaultMock).toHaveBeenCalledTimes(1); | ||
|
||
// call with keyCode 13 | ||
output.props?.onKeyDownCapture?.(eventWithKeyCode13); | ||
expect(preventDefaultMock).toHaveBeenCalledTimes(2); | ||
|
||
// call with irrelevant key | ||
output.props?.onKeyDownCapture?.(eventWithIrrelevantKey); | ||
expect(preventDefaultMock).toHaveBeenCalledTimes(2); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -0,0 +1,56 @@ | ||
interface InputProps { | ||
/** | ||
* Should we activate the onKeyDownCapture handler to preventDefault when | ||
* the user presses enter? | ||
*/ | ||
preventDefaultOnEnterKeydown?: boolean; | ||
} | ||
|
||
/** | ||
* Hook to allow the user to spread a set of predefined props to the Div wrapper | ||
* of an Input element | ||
* | ||
* @param param0 an options object which can be expanded to add further functionality | ||
* @returns a props object which can be spread onto the element | ||
*/ | ||
export const useFormInputProps = (options?: InputProps) => { | ||
// Nothing provided to just return an empty object which can still be spread. | ||
// If we need to add more functionality later we will still be able to do so | ||
if (!options) return { props: {} }; | ||
|
||
// Destructure our options so we can use them | ||
const { preventDefaultOnEnterKeydown } = options; | ||
|
||
/** | ||
* Handle the keydown capture event and prevent the default behaviour when the | ||
* user presses enter. | ||
* | ||
* In the event the user presses enter on a field such as a link, prior to | ||
* filling in both label and url, the default behaviour is to submit the form. | ||
* This, ultimately, results in no link being added as you need to fill both | ||
* fields to pass validation. | ||
* | ||
* By calling preventDefault we short circuit the form's submission thus | ||
* allowing the user to continue filling in the other fields | ||
* | ||
* @param e The original event which was provided by the VDOM | ||
* implement their own behaviour on this event | ||
*/ | ||
const handleEnterKeydownCapture = ( | ||
e: React.KeyboardEvent<HTMLDivElement> | ||
) => { | ||
// Prevent the form from submitting | ||
if (e.key === 'Enter' || e.keyCode === 13) { | ||
e.preventDefault(); | ||
} | ||
}; | ||
|
||
return { | ||
props: { | ||
onKeyDownCapture: preventDefaultOnEnterKeydown | ||
? (e: React.KeyboardEvent<HTMLDivElement>) => | ||
handleEnterKeydownCapture(e) | ||
: undefined, | ||
}, | ||
}; | ||
}; |