Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update dom.ts #193

Merged
merged 2 commits into from
Jun 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 10 additions & 24 deletions packages/conform-dom/dom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,8 @@ export function getFormEncType(
const submitter = event.submitter as Submitter | null;
const encType = submitter?.getAttribute('formenctype') ?? form.enctype;

if (
['application/x-www-form-urlencoded', 'multipart/form-data'].includes(
encType,
)
) {
return encType as any;
if (encType === 'multipart/form-data') {
return encType;
}

return 'application/x-www-form-urlencoded';
Expand All @@ -77,8 +73,12 @@ export function getFormMethod(
const method =
submitter?.getAttribute('formmethod') ?? form.getAttribute('method');

if (['get', 'post', 'put', 'patch', 'delete'].includes(method as string)) {
return method as any;
switch (method) {
case 'post':
case 'put':
case 'patch':
case 'delete':
return method;
}

return 'get';
Expand All @@ -97,28 +97,14 @@ export function getFormElement(
| HTMLButtonElement
| null,
): HTMLFormElement | null {
const form = element instanceof HTMLFormElement ? element : element?.form;

if (!form) {
return null;
}

return form;
return element instanceof HTMLFormElement ? element : element?.form ?? null;
}

/**
* Returns a list of form control elements in the form
*/
export function getFormControls(form: HTMLFormElement): FormControl[] {
const formControls: FormControl[] = [];

for (const element of form.elements) {
if (isFormControl(element)) {
formControls.push(element);
}
}

return formControls;
return Array.from(form.elements).filter(isFormControl);
}

/**
Expand Down
10 changes: 5 additions & 5 deletions packages/conform-react/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -880,7 +880,7 @@ export function useInputEvent(options: {
});

useSafeLayoutEffect(() => {
const createEventListner = (
const createEventListener = (
listener: Exclude<keyof typeof options, 'ref'>,
) => {
return (event: any) => {
Expand All @@ -903,10 +903,10 @@ export function useInputEvent(options: {
}
};
};
const inputHandler = createEventListner('onInput');
const focusHandler = createEventListner('onFocus');
const blurHandler = createEventListner('onBlur');
const resetHandler = createEventListner('onReset');
const inputHandler = createEventListener('onInput');
const focusHandler = createEventListener('onFocus');
const blurHandler = createEventListener('onBlur');
const resetHandler = createEventListener('onReset');

// focus/blur event does not bubble
document.addEventListener('input', inputHandler, true);
Expand Down
14 changes: 7 additions & 7 deletions packages/conform-zod/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ export function getFieldsetConstraint<Source extends z.ZodTypeAny>(
'pattern',
];

function resolveFieldsetConstarint<T extends Record<string, any>>(
function resolveFieldsetConstraint<T extends Record<string, any>>(
schema: z.ZodType<T>,
): FieldsetConstraint<z.input<Source>> {
if (schema instanceof z.ZodObject) {
Expand All @@ -111,21 +111,21 @@ export function getFieldsetConstraint<Source extends z.ZodTypeAny>(
}

if (schema instanceof z.ZodEffects) {
return resolveFieldsetConstarint(schema.innerType());
return resolveFieldsetConstraint(schema.innerType());
} else if (schema instanceof z.ZodOptional) {
return resolveFieldsetConstarint(schema.unwrap());
return resolveFieldsetConstraint(schema.unwrap());
} else if (schema instanceof z.ZodIntersection) {
return {
...resolveFieldsetConstarint(schema._def.left),
...resolveFieldsetConstarint(schema._def.right),
...resolveFieldsetConstraint(schema._def.left),
...resolveFieldsetConstraint(schema._def.right),
};
} else if (
schema instanceof z.ZodUnion ||
schema instanceof z.ZodDiscriminatedUnion
) {
const options = schema.options as Array<z.ZodType<any>>;

return options.map(resolveFieldsetConstarint).reduce((prev, next) => {
return options.map(resolveFieldsetConstraint).reduce((prev, next) => {
const list = new Set([...Object.keys(prev), ...Object.keys(next)]);
const result: Record<string, FieldConstraint> = {};

Expand Down Expand Up @@ -163,7 +163,7 @@ export function getFieldsetConstraint<Source extends z.ZodTypeAny>(
return {};
}

return resolveFieldsetConstarint(source);
return resolveFieldsetConstraint(source);
}

export function parse<Schema extends z.ZodTypeAny>(
Expand Down