1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-08-12 10:14:02 +02:00

Allow control of Slate's event handler execution in custom event handler API (#4299)

Co-authored-by: Georg Berecz <gberecz@palantir.com>
Co-authored-by: Claudéric Demers <clauderic.d@gmail.com>
This commit is contained in:
georgberecz
2021-06-01 16:26:08 +02:00
committed by GitHub
parent dfc039601f
commit 2c17e2b7f9
3 changed files with 112 additions and 4 deletions

View File

@@ -1191,13 +1191,19 @@ export const isEventHandled = <
EventType extends React.SyntheticEvent<unknown, unknown>
>(
event: EventType,
handler?: (event: EventType) => void
handler?: (event: EventType) => void | boolean
) => {
if (!handler) {
return false
}
// The custom event handler may return a boolean to specify whether the event
// shall be treated as being handled or not.
const shouldTreatEventAsHandled = handler(event)
if (shouldTreatEventAsHandled != null) {
return shouldTreatEventAsHandled
}
handler(event)
return event.isDefaultPrevented() || event.isPropagationStopped()
}
@@ -1207,12 +1213,19 @@ export const isEventHandled = <
export const isDOMEventHandled = <E extends Event>(
event: E,
handler?: (event: E) => void
handler?: (event: E) => void | boolean
) => {
if (!handler) {
return false
}
handler(event)
// The custom event handler may return a boolean to specify whether the event
// shall be treated as being handled or not.
const shouldTreatEventAsHandled = handler(event)
if (shouldTreatEventAsHandled != null) {
return shouldTreatEventAsHandled
}
return event.defaultPrevented
}