Merge multiple event handlers into one Raises a ValueError if any handlers have conflicting :attr:`~reactpy.core.proto.EventHandlerType.stop_propagation` or :attr:`~reactpy.core.proto.EventHandlerType.prevent_default` attributes.
(
event_handlers: Sequence[EventHandlerType],
)
| 163 | |
| 164 | |
| 165 | def merge_event_handlers( |
| 166 | event_handlers: Sequence[EventHandlerType], |
| 167 | ) -> EventHandlerType: |
| 168 | """Merge multiple event handlers into one |
| 169 | |
| 170 | Raises a ValueError if any handlers have conflicting |
| 171 | :attr:`~reactpy.core.proto.EventHandlerType.stop_propagation` or |
| 172 | :attr:`~reactpy.core.proto.EventHandlerType.prevent_default` attributes. |
| 173 | """ |
| 174 | if not event_handlers: |
| 175 | msg = "No event handlers to merge" |
| 176 | raise ValueError(msg) |
| 177 | elif len(event_handlers) == 1: |
| 178 | return event_handlers[0] |
| 179 | |
| 180 | first_handler = event_handlers[0] |
| 181 | |
| 182 | stop_propagation = first_handler.stop_propagation |
| 183 | prevent_default = first_handler.prevent_default |
| 184 | target = first_handler.target |
| 185 | |
| 186 | for handler in event_handlers: |
| 187 | if ( |
| 188 | handler.stop_propagation != stop_propagation |
| 189 | or handler.prevent_default != prevent_default |
| 190 | or handler.target != target |
| 191 | ): |
| 192 | msg = "Cannot merge handlers - 'stop_propagation', 'prevent_default' or 'target' mismatch." |
| 193 | raise ValueError(msg) |
| 194 | |
| 195 | return EventHandler( |
| 196 | merge_event_handler_funcs([h.function for h in event_handlers]), |
| 197 | stop_propagation, |
| 198 | prevent_default, |
| 199 | target, |
| 200 | ) |
| 201 | |
| 202 | |
| 203 | def merge_event_handler_funcs( |