mirror of
https://github.com/misode/misode.github.io.git
synced 2026-04-25 08:06:51 +00:00
Add sounds explorer tool
This commit is contained in:
31
src/app/components/forms/Input.tsx
Normal file
31
src/app/components/forms/Input.tsx
Normal file
@@ -0,0 +1,31 @@
|
||||
import type { JSXInternal } from 'preact/src/jsx'
|
||||
|
||||
type InputProps = JSXInternal.HTMLAttributes<HTMLInputElement>
|
||||
|
||||
type BaseInputProps<T> = Omit<InputProps, 'onChange' | 'type'> & {
|
||||
onChange?: (value: T) => unknown,
|
||||
onEnter?: (value: T) => unknown,
|
||||
}
|
||||
function BaseInput<T>(name: string, type: string, fn: (value: string) => T) {
|
||||
const component = (props: BaseInputProps<T>) => {
|
||||
const onChange = props.onChange && ((evt: Event) => {
|
||||
const value = (evt.target as HTMLInputElement).value
|
||||
props.onChange?.(fn(value))
|
||||
})
|
||||
const onKeyDown = props.onEnter && ((evt: KeyboardEvent) => {
|
||||
if (evt.key === 'Enter') {
|
||||
const value = (evt.target as HTMLInputElement).value
|
||||
props.onEnter?.(fn(value))
|
||||
}
|
||||
})
|
||||
return <input {...props} {...{ type, onChange, onKeyDown }} />
|
||||
}
|
||||
component.displayName = name
|
||||
return component
|
||||
}
|
||||
|
||||
export const TextInput = BaseInput('TextInput', 'text', v => v)
|
||||
|
||||
export const NumberInput = BaseInput('NumberInput', 'number', v => Number(v))
|
||||
|
||||
export const RangeInput = BaseInput('RangeInput', 'range', v => Number(v))
|
||||
Reference in New Issue
Block a user