-
-
Notifications
You must be signed in to change notification settings - Fork 102
/
Copy pathTextField.tsx
45 lines (41 loc) · 1.23 KB
/
TextField.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
import clsx from 'clsx';
import {
DynamicProps,
WithRef,
} from 'solid-headless/dist/types/utils/dynamic-prop';
import {PropsWithChildren} from 'solid-js';
import {omitProps} from 'solid-use/props';
import {styled} from '../../utils';
import {useText, UseTextProps} from '../Text';
import {textField, TextFieldProps as $TextFieldProps} from './TextField.css';
export type TextFieldProps = {
type: 'text' | 'number';
value?: string | number;
onChange?: (value: string) => void;
size?: UseTextProps['size'];
} & $TextFieldProps &
WithRef<'input'> &
Omit<DynamicProps<'input'>, 'as' | 'ref' | 'onInput' | 'onChange' | 'type'>;
export function TextField(props: PropsWithChildren<TextFieldProps>) {
function onChange(e: Event): void {
if (props.onChange) {
const target = e.target as HTMLInputElement;
props.onChange(String(target.value));
}
}
const textClasses = useText(props);
return (
<styled.input
value={props.value}
type={props.type}
class={clsx(
textClasses(),
textField({inline: props.inline ?? false}),
props.class,
)}
onInput={onChange}
onChange={onChange}
{...omitProps(props, ['class', 'type', 'value', 'onChange'])}
/>
);
}