-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathInput.tsx
54 lines (52 loc) · 1.42 KB
/
Input.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import cl from 'clsx/lite';
import type { InputHTMLAttributes } from 'react';
import { forwardRef } from 'react';
import type { Size } from '../../../types';
type InputAttr = InputHTMLAttributes<HTMLInputElement>;
export type InputProps = {
/**
* Changes field size and paddings
*/
size?: Size;
/** Supported `input` types */
type?: InputAttr['type'];
/** Exposes the HTML `size` attribute.
* @default 20
*/
htmlSize?: number;
/** Disables element
* @note Avoid using if possible for accessibility purposes
*/
disabled?: boolean;
/** Toggle `readOnly` */
readOnly?: boolean;
/** Set role, i.e. `switch` when `checkbox` or `radio` */
role?: InputAttr['role'];
} & Omit<InputAttr, 'size' | 'prefix' | 'role' | 'type'>;
/** Input field
*
* @example
* ```tsx
* <Input />
* ```
*/
export const Input = forwardRef<HTMLInputElement, InputProps>(function Input(
{ type = 'text', size, htmlSize, className, onChange, onClick, ...rest },
ref,
) {
return (
<input
className={cl(`ds-input`, className)}
data-size={size}
ref={ref}
size={htmlSize}
type={type}
onChange={(event) => rest.readOnly || onChange?.(event)} // Make readonly work for checkbox / radio / switch
onClick={(event) => {
if (rest.readOnly) event.preventDefault(); // Make readonly work for checkbox / radio / switch
onClick?.(event);
}}
{...rest}
/>
);
});