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

Carbon Multi Select Not Resetting Correctly With Form Reset #1412 #1413

Open
GilbertCherrie opened this issue Sep 14, 2023 · 3 comments
Open

Comments

@GilbertCherrie
Copy link
Contributor

Possible bug found with the carbon multi select component and the form reset functionality. I am running into an issue where resetting a form correctly returns all fields to initial values except the mutli select field. It seems to be receiving the correct values after clicking reset (the initial selected values) but the component seems to not react to this change visually as it still holds whatever check boxes were checked before hitting the reset button.

@Hyperkid123
Copy link
Member

@GilbertCherrie can you provide the form schema, please?

@GilbertCherrie
Copy link
Contributor Author

Form schema:

    {
      component: componentTypes.TEXT_FIELD,
      label: __('Full Name'),
      maxLength: 50,
      id: 'name',
      name: 'name',
      isDisabled: disabled,
      isRequired: true,
      validate: [{
        type: validatorTypes.REQUIRED,
        message: __('Required'),
      }],
    },
    {
      component: componentTypes.TEXT_FIELD,
      label: __('Username'),
      maxLength: 255,
      id: 'userid',
      name: 'userid',
      isDisabled: disabled,
      isRequired: true,
      validate: [{
        type: validatorTypes.REQUIRED,
        message: __('Required'),
      }],
    },
    ...(id ? [
      ...(editMode ? [
        {
          component: 'edit-password-field',
          label: __('Password'),
          id: 'password',
          name: 'password',
          maxLength: 50,
          editMode,
          disabled: !editMode,
          setEditMode: () => {
            setState((state) => ({
              ...state,
              editMode: !editMode,
            }));
          },
          placeholder: '●●●●●●●●',
          buttonLabel: editMode ? __('Cancel') : __('Change'),
        },
        {
          component: componentTypes.TEXT_FIELD,
          label: __('Confirm Password'),
          maxLength: 50,
          type: 'password',
          id: 'confirmPassword',
          name: 'confirmPassword',
          initialValue: '',
          isRequired: true,
        },
      ] : [
        {
          component: 'edit-password-field',
          label: __('Password'),
          maxLength: 50,
          id: 'passwordPlaceholder',
          name: 'passwordPlaceholder',
          editMode,
          disabled: true,
          setEditMode: () => {
            setState((state) => ({
              ...state,
              editMode: !editMode,
            }));
          },
          placeholder: '●●●●●●●●',
          buttonLabel: editMode ? __('Cancel') : __('Change'),
        },
      ]),
    ] : [
      {
        component: componentTypes.TEXT_FIELD,
        label: __('Password'),
        maxLength: 50,
        type: 'password',
        id: 'password',
        name: 'password',
        isRequired: true,
        validate: [{
          type: validatorTypes.REQUIRED,
          message: __('Required'),
        }],
      },
      {
        component: componentTypes.TEXT_FIELD,
        label: __('Confirm Password'),
        maxLength: 50,
        type: 'password',
        id: 'confirmPassword',
        name: 'confirmPassword',
        initialValue: '',
        isRequired: true,
        validate: [{
          type: validatorTypes.REQUIRED,
          message: __('Required'),
        }],
      },
    ]),
    {
      component: componentTypes.TEXT_FIELD,
      label: __('E-mail Address'),
      maxLength: 255,
      id: 'email',
      name: 'email',
      validate: [{
        type: validatorTypes.PATTERN,
        pattern: '[a-z0-9._%+-]+@[a-z0-9.-]+.[a-z]{2,}$',
        message: __('Email must be a valid email address'),
      }],
    },
    {
      component: componentTypes.SELECT,
      label: __('Available Groups'),
      id: 'groups',
      name: 'groups',
      isDisabled: disabled,
      isMulti: true,
      isRequired: true,
      placeholder: __('<Choose one or more Groups>'),
      options: availableGroups,
      validate: [{
        type: validatorTypes.REQUIRED,
        message: __('Required'),
      }],
      onChange: (values) => {
        const groups = [];
        values.forEach((group) => {
          if (group.label) {
            groups.push(group.label);
          } else {
            availableGroups.forEach((availableGroup) => {
              if (availableGroup.value === group) {
                groups.push(availableGroup.label);
              }
            });
          }
        });
        setState((state) => ({
          ...state,
          selectedGroups: groups,
        }));
      },
    },

@GilbertCherrie
Copy link
Contributor Author

Some of the components in the schema have some custom functionality but I noticed this same issue with other simpler forms that use this component as well so I don't think the custom functionality should be an issue. I also noticed this issue before any of the onChange code was added to the component.

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

No branches or pull requests

2 participants