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

Fix inverted validate label check in NcInputField #3980

Merged
merged 4 commits into from
Apr 18, 2023
Merged
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
23 changes: 7 additions & 16 deletions src/components/NcInputField/NcInputField.vue
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ For a list of all available props and attributes, please check the [HTMLInputEle

<template>
<div class="input-field">
<label v-if="!labelOutside && label !== undefined"
<label v-if="!labelOutside && isValidLabel"
class="input-field__label"
:class="{ 'input-field__label--hidden': !labelVisible }"
:for="computedId">
Expand Down Expand Up @@ -283,15 +283,12 @@ export default {
return this.hasPlaceholder ? this.placeholder : this.label
}
},
},

watch: {
label() {
this.validateLabel()
},

labelOutside() {
this.validateLabel()
isValidLabel() {
const isValidLabel = this.label || this.labelOutside
if (!isValidLabel) {
console.warn('You need to add a label to the NcInputField component. Either use the prop label or use an external one, as per the example in the documentation.')
}
Comment on lines +288 to +290
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Warn in computed = log every time the computed changes. It might not be the desired outcome
I'm not sure this is a good idea 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't the same happen if we warn in the watcher? The computed changes, the watcher fires, checks the label and warns in case it is not valid!?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hum, good point 🙈
I'm weirded out by this anti pattern though 😁

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are we sure the computed is not executed on other occasions? if so, you can merge !

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The computed is triggered every time label or labelOutside is changed, but of cause the warning will only be fired if one of both is falsy.

Copy link
Contributor Author

@raimund-schluessler raimund-schluessler Apr 18, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@skjnldsv I don't know whether it's an anti-pattern, but we do the same here for example: https://github.com/nextcloud/nextcloud-vue/blob/master/src/mixins/actionText.js#L87-L93 🙈

The computed is triggered every time label or labelOutside is changed, but of cause the warning will only be fired if one of both is falsy.

I would think so too. If label changes between False, undefined, null, an empty string or anything else evaluating to False, the warning would be triggered again even though it was invalid before already. But that seems a bit theoretic to me.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's hear what @ShGKme thinks about this approach 🙂

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@skjnldsv I don't know whether it's an anti-pattern, but we do the samer here for example: master/src/mixins/actionText.js#L87-L93 see_no_evil

looks at blame
sees @skjnldsv
closes tab

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's hear what @ShGKme thinks about this approach 🙂

It's too late, but yes 😅.

It proper works, but it is an anti-pattern, computed should never have side-effects, only computing the value.

In options API there is a place for computed + watch, in Composition - watchEffect.

return isValidLabel
},
},

Expand All @@ -303,12 +300,6 @@ export default {
handleTrailingButtonClick(event) {
this.$emit('trailing-button-click', event)
},

validateLabel() {
if (this.label && !this.labelOutside) {
throw new Error('You need to add a label to the textField component. Either use the prop label or use an external one, as per the example in the documentation')
}
},
},
}
</script>
Expand Down