From 95975afbf0711368ce4149ca0f3e8776161c2ab6 Mon Sep 17 00:00:00 2001 From: Philipp Burgmer Date: Mon, 25 Sep 2017 22:21:22 +0200 Subject: [PATCH] feat(single-select): add option to force array output for single-select mode --- src/model/config.model.ts | 2 ++ src/w11k-select.directive.ts | 20 +++++++++++++------- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/src/model/config.model.ts b/src/model/config.model.ts index 0a8d286..b84d29b 100644 --- a/src/model/config.model.ts +++ b/src/model/config.model.ts @@ -27,6 +27,8 @@ export class ConfigInstance { hideCheckboxes: boolean = false; /** single or multiple select */ multiple: boolean = true; + /** force ngModel to be an array for single select too */ + forceArrayOutput = false; /** disable user interaction */ disabled: boolean = false; /** all the configuration for the header (visible if dropdown closed) */ diff --git a/src/w11k-select.directive.ts b/src/w11k-select.directive.ts index a598da5..992ae21 100644 --- a/src/w11k-select.directive.ts +++ b/src/w11k-select.directive.ts @@ -522,7 +522,7 @@ export function w11kSelect (w11kSelectConfig: Config, let modelValue; - if (scope.config.multiple) { + if (scope.config.multiple || scope.config.forceArrayOutput) { modelValue = viewValue; } else { modelValue = viewValue[0]; @@ -531,12 +531,18 @@ export function w11kSelect (w11kSelectConfig: Config, return modelValue; } - function validateRequired(viewValue) { - if (scope.config.multiple === true && scope.config.required === true && viewValue.length === 0) { - return false; - } - if (scope.config.multiple === false && scope.config.required === true && viewValue === undefined) { - return false; + function validateRequired (value): boolean { + if (scope.config.required) { + if (scope.config.multiple === true && value.length === 0) { + return false; + } + if (scope.config.multiple === false && scope.config.forceArrayOutput === true && value.length === 0) { + return false; + } + if (scope.config.multiple === false && value === undefined) { + return false; + } + } return true;