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

FormSelect: Compatibility with react-hook-form #9771

Closed
lavocatt opened this issue Oct 25, 2023 · 2 comments
Closed

FormSelect: Compatibility with react-hook-form #9771

lavocatt opened this issue Oct 25, 2023 · 2 comments
Labels

Comments

@lavocatt
Copy link

lavocatt commented Oct 25, 2023

Hello 👋

I'm trying to make PF5 work with the react-hook-form library

Considering this minimalist code example:

type Inputs = {
  PF5Select: string;
  JSSelect: string;
};

const ImageOutput = () => {
  const { register, watch } = useForm<Inputs>({});
  const pf5Select = watch("PF5Select");
  const jsSelect = watch("JSSelect");
  console.log("DEMO release", pf5Select);
  console.log("DEMO category", jsSelect);

  const { onChange, onBlur, name, ref } = register("PF5Select");

  return (
    <Form>
      <FormGroup>
        <FormSelect
          ouiaId="release_select"
          onChange={onChange}
          onBlur={onBlur}
          name={name}
          ref={ref}
        >
          <FormSelectOption value="A" label="Category A" />
          <FormSelectOption value="B" label="Category B" />
        </FormSelect>
        <select {...register("JSSelect")}>
          <option value="A">Category A</option>
          <option value="B">Category B</option>
        </select>
      </FormGroup>
    </Form>
  );
};

I would expect the two select to have the same behavior which is to keep the selection the user made and to populate the state accordingly. However the behavior is different, the PF5 select isn't working as I would expect it to.

You can test this out on the code sandbox there https://codesandbox.io/s/cold-sea-cs7wqr

I think it's due to the fact that there's no forwarding for the ref to the inner select of the FormSelect. And if I'm not mistaken there was a similar issue with TextInput in the past #2913.

Will it be possible to have a support for ref for the FormSelect please?

Alternatively, if that's not possible, what are you recommending for form state validation and state management ?

Thanks for your help.

Is this request originating from a Red Hat product team? If so, which ones and is there any sort of deadline for this enhancement?

We're from the Image Builder team and we want to refactor our image creation wizard by the end of q4 2023.

@lavocatt
Copy link
Author

I've found a way to make it work:

const ImageOutput = () => {
  const { register, setValue, watch } = useForm<Inputs>({});
  const pf5Select = watch("category");
  console.log("DEMO release", pf5Select);

  const { name } = register("category");

  return (
    <Form>
      <FormGroup>
        <FormSelect
          ouiaId="release_select"
          value={pf5Select}
          onChange={(value) =>
            setValue("category", value.currentTarget.value, {
              shouldValidate: true
            })
          }
          name={name}
        >
          <FormSelectOption value="A" label="Category A" />
          <FormSelectOption value="B" label="Category B" />
        </FormSelect>
      </FormGroup>
    </Form>
  );
};

https://codesandbox.io/s/strange-tess-sl62gp

Would still be neat to have it work with the ref though.

@wise-king-sullyman wise-king-sullyman moved this from Needs triage to Backlog in PatternFly Issues Oct 31, 2023
Copy link

stale bot commented Jan 9, 2024

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.

@stale stale bot added the wontfix label Jan 9, 2024
@stale stale bot closed this as completed Jan 26, 2024
@github-project-automation github-project-automation bot moved this from Backlog to Done in PatternFly Issues Jan 26, 2024
@tlabaj tlabaj removed this from the Prioritized Backlog milestone Nov 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Archived in project
Development

No branches or pull requests

3 participants