How to uncheck all other checkbox if data is “No Preference”?





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















How do I uncheck all checkbox if the data is no preference? I don't know how to manipulate the data.



This is the index.js:



import React, { Component } from "react";
import { Text, View } from 'react-native';
import { CheckBox } from 'react-native-elements';

import { Colors } from '../../../themes/';

import style from "./style";

class CCheckBox extends React.Component {

/////////////////////////////
// constructor()
/////////////////////////////
constructor(props, context) {
super(props, context);
console.log('custom/ccheckbox/index.js constructor()');
this.state = {
checked: false,
};
}

/////////////////////////////
// handleCheck()
/////////////////////////////
handleCheck() {
this.setState({ selectedCheckbox }); // update selected item
}

render() {

return (
<CheckBox
iconType='material'
checkedIcon='check'
uncheckedIcon='check-box-outline-blank'
checkedColor={Colors.ORANGE}
checked={this.state.checked}
containerStyle={style.content}
onPress={() => this.handleCheck()}
/>
);
}
}

export default CCheckBox;


And this is my profalcoholpref.js:



import React, { Component } from "react";
import { ScrollView, View } from 'react-native';
import { Content } from 'native-base';
import CButton from '../cbutton/index';
import PopSelectList from './popselectlist';

import styleC from "../../common/style";
import style from "./style";

class PopAlcoholPref extends React.Component {

///////////////////////////////
// constructor()
///////////////////////////////
constructor(props, context) {
super(props, context);
console.log('custom/cfield/popalcoholpref.js constructor()');
this.state = {
selectedCheckbox: {},
visible: this.props.visible,
data: [
{
id : 1,
code : 'DON',
description : 'Do not drink',
},
{
id : 2,
code : 'INF',
description : 'Infrequently',
},
{
id : 3,
code : 'SOC',
description : 'Socially',
},
{
id : 4,
code : 'MOD',
description : 'Moderately',
},
{
id : 5,
code : 'ASN',
description : 'As Needed',
},
{
id : 5,
code : 'NOP',
description : 'No Preference',
},
]
};

}

///////////////////////////////
// componentWillReceiveProps()
///////////////////////////////
componentWillReceiveProps(nextProps) {
console.log('componentWillReceiveProps()');
this.setState({
visible: nextProps.visible
});
}

///////////////////////////////
// handleSave()
///////////////////////////////
handleSave() {
console.log('handleSave()');
this.setState({
visible: false
});
}

///////////////////////////////
// render()
///////////////////////////////
render() {

return (
<View>
<PopSelectList title='Alcohol Preference' data={this.state.data} visible={this.state.visible} handleSave={() => this.handleSave()} />
</View>
);
}
}

export default PopAlcoholPref;


How do I uncheck all other checkbox if no preference is checked? Is there any way I can manipulate the data? Index.js is the frontend and I manipulated the checkbox there and in the prefalcohol is where the data is being stored.










