<commit_before>import { h, defineComponent, nextTick, toRef, SetupContext } from 'vue';
import { getConfig } from './config';
import { useField } from './useField';
import { normalizeChildren, isHTMLTag, hasCheckedAttr, isFileInput } from './utils';

export const Field = defineComponent({
  name: 'Field',
  inheritAttrs: false,
  props: {
    as: {
      type: [String, Object],
      default: undefined,
    },
    name: {
      type: String,
      required: true,
    },
    rules: {
      type: [Object, String, Function],
      default: null,
    },
    validateOnMount: {
      type: Boolean,
      default: false,
    },
    bails: {
      type: Boolean,
      default: () => getConfig().bails,
    },
    disabled: {
      type: Boolean,
      default: false,
    },
    label: {
      type: String,
      default: undefined,
    },
  },
  setup(props, ctx) {
    const [disabled, rules] = [toRef(props, 'disabled'), toRef(props, 'rules')];

    const {
      errors,
      value,
      errorMessage,
      validate: validateField,
      handleChange,
      handleBlur,
      handleInput,
      reset,
      meta,
      aria,
      checked,
    } = useField(props.name, rules, {
      validateOnMount: props.validateOnMount,
      bails: props.bails,
      disabled,
      type: ctx.attrs.type as string,
      // Gets the initial value either from `value` prop/attr or `v-model` binding (modelValue)
      // For checkboxes and radio buttons it will always be the model value not the `value` attribute
      initialValue: hasCheckedAttr(ctx.attrs.type)
        ? ctx.attrs.modelValue
        : 'modelValue' in ctx.attrs
        ? ctx.attrs.modelValue
        : ctx.attrs.value,
      // Only for checkboxes and radio buttons
      valueProp: ctx.attrs.value,
      label: props.label || props.name,
      validateOnValueUpdate: false,
    });

    let isDuringValueTick = false;
    // Prevents re-render updates that rests value when using v-model (#2941)
    function valueTick() {
      isDuringValueTick = true;
      nextTick(() => {
        isDuringValueTick = false;
      });
    }

    // If there is a v-model applied on the component we need to emit the `update:modelValue` whenever the value binding changes
    const onChangeHandler =
      'modelValue' in ctx.attrs
        ? function handleChangeWithModel(e: any) {
            handleChange(e);
            ctx.emit('update:modelValue', value.value);
          }
        : handleChange;

    const { validateOnInput, validateOnChange, validateOnBlur, validateOnModelUpdate } = getConfig();
    const makeSlotProps = () => {
      const fieldProps: Record<string, any> = {
        name: props.name,
        disabled: props.disabled,
        onBlur: [handleBlur],
        onInput: [handleInput, valueTick],
        onChange: [handleInput, valueTick],
      };

      if (validateOnInput) {
        fieldProps.onInput.push(onChangeHandler);
      }

      if (validateOnChange) {
        fieldProps.onChange.push(onChangeHandler);
      }

      if (validateOnBlur) {
        fieldProps.onBlur.push(validateField);
      }

      if (validateOnModelUpdate) {
        fieldProps['onUpdate:modelValue'] = [onChangeHandler, valueTick];
      }

      if (hasCheckedAttr(ctx.attrs.type) && checked) {
        fieldProps.checked = checked.value;
      } else {
        fieldProps.value = value.value;
      }

      if (isFileInput(resolveTag(props, ctx), ctx.attrs.type as string)) {
        delete fieldProps.value;
      }<commit_msg>fix: sync model value on input closes #2944<commit_after>import { h, defineComponent, nextTick, toRef, SetupContext } from 'vue';
import { getConfig } from './config';
import { useField } from './useField';
import { normalizeChildren, isHTMLTag, hasCheckedAttr, isFileInput } from './utils';

export const Field = defineComponent({
  name: 'Field',
  inheritAttrs: false,
  props: {
    as: {
      type: [String, Object],
      default: undefined,
    },
    name: {
      type: String,
      required: true,
    },
    rules: {
      type: [Object, String, Function],
      default: null,
    },
    validateOnMount: {
      type: Boolean,
      default: false,
    },
    bails: {
      type: Boolean,
      default: () => getConfig().bails,
    },
    disabled: {
      type: Boolean,
      default: false,
    },
    label: {
      type: String,
      default: undefined,
    },
  },
  setup(props, ctx) {
    const [disabled, rules] = [toRef(props, 'disabled'), toRef(props, 'rules')];

    const {
      errors,
      value,
      errorMessage,
      validate: validateField,
      handleChange,
      handleBlur,
      handleInput,
      reset,
      meta,
      aria,
      checked,
    } = useField(props.name, rules, {
      validateOnMount: props.validateOnMount,
      bails: props.bails,
      disabled,
      type: ctx.attrs.type as string,
      // Gets the initial value either from `value` prop/attr or `v-model` binding (modelValue)
      // For checkboxes and radio buttons it will always be the model value not the `value` attribute
      initialValue: hasCheckedAttr(ctx.attrs.type)
        ? ctx.attrs.modelValue
        : 'modelValue' in ctx.attrs
        ? ctx.attrs.modelValue
        : ctx.attrs.value,
      // Only for checkboxes and radio buttons
      valueProp: ctx.attrs.value,
      label: props.label || props.name,
      validateOnValueUpdate: false,
    });

    let isDuringValueTick = false;
    // Prevents re-render updates that rests value when using v-model (#2941)
    function valueTick() {
      isDuringValueTick = true;
      nextTick(() => {
        isDuringValueTick = false;
      });
    }

    // If there is a v-model applied on the component we need to emit the `update:modelValue` whenever the value binding changes
    const onChangeHandler =
      'modelValue' in ctx.attrs
        ? function handleChangeWithModel(e: any) {
            handleChange(e);
            ctx.emit('update:modelValue', value.value);
          }
        : handleChange;

    const onInputHandler =
      'modelValue' in ctx.attrs
        ? function handleChangeWithModel(e: any) {
            handleInput(e);
            ctx.emit('update:modelValue', value.value);
          }
        : handleInput;

    const { validateOnInput, validateOnChange, validateOnBlur, validateOnModelUpdate } = getConfig();
    const makeSlotProps = () => {
      const fieldProps: Record<string, any> = {
        name: props.name,
        disabled: props.disabled,
        onBlur: [handleBlur],
        onInput: [onInputHandler, valueTick],
        onChange: [onInputHandler, valueTick],
      };

      if (validateOnInput) {
        fieldProps.onInput.push(onChangeHandler);
      }

      if (validateOnChange) {
        fieldProps.onChange.push(onChangeHandler);
      }

      if (validateOnBlur) {
        fieldProps.onBlur.push(validateField);
      }

      if (validateOnModelUpdate) {
        fieldProps['onUpdate:modelValue'] = [onChangeHandler, valueTick];
      }

      if (hasCheckedAttr(ctx.attrs.type) && checked) {
        fieldProps.checked = checked.value;
      } else {
        fieldProps.value = value.value;
      }

      if (isFileInput(resolveTag(props, ctx), ctx.attrs.type as string)) {
        delete fieldProps.value;
      }
{'fraction_non_alphanumeric': 0.11245532031894419, 'fraction_numerical': 0.0016497113005224085, 'mean_word_length': 2.3448275862068964, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7289590', 'n_tokens_mistral': 2133, 'n_tokens_neox': 2093, 'n_words': 535}