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

@nstudio/nativescript-checkbox: Toggle is being called twice when binding the checked to an attribute. #132

Open
leftundersun opened this issue May 16, 2023 · 1 comment

Comments

@leftundersun
Copy link

If you make it this way:

<template>
  <Page>
    <StackLayout
      orientation="vertical">
      <check-box
        v-for="(option, index) in options"
        :key="index"
        :text="option"
        :checked="index == selected"
        boxType="circle"
        @tap="selectOption(index)"
        @checkedChange="changed(index)"/>
    </StackLayout>
  </Page>
</template>

<script lang="coffee">

export default
  
  data: () ->
    {
      options: [
        "First"
        "Second"
        "Third"
        "Fourth"
      ]
      selected: null
    }

  methods:

    selectOption: (index) ->
      if this.selected == index
        this.selected = null
      else
        this.selected = index

    changed: (index) ->
      console.log "changed #{index}"

</script>

and click on an already selected option, you became able to select another one.

@leftundersun leftundersun changed the title Toggle is being called twice when binding the checked to an attribute. @nstudio/nativescript-checkbox: Toggle is being called twice when binding the checked to an attribute. May 17, 2023
@NathanWalker
Copy link
Member

NathanWalker commented May 26, 2023

Hi @leftundersun check out this sample:
https://stackblitz.com/edit/nativescript-checkbox-example?file=src%2Fapp%2Fitem%2Fitems.component.html

Although it's shown in Angular you can adapt to Vue this way (feel free to adjust Vue syntax as needed):

<check-box
    v-for="(option, index) in options"
    :key="index"
    :text="option"
    :checked="checkboxValue"
    @checkedChange="checkedChange"
    @loaded="checkboxLoaded"
    boxType="circle"
    fontSize="20"
    ios:fillColor="#fff"
    onTintColor="#007A42"
  />


checkbox: CheckBox;
checkboxValue: boolean;

  checkboxLoaded(e) {
    this.checkbox = e.object;
  }

checkedChange() {
    if (this.checkbox) {
      this.checkboxValue = this.checkbox.checked;
      console.log('Checkbox value:', this.checkbox.checked);
    }
  }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants