Skip to content

Commit

Permalink
feat(input-date): support onKeyDown and onKeyUp
Browse files Browse the repository at this point in the history
  • Loading branch information
clementprevot committed Aug 17, 2023
1 parent 5775fcf commit ff4c30f
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
15 changes: 15 additions & 0 deletions packages/fractal/src/components/InputDate/InputDate.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ export const InputDate = forwardRef<CombinedRefs, InputDateProps>(
name,
onChange,
onFieldChange,
onKeyDown,
onKeyUp,
placeholders,
readOnly = false,
required = false,
Expand Down Expand Up @@ -294,6 +296,10 @@ export const InputDate = forwardRef<CombinedRefs, InputDateProps>(
default:
break
}

if (isFunction(onKeyDown)) {
onKeyDown(event, type)
}
}

return (
Expand Down Expand Up @@ -337,6 +343,9 @@ export const InputDate = forwardRef<CombinedRefs, InputDateProps>(
onChange={(event, newDay) => handleChange(event, newDay, 'day')}
onFocus={handleFocus}
onKeyDown={(event) => handleKeyDown(event, 'day')}
{...(isFunction(onKeyUp)
? { onKeyUp: (event) => onKeyUp(event, 'day') }
: {})}
/>

<InputText
Expand Down Expand Up @@ -367,6 +376,9 @@ export const InputDate = forwardRef<CombinedRefs, InputDateProps>(
}
onFocus={handleFocus}
onKeyDown={(event) => handleKeyDown(event, 'month')}
{...(isFunction(onKeyUp)
? { onKeyUp: (event) => onKeyUp(event, 'month') }
: {})}
/>

<InputText
Expand Down Expand Up @@ -402,6 +414,9 @@ export const InputDate = forwardRef<CombinedRefs, InputDateProps>(
onChange={(event, newYear) => handleChange(event, newYear, 'year')}
onFocus={handleFocus}
onKeyDown={(event) => handleKeyDown(event, 'year')}
{...(isFunction(onKeyUp)
? { onKeyUp: (event) => onKeyUp(event, 'year') }
: {})}
/>
</div>

Expand Down
15 changes: 13 additions & 2 deletions packages/fractal/src/components/InputDate/InputDate.types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ChangeEvent, HTMLAttributes } from 'react'
import type { ChangeEvent, HTMLAttributes, KeyboardEvent } from 'react'

export type DateFormat = {
day?: number | undefined
Expand Down Expand Up @@ -27,7 +27,7 @@ export type CombinedRefs = {
export interface InputDateProps
extends Omit<
HTMLAttributes<HTMLDivElement>,
'defaultValue' | 'onChange' | 'placeholder'
'defaultValue' | 'onChange' | 'onKeyDown' | 'onKeyUp' | 'placeholder'
> {
/** Indicates if the day field must be focused on render. */
autoFocus?: boolean
Expand Down Expand Up @@ -80,6 +80,17 @@ export interface InputDateProps
type: keyof DateFormat,
newDay: number,
) => void
/** Event handler when a key is pressed down in one of the fields. */
onKeyDown: (
event: KeyboardEvent<HTMLInputElement>,
type: keyof DateFormat,
) => void

/** Event handler when a key is released in one of the fields. */
onKeyUp: (
event: KeyboardEvent<HTMLInputElement>,
type: keyof DateFormat,
) => void
/**
* A string to display in each of the date field when the value is
* empty.
Expand Down

0 comments on commit ff4c30f

Please sign in to comment.