Skip to content

Commit

Permalink
Allow keydown capture
Browse files Browse the repository at this point in the history
  • Loading branch information
dan-lee committed Nov 12, 2024
1 parent 7388a26 commit 8e07ead
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
15 changes: 15 additions & 0 deletions .changeset/grumpy-mangos-march.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
'timescape': patch
---

Added the ability to prevent the default keydown event handling. If you want to handle keydown events yourself, you can now prevent the default behavior by using `onKeyDownCapture` (or the equivalent in your framework) and calling `preventDefault()` in your handler.

```tsx
<input
onKeyDownCapture={(e) => {
if (e.key === 'Enter') {
e.preventDefault()
}
}}
/>
```
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,20 @@ The `placeholder` attribute on the input elements is supported and will be used

The [`step` attribute for input elements](https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/step) is supported and will be used to increment/decrement the values when the user uses the arrow keys. The default value is `1`, but you can set it to any value you want. Also see [`snapToStep`](#options) if you want to snap to the nearest step.

### Preventing default `keydown` behavior

By default, timescape intercepts keydown events to enhance input behavior. If you want to handle keydown events yourself and prevent the default processing, you can do so by attaching your event handler during the capturing phase and calling `preventDefault`:

```tsx
<input
onKeyDownCapture={(e) => {
if (e.key === 'Enter') {
e.preventDefault()
}
}}
/>
```

## Ranges

`timescape` supports ranges for the date/time inputs. This means a user can select a start and end. This is useful for things like booking systems, where you want to allow the user to select a range of dates.
Expand Down
2 changes: 2 additions & 0 deletions packages/lib/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,8 @@ export class TimescapeManager implements Options {
}

#handleKeyDown(e: KeyboardEvent) {
if (e.defaultPrevented) return

const registryEntry = [...this.#registry.values()].find(
({ inputElement }) => inputElement === e.target,
)
Expand Down

0 comments on commit 8e07ead

Please sign in to comment.