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:
@@ -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
|
||||
}
|
||||
|
Reference in New Issue
Block a user