4 changed files with 139 additions and 1 deletions
@ -0,0 +1,134 @@ |
|||
<template> |
|||
<div v-show="showIfComputed"> |
|||
<q-file |
|||
ref="fileRef" |
|||
v-model="fileValue" |
|||
:hide-bottom-space="true" |
|||
:hide-hint="true" |
|||
:outlined="true" |
|||
:dense="true" |
|||
:clearable="true" |
|||
v-bind="attrs" |
|||
:rules="rulesComputed" |
|||
:readonly="readonlyIfComputed" |
|||
:disable="disableIfComputed" |
|||
@update:model-value="updateModelValue" |
|||
@change="changeValue" |
|||
> |
|||
<template #label> <span v-if="requiredIfComputed" style="color: red">*</span> {{ attrs.label }}</template> |
|||
<template #append> |
|||
<q-icon name="attachment" /> |
|||
</template> |
|||
</q-file> |
|||
</div> |
|||
</template> |
|||
|
|||
<script setup lang="ts"> |
|||
import { ref, computed, defineProps, useAttrs, watch } from 'vue'; |
|||
import { FormValidators } from '@/platform/components'; |
|||
|
|||
const fileRef = ref(); |
|||
const attrs = useAttrs(); |
|||
const rules = attrs.rules; |
|||
const props = defineProps({ |
|||
onChange: { |
|||
type: Function, |
|||
default: () => {}, |
|||
}, |
|||
modelValue: { type: [File, Array], default: undefined }, |
|||
showIf: { |
|||
type: Function, |
|||
default: () => { |
|||
return true; |
|||
}, |
|||
}, |
|||
required: { |
|||
type: Boolean, |
|||
default: false, |
|||
}, |
|||
requiredIf: { |
|||
type: Function, |
|||
default: undefined, |
|||
}, |
|||
readOnlyIf: { |
|||
type: Function, |
|||
default: () => { |
|||
return false; |
|||
}, |
|||
}, |
|||
disableIf: { |
|||
type: Function, |
|||
default: () => { |
|||
return false; |
|||
}, |
|||
}, |
|||
form: { |
|||
type: Object, |
|||
default: undefined, |
|||
}, |
|||
}); |
|||
const emit = defineEmits(['update:modelValue', 'change']); |
|||
const fileValue = ref(props.modelValue); |
|||
watch( |
|||
() => props.modelValue, |
|||
(newVal, oldVal) => { |
|||
fileValue.value = newVal; |
|||
}, |
|||
); |
|||
|
|||
const rulesComputed = computed(() => { |
|||
let result = rules || <any>[]; |
|||
if (showIfComputed.value && requiredIfComputed.value) { |
|||
result.push(FormValidators.required()); |
|||
} else if (!showIfComputed.value) { |
|||
result = []; |
|||
} |
|||
if (fileRef?.value) { |
|||
fileRef.value.resetValidation(); |
|||
} |
|||
return result; |
|||
}); |
|||
|
|||
const showIfComputed = computed(() => { |
|||
return props.showIf({ |
|||
value: fileValue.value, |
|||
form: props.form, |
|||
}); |
|||
}); |
|||
const requiredIfComputed = computed(() => { |
|||
if (props.requiredIf) { |
|||
return ( |
|||
props.requiredIf({ |
|||
value: fileValue.value, |
|||
form: props.form, |
|||
}) || false |
|||
); |
|||
} else if (props.required) { |
|||
return true; |
|||
} |
|||
return false; |
|||
}); |
|||
const readonlyIfComputed = computed(() => { |
|||
return props.readOnlyIf({ |
|||
value: fileValue.value, |
|||
form: props.form, |
|||
}); |
|||
}); |
|||
const disableIfComputed = computed(() => { |
|||
return props.disableIf({ |
|||
value: fileValue.value, |
|||
form: props.form, |
|||
}); |
|||
}); |
|||
|
|||
const updateModelValue = (value) => { |
|||
emit('update:modelValue', value, props.form); |
|||
}; |
|||
|
|||
const changeValue = (value) => { |
|||
emit('change', { |
|||
value: value, |
|||
form: props.form, |
|||
}); |
|||
}; |
|||
</script> |
Loading…
Reference in new issue