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(refs dplan-12315): Make multiselect validation vue3 compatible #1054

Merged
merged 3 commits into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ Since v0.0.10, this Changelog is formatted according to the [Common Changelog][c

## UNRELEASED

### Fixed
- ([#1054](https://github.com/demos-europe/demosplan-ui/pull/1054)) Fix validating dpMultiselect ([@salisdemos](https://github.com/salisdemos))


## v0.4.1 - 2024-10-07

### Added
Expand Down
2 changes: 1 addition & 1 deletion src/components/DpMultiselect/DpMultiselect.vue
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
name,
options,
placeholder,
required,
searchable,
selectGroupLabel,
selectLabel,
Expand All @@ -29,6 +28,7 @@
trackBy,
value
}"
:class="{ 'is-required' : required }"
:data-cy="dataCy"
:data-dp-validate-error-fieldname="dataDpValidateErrorFieldname"
:model-value="value"
Expand Down
18 changes: 11 additions & 7 deletions src/lib/validation/dpValidateMultiselectDirective.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,17 @@ const dpValidateMultiselectDirective = {
// Set initially correct validity value (no dropdown options => isValid)
const component = binding.instance
const hasOptions = component.groupValues ? component.options.some(group => group[component.groupValues].length > 0) : component.options.length > 0
el.setAttribute('data-dp-validate-is-valid', !hasOptions)

component.$el.setAttribute('data-dp-validate-is-valid', !hasOptions)
const validateMultiselectField = (e) => {
e.stopPropagation()
const validate = (e) => {
const validate = () => {
document.removeEventListener('mouseup', validate)
validateMultiselect(el)
}
document.addEventListener('mouseup', validate)
}

el.addEventListener('mouseup', validateMultiselectField)
},

Expand All @@ -28,19 +30,20 @@ const dpValidateMultiselectDirective = {

const component = binding.instance
const hasOptions = component.groupValues ? component.options.some(group => group[component.groupValues].length > 0) : component.options.length > 0
let isValid = checkValue(component.value)
if (hasOptions === false) {
isValid = true
}
el.setAttribute('data-dp-validate-is-valid', isValid)
const isValid = !hasOptions ? true : checkValue(component.value)

component.$el.setAttribute('data-dp-validate-is-valid', isValid)
validateMultiselect(component.$el)
}
}

function checkValue (val) {
if (!val) {
return false
}

let isValid

if (Array.isArray(val)) { // If it is a multiple select, value is Array
if (val.length === 0) {
isValid = false
Expand All @@ -54,6 +57,7 @@ function checkValue (val) {
} else if (typeof val === 'string') { // In single selects value is object or string
isValid = val !== ''
}

return isValid
}

Expand Down