Skip to content

Commit e3db37a

Browse files
committed
feat: add input util
1 parent a7042f9 commit e3db37a

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

primitives/utils/src/input.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import type { FieldElement } from 'skyroc-type-utils';
2+
3+
import { isEventObject } from './object';
4+
5+
type Event = { target: any };
6+
7+
export const isCheckBoxInput = (element: FieldElement): element is HTMLInputElement => element.type === 'checkbox';
8+
9+
export const isRadioInput = (element: FieldElement): element is HTMLInputElement => element.type === 'radio';
10+
11+
export const isFileInput = (element: FieldElement): element is HTMLInputElement => element.type === 'file';
12+
13+
export const getEventValue = (event: unknown, valuePropName: string = 'value') => {
14+
if (!isEventObject(event)) return event;
15+
16+
const { target } = event as Event;
17+
18+
if (!target) return event;
19+
20+
if (isCheckBoxInput(target)) return target.checked;
21+
22+
return target[valuePropName];
23+
};

0 commit comments

Comments
 (0)