Home > OS >  How to test svelte input attribute agains multiple options
How to test svelte input attribute agains multiple options

Time:12-28

i have store value and two sets of data:

$selectedColor

const productColor = {
   options: [
      {id:0, title: 'white'},
      {id:1, title: 'black'},
      {id:2, title: 'red'},
      {id:3, title: 'yellow'}
   ],
}


const productType = {
   options: [
      {id:0, title: 'somethings', hiddenInColor: 'white'},
      {id:1, title: 'somethings', hiddenInColor: 'black'},
      {
         id:2, 
         title: 'somethings', 
         hiddenInColor: 
        //here should be black AND white
        //problem is I can't understand how to use nested json with disable attribute (see below) 
      }
   ],
}

then i have populate inputs (type: radio) for product type and color

{#each productType.options as option (option.id)}
            {#if activeTab !== option.hiddenIn}
                <li>
                    <label>
                        <input type="radio" name="name" 
                            value={option.id} 
                            on:change={() => optionHasChanged(option.name, somethings)}
                            disabled={option.hiddenInColor == $selectedColor}
                            bind:group={defaultProductType}>
                        {option.title}
                    </label>
                </li>
            {/if}
        {/each}

I was trying to -> disable input IF $selectedColor == hiddenInColor

i did it like this ->

disabled={option.hiddenInColor == $selectedColor}

and it works well. But... only when you testing it against 1 value

i cant understand how to check more then 1 color at once. So basically, using my const productType -> i would like to disable id:3 if $selectedColor == 'black' || 'white'

any advices? Or maybe there is more compact solution to this? Thanks

CodePudding user response:

The simplest way to achieve this would be using an array hiddenInColors instead of a single scalar value:

const productType = {
   options: [
      {id:0, title: 'somethings', hiddenInColors: ['white']},
      {id:1, title: 'somethings', hiddenInColors: ['black']},
      {id:2, title: 'somethings', hiddenInColors: ['black','white']},
   ],
}

and then set the disabled option like this:

disabled={option.hiddenInColors.includes($selectedColor)}
  • Related