3lvis / Form

The most flexible and powerful way to build a form on iOS
http://hyper.no
Other
1.64k stars 145 forks source link

Segment get unselected when combined with Show target #585

Closed AnthoPakPak closed 6 years ago

AnthoPakPak commented 6 years ago

Hi,

I've just tried to use the library but I encounter a bug with segment. I've bind a Show target on a specific value of the segment, and a Hide one on others. Whenever I click on the value with the Show target, the value get instantly unselected (it looks like it deselect when the Show has completed). The target field is correctly shown, but the clicked segment value get deselected. If I click a second time on the same value (so no target has to be performed), the value stay correctly selected.

I've tried several workarounds to reselect the value but none of them worked, for example :

self.dataSource.fieldUpdatedBlock = ^(id cell, FORMField *field) {
        BOOL shouldUpdateCivilite = ([field.fieldID isEqualToString:@"civilite"]);

        if (shouldUpdateCivilite) {
            [weakSelf.dataSource fieldWithID:@"civilite"
                       includingHiddenFields:YES
                                  completion:^(FORMField *field, NSIndexPath *indexPath) {
                                      if (field) {
                                          [field selectFieldValueWithValueID:@"Mme"];
                                          [weakSelf.dataSource reloadFieldsAtIndexPaths:@[indexPath]];
                                      }
                                  }];
        }
};

Here is my json file :

{
   "groups":[
      {
         "id":"demandeur-group",
         "title":"Demandeur",
         "sections":[
            {
               "id":"demandeur-group-0",
               "fields":[

                  {
                     "id":"civilite",
                     "title":"Civilité",
                     "type":"segment",
                     "values":[
                        {
                           "id":"M",
                           "title":"M",
                           "default":true,
                           "targets":[
                              {
                                 "id":"last_name_jeune_fille",
                                 "type":"field",
                                 "action":"hide"
                              }
                           ]
                        },
                        {
                           "id":"Mme",
                           "title":"Mme",
                           "targets":[
                              {
                                 "id":"last_name_jeune_fille",
                                 "type":"field",
                                 "action":"show"
                              }
                           ]
                        },
                        {
                           "id":"Mlle",
                           "title":"Mlle",
                           "targets":[
                              {
                                 "id":"last_name_jeune_fille",
                                 "type":"field",
                                 "action":"hide"
                              }
                           ]
                        }
                     ],
                     "size":{
                        "width":100,
                        "height":1
                     }
                  },

                  {
                     "id":"last_name",
                     "title":"Nom",
                     "type":"name",
                     "info":"Comme sur la pièce d'identité",
                     "size":{
                        "width":50,
                        "height":1
                     },
                     "validations":{
                        "required":true,
                        "min_length":2
                     }
                  },

                  {
                     "id":"first_name",
                     "title":"Prénom",
                     "info":"Comme sur la pièce d'identité",
                     "type":"name",
                     "size":{
                        "width":50,
                        "height":1
                     },
                     "validations":{
                        "required":true,
                        "min_length":2
                     }
                  },

                  {
                     "id":"last_name_jeune_fille",
                     "title":"Nom de jeune fille",
                     "type":"name",
                     "info":"Comme sur la pièce d'identité",
                     "size":{
                        "width":50,
                        "height":1
                     },
                     "validations":{
                        "required":true,
                        "min_length":2
                     }
                  }

               ]
            }
         ]
      }
   ]
}

If anyone has an idea to help, I would be really thankful :)

AnthoPakPak commented 6 years ago

I've found that the issue had already been solved here PR, but changes had not been reported in Cocoapods version.

But I've made a fix to get expected behavior :

replace NSInteger *selectedIndex = [self.segment selectedSegmentIndex]; with NSInteger selectedIndex = [self.segment selectedSegmentIndex];

AND

replace if (selectedIndex || selectedIndex == 0) with if (selectedIndex && selectedIndex != UISegmentedControlNoSegment)

3lvis commented 6 years ago

I see, time to publish a new version then :)