-
Notifications
You must be signed in to change notification settings - Fork 21
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
fix(kfileupload, kselect, kmultiselect): kinput id [KHCP-12336] #2241
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
d5cac9a
fix(kfileupload, kselect, kmultiselect): kinput id [KHCP-12336]
portikM 1bfe88c
fix: kinput id tweaks [KHCP-12336]
portikM be64d57
fix: address pr feedback [KHCP-12336]
portikM 918b750
fix(kinput): minor style fix [KHCP-12336]
portikM fa2b0ee
fix(kselect, kfileupload): minor fixes [KHCP-12336]
portikM d53f6d0
fix(kfileupload, kselect): watch id [KHCP-12336]
portikM 273973f
fix(kfileupload): minor fix [KHCP-12336]
portikM 98da899
fix(kselect): minor fix [KHCP-12336]
portikM f526f13
fix(kfileupload): address pr feedback [KHCP-12336]
portikM 599b52c
fix: move inheritattrs into script setup [KHCP-12336]
portikM 222f04d
chore(sandbox): revert changes [KHCP-12336]
portikM File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,13 @@ | ||
<template> | ||
<div class="k-file-upload"> | ||
<div | ||
class="k-file-upload" | ||
v-bind="modifiedAttrs" | ||
> | ||
<KLabel | ||
v-if="label" | ||
v-bind-once="{ for: inputId }" | ||
v-bind="labelAttributes" | ||
ref="labelElement" | ||
:for="$attrs.id ? String($attrs.id) : undefined" | ||
:required="isRequired" | ||
> | ||
{{ strippedLabel }} | ||
|
@@ -26,9 +30,9 @@ | |
</span> | ||
|
||
<KInput | ||
v-bind="attrs.id ? { id: String(attrs.id) } : {}" | ||
:key="fileInputKey" | ||
ref="fileInputElement" | ||
v-bind-once="{ id: inputId }" | ||
:accept="accept" | ||
class="upload-input" | ||
:disabled="disabled" | ||
|
@@ -65,13 +69,16 @@ | |
</template> | ||
|
||
<script lang="ts" setup> | ||
defineOptions({ | ||
inheritAttrs: false, | ||
}) | ||
|
||
Leopoldthecoder marked this conversation as resolved.
Show resolved
Hide resolved
|
||
import type { PropType } from 'vue' | ||
import { computed, ref, useAttrs, useSlots } from 'vue' | ||
import { computed, ref, useAttrs, useSlots, onMounted, watch, nextTick } from 'vue' | ||
import KLabel from '@/components/KLabel/KLabel.vue' | ||
import KInput from '@/components/KInput/KInput.vue' | ||
import KButton from '@/components/KButton/KButton.vue' | ||
import useUtilities from '@/composables/useUtilities' | ||
import useUniqueId from '@/composables/useUniqueId' | ||
|
||
const props = defineProps({ | ||
labelAttributes: { | ||
|
@@ -127,8 +134,16 @@ const emit = defineEmits<{ | |
|
||
const { stripRequiredLabel } = useUtilities() | ||
|
||
const inputId = attrs.id ? String(attrs.id) : useUniqueId() | ||
const modifiedAttrs = computed(() => { | ||
const $attrs = { ...attrs } | ||
|
||
delete $attrs.id // delete id because we bind id to the input element | ||
|
||
return $attrs | ||
}) | ||
|
||
const fileInputElement = ref<InstanceType<typeof KInput> | null>(null) | ||
const labelElement = ref<InstanceType<typeof KLabel> | null>(null) | ||
const hasLabelTooltip = computed((): boolean => !!(props.labelAttributes?.info || slots['label-tooltip'])) | ||
const strippedLabel = computed((): string => stripRequiredLabel(props.label, isRequired.value)) | ||
const isRequired = computed((): boolean => attrs?.required !== undefined && String(attrs?.required) !== 'false') | ||
|
@@ -165,7 +180,7 @@ const hasUploadError = ref<boolean>(false) | |
// This holds the FileList | ||
const fileInput = ref<File[]>([]) | ||
// To clear the input value after reset | ||
const fileInputKey = ref(0) | ||
const fileInputKey = ref<number>(0) | ||
// File fakepath | ||
const fileValue = ref<string>('') | ||
// Array to store the previously selected FileList when user clicks reopen the file uploader and clicks on Cancel | ||
|
@@ -235,6 +250,33 @@ const resetInput = (): void => { | |
|
||
emit('file-removed') | ||
} | ||
|
||
const setLabelAttributes = () => { | ||
/** | ||
* Temporary fix for the issue where we can't use v-bind-once to pass id to a custom element (KInput) | ||
* TODO: remove this once useId is released in Vue 3.5 | ||
*/ | ||
if (!attrs.id) { | ||
const inputElementId = fileInputElement.value?.$el?.querySelector('input')?.id | ||
|
||
if (inputElementId) { | ||
labelElement.value?.$el?.setAttribute('for', inputElementId) | ||
} | ||
} | ||
} | ||
|
||
watch(fileInputKey, async () => { | ||
await nextTick() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why does this code need to be in a separate watcher instead of in the one below that triggers the key to increment? |
||
setLabelAttributes() | ||
}) | ||
|
||
watch(() => attrs.id, () => { | ||
fileInputKey.value++ | ||
}, { immediate: true }) | ||
|
||
onMounted(() => { | ||
setLabelAttributes() | ||
}) | ||
</script> | ||
|
||
<style lang="scss" scoped> | ||
|
@@ -260,10 +302,6 @@ $kFileUploadInputPaddingY: var(--kui-space-40, $kui-space-40); // corresponds to | |
color: transparent !important; | ||
} | ||
|
||
:deep(.k-input) { | ||
padding-right: 90px !important; // offset to account for button | ||
} | ||
|
||
.file-upload-input-wrapper { | ||
position: relative; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn’t this go below the imports?