share|improve this question





























    0















    How do I uncheck all checkbox if the data is no preference? I don't know how to manipulate the data.



    This is the index.js:



    import React, { Component } from "react";
    import { Text, View } from 'react-native';
    import { CheckBox } from 'react-native-elements';

    import { Colors } from '../../../themes/';

    import style from "./style";

    class CCheckBox extends React.Component {

    /////////////////////////////
    // constructor()
    /////////////////////////////
    constructor(props, context) {
    super(props, context);
    console.log('custom/ccheckbox/index.js constructor()');
    this.state = {
    checked: false,
    };
    }

    /////////////////////////////
    // handleCheck()
    /////////////////////////////
    handleCheck() {
    this.setState({ selectedCheckbox }); // update selected item
    }

    render() {

    return (
    <CheckBox
    iconType='material'
    checkedIcon='check'
    uncheckedIcon='check-box-outline-blank'
    checkedColor={Colors.ORANGE}
    checked={this.state.checked}
    containerStyle={style.content}
    onPress={() => this.handleCheck()}
    />
    );
    }
    }

    export default CCheckBox;


    And this is my profalcoholpref.js:



    import React, { Component } from "react";
    import { ScrollView, View } from 'react-native';
    import { Content } from 'native-base';
    import CButton from '../cbutton/index';
    import PopSelectList from './popselectlist';

    import styleC from "../../common/style";
    import style from "./style";

    class PopAlcoholPref extends React.Component {

    ///////////////////////////////
    // constructor()
    ///////////////////////////////
    constructor(props, context) {
    super(props, context);
    console.log('custom/cfield/popalcoholpref.js constructor()');
    this.state = {
    selectedCheckbox: {},
    visible: this.props.visible,
    data: [
    {
    id : 1,
    code : 'DON',
    description : 'Do not drink',
    },
    {
    id : 2,
    code : 'INF',
    description : 'Infrequently',
    },
    {
    id : 3,
    code : 'SOC',
    description : 'Socially',
    },
    {
    id : 4,
    code : 'MOD',
    description : 'Moderately',
    },
    {
    id : 5,
    code : 'ASN',
    description : 'As Needed',
    },
    {
    id : 5,
    code : 'NOP',
    description : 'No Preference',
    },
    ]
    };

    }

    ///////////////////////////////
    // componentWillReceiveProps()
    ///////////////////////////////
    componentWillReceiveProps(nextProps) {
    console.log('componentWillReceiveProps()');
    this.setState({
    visible: nextProps.visible
    });
    }

    ///////////////////////////////
    // handleSave()
    ///////////////////////////////
    handleSave() {
    console.log('handleSave()');
    this.setState({
    visible: false
    });
    }

    ///////////////////////////////
    // render()
    ///////////////////////////////
    render() {

    return (
    <View>
    <PopSelectList title='Alcohol Preference' data={this.state.data} visible={this.state.visible} handleSave={() => this.handleSave()} />
    </View>
    );
    }
    }

    export default PopAlcoholPref;


    How do I uncheck all other checkbox if no preference is checked? Is there any way I can manipulate the data? Index.js is the frontend and I manipulated the checkbox there and in the prefalcohol is where the data is being stored.










    share|improve this question

























      0












      0








      0








      How do I uncheck all checkbox if the data is no preference? I don't know how to manipulate the data.



      This is the index.js:



      import React, { Component } from "react";
      import { Text, View } from 'react-native';
      import { CheckBox } from 'react-native-elements';

      import { Colors } from '../../../themes/';

      import style from "./style";

      class CCheckBox extends React.Component {

      /////////////////////////////
      // constructor()
      /////////////////////////////
      constructor(props, context) {
      super(props, context);
      console.log('custom/ccheckbox/index.js constructor()');
      this.state = {
      checked: false,
      };
      }

      /////////////////////////////
      // handleCheck()
      /////////////////////////////
      handleCheck() {
      this.setState({ selectedCheckbox }); // update selected item
      }

      render() {

      return (
      <CheckBox
      iconType='material'
      checkedIcon='check'
      uncheckedIcon='check-box-outline-blank'
      checkedColor={Colors.ORANGE}
      checked={this.state.checked}
      containerStyle={style.content}
      onPress={() => this.handleCheck()}
      />
      );
      }
      }

      export default CCheckBox;


      And this is my profalcoholpref.js:



      import React, { Component } from "react";
      import { ScrollView, View } from 'react-native';
      import { Content } from 'native-base';
      import CButton from '../cbutton/index';
      import PopSelectList from './popselectlist';

      import styleC from "../../common/style";
      import style from "./style";

      class PopAlcoholPref extends React.Component {

      ///////////////////////////////
      // constructor()
      ///////////////////////////////
      constructor(props, context) {
      super(props, context);
      console.log('custom/cfield/popalcoholpref.js constructor()');
      this.state = {
      selectedCheckbox: {},
      visible: this.props.visible,
      data: [
      {
      id : 1,
      code : 'DON',
      description : 'Do not drink',
      },
      {
      id : 2,
      code : 'INF',
      description : 'Infrequently',
      },
      {
      id : 3,
      code : 'SOC',
      description : 'Socially',
      },
      {
      id : 4,
      code : 'MOD',
      description : 'Moderately',
      },
      {
      id : 5,
      code : 'ASN',
      description : 'As Needed',
      },
      {
      id : 5,
      code : 'NOP',
      description : 'No Preference',
      },
      ]
      };

      }

      ///////////////////////////////
      // componentWillReceiveProps()
      ///////////////////////////////
      componentWillReceiveProps(nextProps) {
      console.log('componentWillReceiveProps()');
      this.setState({
      visible: nextProps.visible
      });
      }

      ///////////////////////////////
      // handleSave()
      ///////////////////////////////
      handleSave() {
      console.log('handleSave()');
      this.setState({
      visible: false
      });
      }

      ///////////////////////////////
      // render()
      ///////////////////////////////
      render() {

      return (
      <View>
      <PopSelectList title='Alcohol Preference' data={this.state.data} visible={this.state.visible} handleSave={() => this.handleSave()} />
      </View>
      );
      }
      }

      export default PopAlcoholPref;


      How do I uncheck all other checkbox if no preference is checked? Is there any way I can manipulate the data? Index.js is the frontend and I manipulated the checkbox there and in the prefalcohol is where the data is being stored.










      share|improve this question














      How do I uncheck all checkbox if the data is no preference? I don't know how to manipulate the data.



      This is the index.js:



      import React, { Component } from "react";
      import { Text, View } from 'react-native';
      import { CheckBox } from 'react-native-elements';

      import { Colors } from '../../../themes/';

      import style from "./style";

      class CCheckBox extends React.Component {

      /////////////////////////////
      // constructor()
      /////////////////////////////
      constructor(props, context) {
      super(props, context);
      console.log('custom/ccheckbox/index.js constructor()');
      this.state = {
      checked: false,
      };
      }

      /////////////////////////////
      // handleCheck()
      /////////////////////////////
      handleCheck() {
      this.setState({ selectedCheckbox }); // update selected item
      }

      render() {

      return (
      <CheckBox
      iconType='material'
      checkedIcon='check'
      uncheckedIcon='check-box-outline-blank'
      checkedColor={Colors.ORANGE}
      checked={this.state.checked}
      containerStyle={style.content}
      onPress={() => this.handleCheck()}
      />
      );
      }
      }

      export default CCheckBox;


      And this is my profalcoholpref.js:



      import React, { Component } from "react";
      import { ScrollView, View } from 'react-native';
      import { Content } from 'native-base';
      import CButton from '../cbutton/index';
      import PopSelectList from './popselectlist';

      import styleC from "../../common/style";
      import style from "./style";

      class PopAlcoholPref extends React.Component {

      ///////////////////////////////
      // constructor()
      ///////////////////////////////
      constructor(props, context) {
      super(props, context);
      console.log('custom/cfield/popalcoholpref.js constructor()');
      this.state = {
      selectedCheckbox: {},
      visible: this.props.visible,
      data: [
      {
      id : 1,
      code : 'DON',
      description : 'Do not drink',
      },
      {
      id : 2,
      code : 'INF',
      description : 'Infrequently',
      },
      {
      id : 3,
      code : 'SOC',
      description : 'Socially',
      },
      {
      id : 4,
      code : 'MOD',
      description : 'Moderately',
      },
      {
      id : 5,
      code : 'ASN',
      description : 'As Needed',
      },
      {
      id : 5,
      code : 'NOP',
      description : 'No Preference',
      },
      ]
      };

      }

      ///////////////////////////////
      // componentWillReceiveProps()
      ///////////////////////////////
      componentWillReceiveProps(nextProps) {
      console.log('componentWillReceiveProps()');
      this.setState({
      visible: nextProps.visible
      });
      }

      ///////////////////////////////
      // handleSave()
      ///////////////////////////////
      handleSave() {
      console.log('handleSave()');
      this.setState({
      visible: false
      });
      }

      ///////////////////////////////
      // render()
      ///////////////////////////////
      render() {

      return (
      <View>
      <PopSelectList title='Alcohol Preference' data={this.state.data} visible={this.state.visible} handleSave={() => this.handleSave()} />
      </View>
      );
      }
      }

      export default PopAlcoholPref;


      How do I uncheck all other checkbox if no preference is checked? Is there any way I can manipulate the data? Index.js is the frontend and I manipulated the checkbox there and in the prefalcohol is where the data is being stored.







      react-native






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Jan 3 at 4:30









      Kenneth BunyiKenneth Bunyi

      132




      132
























          1 Answer
          1






          active

          oldest

          votes


















          0














          You will need a bit of a refactoring here I beleive.



          You should move the state handling logic to the list. In the list you can manipulate all the checkboxes at the same time.



          class List extends Component {
          constructor(props) {
          super(props);
          this.state = {
          checkBoxesList: [{
          id: 1,
          checked: false,
          }, {
          id: 2,
          checked: false,
          }]
          }
          }

          unCheckAll() {
          this.setState({ checkBoxesList: this.state.checkBoxesList.map(({ id }) => ({
          id: id,
          checked: false,
          })) })
          }

          checkBoxSelected(id) {
          const index = this.state.checkBoxesList.findIndex((value) => value.id === id);
          this.setState({ checkBoxesList[index]: {
          ...this.state.checkBoxesList[index],
          checked: !this.state.checkBoxesList[index].checked
          })
          }

          renderCheckBoxes() {
          return this.state.checkBoxesList.map(({id, checked}) => (
          <CheckBox id={id} checked={checked} onPress={this.checkBoxSelected} />
          ))
          }

          render() {
          return (
          <View>
          {this.renderCheckBoxes()}
          </View>
          )
          }
          }


          So this Component handles the states for the checkboxes. You need to make sure that you also implement the callback inside the CheckBox component for the OnPress method. Now calling the UncheckAll method will uncheck all the checkboxes.
          But also you have to put in some extra check before setting the checkBoxesList if the index does exist.






          share|improve this answer
























          • The creation of data and the checkbox are at different files sir.

            – Kenneth Bunyi
            Jan 4 at 4:48












          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54016375%2fhow-to-uncheck-all-other-checkbox-if-data-is-no-preference%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          You will need a bit of a refactoring here I beleive.



          You should move the state handling logic to the list. In the list you can manipulate all the checkboxes at the same time.



          class List extends Component {
          constructor(props) {
          super(props);
          this.state = {
          checkBoxesList: [{
          id: 1,
          checked: false,
          }, {
          id: 2,
          checked: false,
          }]
          }
          }

          unCheckAll() {
          this.setState({ checkBoxesList: this.state.checkBoxesList.map(({ id }) => ({
          id: id,
          checked: false,
          })) })
          }

          checkBoxSelected(id) {
          const index = this.state.checkBoxesList.findIndex((value) => value.id === id);
          this.setState({ checkBoxesList[index]: {
          ...this.state.checkBoxesList[index],
          checked: !this.state.checkBoxesList[index].checked
          })
          }

          renderCheckBoxes() {
          return this.state.checkBoxesList.map(({id, checked}) => (
          <CheckBox id={id} checked={checked} onPress={this.checkBoxSelected} />
          ))
          }

          render() {
          return (
          <View>
          {this.renderCheckBoxes()}
          </View>
          )
          }
          }


          So this Component handles the states for the checkboxes. You need to make sure that you also implement the callback inside the CheckBox component for the OnPress method. Now calling the UncheckAll method will uncheck all the checkboxes.
          But also you have to put in some extra check before setting the checkBoxesList if the index does exist.






          share|improve this answer
























          • The creation of data and the checkbox are at different files sir.

            – Kenneth Bunyi
            Jan 4 at 4:48
















          0














          You will need a bit of a refactoring here I beleive.



          You should move the state handling logic to the list. In the list you can manipulate all the checkboxes at the same time.



          class List extends Component {
          constructor(props) {
          super(props);
          this.state = {
          checkBoxesList: [{
          id: 1,
          checked: false,
          }, {
          id: 2,
          checked: false,
          }]
          }
          }

          unCheckAll() {
          this.setState({ checkBoxesList: this.state.checkBoxesList.map(({ id }) => ({
          id: id,
          checked: false,
          })) })
          }

          checkBoxSelected(id) {
          const index = this.state.checkBoxesList.findIndex((value) => value.id === id);
          this.setState({ checkBoxesList[index]: {
          ...this.state.checkBoxesList[index],
          checked: !this.state.checkBoxesList[index].checked
          })
          }

          renderCheckBoxes() {
          return this.state.checkBoxesList.map(({id, checked}) => (
          <CheckBox id={id} checked={checked} onPress={this.checkBoxSelected} />
          ))
          }

          render() {
          return (
          <View>
          {this.renderCheckBoxes()}
          </View>
          )
          }
          }


          So this Component handles the states for the checkboxes. You need to make sure that you also implement the callback inside the CheckBox component for the OnPress method. Now calling the UncheckAll method will uncheck all the checkboxes.
          But also you have to put in some extra check before setting the checkBoxesList if the index does exist.






          share|improve this answer
























          • The creation of data and the checkbox are at different files sir.

            – Kenneth Bunyi
            Jan 4 at 4:48














          0












          0








          0







          You will need a bit of a refactoring here I beleive.



          You should move the state handling logic to the list. In the list you can manipulate all the checkboxes at the same time.



          class List extends Component {
          constructor(props) {
          super(props);
          this.state = {
          checkBoxesList: [{
          id: 1,
          checked: false,
          }, {
          id: 2,
          checked: false,
          }]
          }
          }

          unCheckAll() {
          this.setState({ checkBoxesList: this.state.checkBoxesList.map(({ id }) => ({
          id: id,
          checked: false,
          })) })
          }

          checkBoxSelected(id) {
          const index = this.state.checkBoxesList.findIndex((value) => value.id === id);
          this.setState({ checkBoxesList[index]: {
          ...this.state.checkBoxesList[index],
          checked: !this.state.checkBoxesList[index].checked
          })
          }

          renderCheckBoxes() {
          return this.state.checkBoxesList.map(({id, checked}) => (
          <CheckBox id={id} checked={checked} onPress={this.checkBoxSelected} />
          ))
          }

          render() {
          return (
          <View>
          {this.renderCheckBoxes()}
          </View>
          )
          }
          }


          So this Component handles the states for the checkboxes. You need to make sure that you also implement the callback inside the CheckBox component for the OnPress method. Now calling the UncheckAll method will uncheck all the checkboxes.
          But also you have to put in some extra check before setting the checkBoxesList if the index does exist.






          share|improve this answer













          You will need a bit of a refactoring here I beleive.



          You should move the state handling logic to the list. In the list you can manipulate all the checkboxes at the same time.



          class List extends Component {
          constructor(props) {
          super(props);
          this.state = {
          checkBoxesList: [{
          id: 1,
          checked: false,
          }, {
          id: 2,
          checked: false,
          }]
          }
          }

          unCheckAll() {
          this.setState({ checkBoxesList: this.state.checkBoxesList.map(({ id }) => ({
          id: id,
          checked: false,
          })) })
          }

          checkBoxSelected(id) {
          const index = this.state.checkBoxesList.findIndex((value) => value.id === id);
          this.setState({ checkBoxesList[index]: {
          ...this.state.checkBoxesList[index],
          checked: !this.state.checkBoxesList[index].checked
          })
          }

          renderCheckBoxes() {
          return this.state.checkBoxesList.map(({id, checked}) => (
          <CheckBox id={id} checked={checked} onPress={this.checkBoxSelected} />
          ))
          }

          render() {
          return (
          <View>
          {this.renderCheckBoxes()}
          </View>
          )
          }
          }


          So this Component handles the states for the checkboxes. You need to make sure that you also implement the callback inside the CheckBox component for the OnPress method. Now calling the UncheckAll method will uncheck all the checkboxes.
          But also you have to put in some extra check before setting the checkBoxesList if the index does exist.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 3 at 13:42









          ddobby94ddobby94

          15319




          15319













          • The creation of data and the checkbox are at different files sir.

            – Kenneth Bunyi
            Jan 4 at 4:48



















          • The creation of data and the checkbox are at different files sir.

            – Kenneth Bunyi
            Jan 4 at 4:48

















          The creation of data and the checkbox are at different files sir.

          – Kenneth Bunyi
          Jan 4 at 4:48





          The creation of data and the checkbox are at different files sir.

          – Kenneth Bunyi
          Jan 4 at 4:48




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54016375%2fhow-to-uncheck-all-other-checkbox-if-data-is-no-preference%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          MongoDB - Not Authorized To Execute Command

          How to fix TextFormField cause rebuild widget in Flutter

          in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith