Skip to content

Commit

Permalink
fix: avoid binding the value to file inputs
Browse files Browse the repository at this point in the history
  • Loading branch information
logaretm committed Oct 2, 2020
1 parent 18fb60e commit 02a2745
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
23 changes: 17 additions & 6 deletions packages/core/src/Field.ts
@@ -1,7 +1,7 @@
import { h, defineComponent, nextTick, toRef } from 'vue';
import { h, defineComponent, nextTick, toRef, SetupContext } from 'vue';
import { getConfig } from './config';
import { useField } from './useField';
import { normalizeChildren, isHTMLTag, hasCheckedAttr } from './utils';
import { normalizeChildren, isHTMLTag, hasCheckedAttr, isFileInput } from './utils';

export const Field = defineComponent({
name: 'Field',
Expand Down Expand Up @@ -106,6 +106,10 @@ export const Field = defineComponent({
fieldProps.value = value.value;
}

if (isFileInput(resolveTag(props, ctx), ctx.attrs.type as string)) {
delete fieldProps.value;
}

return {
field: fieldProps,
aria: aria.value,
Expand All @@ -119,11 +123,8 @@ export const Field = defineComponent({
};

return () => {
let tag: string | undefined = props.as;
const tag = resolveTag(props, ctx);
const slotProps = makeSlotProps();
if (!props.as && !ctx.slots.default) {
tag = 'input';
}

// Sync the model value with the inner field value if they mismatch
// a simple string comparison is used here
Expand Down Expand Up @@ -151,3 +152,13 @@ export const Field = defineComponent({
};
},
});

function resolveTag(props: Record<string, any>, ctx: SetupContext) {
let tag: string = props.as || '';

if (!props.as && !ctx.slots.default) {
tag = 'input';
}

return tag;
}
7 changes: 7 additions & 0 deletions packages/core/src/utils/assertions.ts
Expand Up @@ -12,6 +12,13 @@ export function isHTMLTag(tag: string) {
return ['input', 'textarea', 'select'].includes(tag);
}

/**
* Checks if an input is of type file
*/
export function isFileInput(tag: string, type: string) {
return isHTMLTag(tag) && type === 'file';
}

type YupValidator = { validate: (value: any) => Promise<void | boolean> };

export function isYupValidator(value: unknown): value is YupValidator {
Expand Down

0 comments on commit 02a2745

Please sign in to comment.