Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow keydown capture #44

Merged
merged 1 commit into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading