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

react choices blinks and disappears when performing an event like formik's submit #1005

Open
joan3423 opened this issue Feb 9, 2022 · 1 comment
Labels

Comments

@joan3423
Copy link

joan3423 commented Feb 9, 2022

When I click my button to save the form all the selectors blink and it's as if they disappeared for a few seconds I have no idea why it happens

WhatsApp Image 2022-02-09 at 11 52 41 AM
WhatsApp Image 2022-02-09 at 11 52 01 AM

Does anyone have an idea why it happens?

this is the code

export default function ChoicesComponent(props) {

  const inputRef = useRef(false)

  const showSelectedOptions = () => {
    const selected = [...inputRef.current.options]
      .filter(option => option.selected)
      .map(option => option.value);
    const result = selected.map((item) => {
      const parts = item.split(" - ", 2);
      let currentItem = parts[0]
      return { [props.options.childName]: currentItem }
    })
    props.values[props.name] = result
  }

  useEffect(() => {
    const select = new Choices(inputRef.current, {
      choices: props.options.datas,
      addItemFilter: false,
      classNames: {
        containerInner: "p-3 choices__inner bg-white",
        list: "p-0",
      },
      removeItems: true,
      removeItemButton: true,
      searchPlaceholderValue: "Search",
      itemSelectText: "Select",
      noResultsText: "No se encontró",
    })
    if (!props.multiple) {
      select.setChoiceByValue(props.values[props.name])
    } else {
      select.setChoiceByValue(props.values[props.name].map(option => option[props.options.childName]))
    }
  }, [])

  return (
    <div className="position-relative">
      <select
        name={props.name}
        onChange={(event) => {
          if (props.multiple) {
            showSelectedOptions()
          } else {
            props.handleChange(event);
            props.customChangeFunction && 
            props.customChangeFunction(event.target.value, props.customSentValue);
          }
        }}
        ref={inputRef}
        multiple={props.multiple}
      >
        <option disabled value="">
          {props.defaultText}
        </option>
      </select>
    </div>
  )
}
@joan3423 joan3423 added the bug label Feb 9, 2022
@TimVerheul
Copy link

@joan3423 Did you find a solution to this?

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

No branches or pull requests

2 participants