0

Basically I have this Select box that I'm trying to validate the inputs going into it, they can't be shorter or longer than 7 characters, and all the characters have to be numbers, and the input box shouldn't let you put in a duplicate value.

This code is kinda messy but I've been going back and forth with deepseek trying to get it working, so apologies for the spaghetti code! I feel like it's a pretty simple thing to do, and I'm overthinking the solution.

These input filters are making sure that the inputs sent to the PID state are correct, including duplicate removal. The problem is that the select box shows the duplicate value even when filtered out via the multiple functions.

Shows a select box that has the value 1234567,1234567

here's the input behavior I'm trying to avoid above. No matter what filters I put on the props, I can't get the duplicate values to display correctly, and I'm pretty stumped on how to fix it.

Thanks in advance for any help!

< Select
mode = "tags"
value = {
  pid
}
onChange = {
  (selectedPIDs) => {
    // Convert all entries to strings first for consistent validation
    console.log("SelectedPIDs:");
    console.log(selectedPIDs);
    const stringPIDs = selectedPIDs.map(pid => pid.toString());

    // Filter valid (7-digit) and remove duplicates
    const validUniquePIDs = [...new Set(
      stringPIDs.filter(pidStr => /^\d{7}$/.test(pidStr))
    )].map(Number); // Convert back to numbers

    // Update both local and form state
    console.log("validUniquePIDs:");
    console.log(validUniquePIDs);
    setPid(validUniquePIDs);
    setCompany(prev => ({
      ...prev,
      pid: validUniquePIDs
    }));
  }
}
onBlur = {
  () => {
    const stringPIDs = pid.map(pid => pid.toString());
    const validatedPIDs = stringPIDs.filter(p => /^\d{7}$/.test(p.toString()));
    // Filter valid (7-digit) and remove duplicates
    const validUniquePIDs = [...new Set(
      validatedPIDs.filter(pidStr => /^\d{7}$/.test(pidStr))
    )].map(Number); // Convert back to numbers
    setPid(validUniquePIDs);
    setCompany({
      ...company,
      pid: validUniquePIDs
    });
  }
}
disabled = {
  isReadOnly
}
tokenSeparators = {
  []
}
onInputKeyDown = {
  (e) => {
    if (e.key === 'Enter') {
      e.preventDefault();
      // Manually trigger validation
      const stringPIDs = pid.map(pid => pid.toString());
      const validUniquePIDs = [...new Set(
        stringPIDs.filter(pidStr => /^\d{7}$/.test(pidStr))
      )].map(Number); // Convert back to numbers
      setPid(validUniquePIDs);
      setCompany({
        ...company,
        pid: validUniquePIDs
      });
    }
  }
}
allowClear
dropdownStyle = {
  {
    display: 'none'
  }
}
notFoundContent = {
  null
} // Hide dropdown entirely
/>

8
  • 1
    if you console.log in onChange, do you see the expected values? don't you also need to use a Set in your onBlur/onInputKeyDown filtering? Commented Jul 8 at 18:55
  • @sparkJ I added some console.logs to the onChange after cleaning it up a bit (code has been changed in the OG post with the most updated version), got this back when trying the above example of adding "1234567" to a input box with "1234567" already inside: SelectedPIDs: company-details.jsx:1050 (2) [1234567, '1234567'] company-details.jsx:1059 validUniquePIDs: company-details.jsx:1060 [1234567] Commented Jul 8 at 19:31
  • 1
    it looks like your onChange filtering is working as intended. it may be that the state is not being handled correctly. can you show where you manage the state? Commented Jul 8 at 19:52
  • The only state handling I'm doing is in the Select box above, other than the initial creation of the Company state and the Pid state: const [ company, setCompany ] = useState( {} ); const [ pid, setPid ] = useState( [] ); the state is being set in the onChange, onBlur, and onInputKeyDown with setPid(validUniquePIDs); setCompany({ ...company, pid: validUniquePIDs }); Commented Jul 8 at 20:03
  • 1
    i'm glad you have it functioning, but i would strongly recommend examining your state management. a wrapper like what you described is NOT needed. state-driven UI is something React excels at. ant's examples show this, even when using controlled inputs. a helpful article on forced renders good luck! Commented Jul 9 at 15:42

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.