Skip to content

Commit ca85ad9

Browse files
committed
Typed input type prop
1 parent 6ccdf3a commit ca85ad9

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

Todo.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,5 @@
22
- Typed `<option>` elements
33
- Split package in `typed-react-form-yup-validator`, `typed-react-form-elements`? Reduces size.
44
- Nested validators in child forms to improve validation performance
5-
- Typed FormInput type prop
65
- Combine array helpers into one object? This is usefull to pass to other components
76
- Require index for array fields

src/elements/FormInput.tsx

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { InputHTMLAttributes } from "react";
33
import { DefaultState, FormState } from "../form";
44
import { useListener } from "../hooks";
55

6-
type BaldInputProps = Omit<InputHTMLAttributes<HTMLInputElement>, "name" | "form" | "value">;
6+
type BaldInputProps = Omit<InputHTMLAttributes<HTMLInputElement>, "name" | "form" | "value" | "type">;
77

88
export const DEFAULT_DIRTY_CLASS = "typed-form-dirty";
99
export const DEFAULT_ERROR_CLASS = "typed-form-error";
@@ -14,9 +14,28 @@ export function getClassName(...args: any) {
1414

1515
export type FormInputCheckMode = "normal" | "setNull" | "setUndefined";
1616

17+
export type FormInputType =
18+
| "number"
19+
| "text"
20+
| "password"
21+
| "date"
22+
| "datetime-local"
23+
| "radio"
24+
| "checkbox"
25+
| "color"
26+
| "email"
27+
| "text"
28+
| "month"
29+
| "url"
30+
| "week"
31+
| "time"
32+
| "tel"
33+
| "range";
34+
1735
export type FormInputProps<T, State, Error, Key extends keyof T, Value extends T[Key] | T[Key][keyof T[Key]]> = BaldInputProps & {
1836
form: FormState<T, State, Error>;
1937
name: Key;
38+
type?: FormInputType;
2039
value?: Value;
2140
errorClassName?: string;
2241
errorStyle?: React.CSSProperties;
@@ -29,6 +48,11 @@ export type FormInputProps<T, State, Error, Key extends keyof T, Value extends T
2948
hideWhenNull?: boolean;
3049
};
3150

51+
/**
52+
* The builtin form input. You must always specify **form** and **name**. Use the **type** prop to specify what type of field it represents.
53+
*
54+
* When this component does not satisfy your needs, you can always [create your own](https://github.com/CodeStix/typed-react-form/wiki/Custom-inputs#example-custom-input).
55+
*/
3256
export function FormInput<T, State extends DefaultState, Error, Key extends keyof T, Value extends T[Key] | T[Key][keyof T[Key]]>({
3357
form,
3458
name,

0 commit comments

Comments
 (0)