-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.tsx
34 lines (28 loc) · 1.27 KB
/
index.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import * as React from 'react';
import { KeyboardEvent } from 'react';
import { propagateRef } from './ref';
import { TSXRef } from './types';
export * from './context';
export * from './hooks';
export * from './ref';
export * from './types';
export interface OnChange {
onChange(value: string): void;
}
const ezyChange = <T extends OnChange>({ onChange, ...props }: T) =>
({ onChange: (e: { target: { value: string } }) => onChange(e.target.value), ...props });
export type InputProps = TSXRef<'input', {
onEnter?: (value: string, element: HTMLInputElement, event: KeyboardEvent<HTMLInputElement>) => void;
} & OnChange>;
export const Input = propagateRef(({ onEnter, ...props }: InputProps) => <input onKeyDown={onEnter && (e => {
const { key, currentTarget } = e;
key === 'Enter' && onEnter(currentTarget.value, currentTarget, e);
})} {...ezyChange(props)} />);
export type TextareaProps = TSXRef<'textarea', OnChange>;
export const Textarea = propagateRef((props: TextareaProps) => <textarea {...ezyChange(props)} />);
export type SvgProps = TSXRef<'svg', {
width: number | string;
height: number | string;
}>;
export const Svg = propagateRef(({ width, height, ...props }: SvgProps) =>
<svg xmlns="http://www.w3.org/2000/svg" viewBox={`0 0 ${width} ${height}`} {...props} />);