-
Notifications
You must be signed in to change notification settings - Fork 85
/
input-event.tsx
119 lines (113 loc) · 3.27 KB
/
input-event.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import {
getFormProps,
useForm,
unstable_useControl as useControl,
} from '@conform-to/react';
import { type LoaderFunctionArgs } from '@remix-run/node';
import { useLoaderData } from '@remix-run/react';
import { type FormEvent, useRef, useState } from 'react';
export async function loader({ request }: LoaderFunctionArgs) {
const url = new URL(request.url);
return {
delegateFocus: url.searchParams.get('delegateFocus') === 'yes',
defaultValue: url.searchParams.get('defaultValue'),
};
}
export default function Example() {
const { defaultValue, delegateFocus } = useLoaderData<typeof loader>();
const [form, fields] = useForm({
defaultValue: {
'native-input': defaultValue,
'base-input': defaultValue,
},
});
const control = useControl(fields['base-input']);
const inputRef = useRef<HTMLInputElement>(null);
const [logsByName, setLogsByName] = useState<Record<string, string[]>>({});
const log = (event: FormEvent<HTMLFormElement>) => {
const input = event.target as HTMLInputElement;
setLogsByName((logsByName) => ({
...logsByName,
[input.name]: [
...(logsByName[input.name] ?? []),
JSON.stringify({
eventPhase: event.eventPhase,
type: event.type,
bubbles: event.bubbles,
cancelable: event.cancelable,
}),
],
}));
};
return (
<form
{...getFormProps(form)}
onChange={log}
onInput={log}
onFocusCapture={log}
onFocus={log}
onBlurCapture={log}
onBlur={log}
>
<div className="sticky top-0 pt-4 pb-8 bg-gray-100 border-b">
<label>Type here</label>
<div className="flex flex-row gap-4">
<input
className="p-2 flex-1"
name="native-input"
type="text"
defaultValue={fields['native-input'].initialValue ?? ''}
ref={inputRef}
onChange={(event) => control.change(event.target.value)}
onFocus={control.focus}
onBlur={control.blur}
/>
<button className="inline-flex justify-center py-2 px-4 border border-transparent shadow-sm text-sm font-medium rounded-md text-white bg-indigo-600 hover:bg-indigo-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500">
Submit
</button>
<button
type="reset"
className="inline-flex justify-center py-2 px-4 border border-transparent text-sm font-medium rounded-md text-gray-700 bg-gray-50 hover:bg-gray-200 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500"
>
Reset
</button>
</div>
<div className="pt-4">
<div>Shadow input</div>
<input
ref={control.register}
name="base-input"
type="text"
value={control.value}
onChange={(event) => control.change(event.target.value)}
onFocus={() => {
control.focus();
if (delegateFocus) {
inputRef.current?.focus();
}
}}
onBlur={control.blur}
/>
</div>
</div>
<div className="my-4 flex flex-row">
<div className="flex-1">
native-input logs
<ul id="native-input">
{logsByName['native-input']?.map((log, i) => (
<li key={i}>{log}</li>
))}
</ul>
</div>
<div className="flex-1">
base-input logs
<ul id="base-input">
{logsByName['base-input']?.map((log, i) => (
<li key={i}>{log}</li>
))}
</ul>
</div>
</div>
</form>
);
}