Giter Site home page Giter Site logo

Comments (6)

bluebill1049 avatar bluebill1049 commented on July 19, 2024

thanks for your feedback @shoeman22 i will update the example as yours :)

from input.

bluebill1049 avatar bluebill1049 commented on July 19, 2024
          <section>
            <label>MUI Select (wrapped)</label>
            <RHFInput
              as={<WrappedSelect name="test" />}
              name="WrappedSelect"
              register={register}
              setValue={setValue}
            />
          </section>

have you try the above?

from input.

shoeman22 avatar shoeman22 commented on July 19, 2024

Yep, that looks like that approach would work as well for the value itself, but the other props aren't going to be sent down.

from input.

bluebill1049 avatar bluebill1049 commented on July 19, 2024

what do you mean other props?

<section>
            <label>React Select</label>
            <RHFInput
              as={<ReactSelect />}
              options={options}
              name="ReactSelect"
              {...methods}
              isClearable // you can pass props here as well
            />
</section>

from input.

shoeman22 avatar shoeman22 commented on July 19, 2024

What I'd initially done was something like this:

// alternately, it could've been thought of as:
// const WrappedSelect = (propsFromRHFInputThatWereIgnored) => (
const WrappedSelect = () => (
  <Select>
    <MenuItem value={10}>Ten</MenuItem>
    <MenuItem value={20}>Twenty</MenuItem>
    <MenuItem value={30}>Thirty</MenuItem>
  </Select>
);

return (
  <RHFInput
     as={<WrappedSelect />}
     name="CountryId"
     register={register}
     setValue={setValue}
     variant="outlined"
     margin="normal" // bunch of material-ui Select props
  />
);

And I couldn't figure out why variant, margin, value, etc. weren't getting applied to the Select (material-ui was complaining about trying to set the value to undefined on the Select). The problem was if you use a wrapper, RHFInput is actually cloning the wrapper element (WrappedSelect) and it is getting the props passed down to it. So you need to forward those props that are coming in to the real Select element in that case by passing the props down:

const WrappedSelect = (propsFromRHFInputImNowForwarding) => (
  <Select {...propsFromRHFInputImNowForwarding}>
    <MenuItem value={10}>Ten</MenuItem>
    <MenuItem value={20}>Twenty</MenuItem>
    <MenuItem value={30}>Thirty</MenuItem>
  </Select>
);

from input.

bluebill1049 avatar bluebill1049 commented on July 19, 2024

sorry for the late reply. i do spread rest of the props to the as component

return React.cloneElement(as, {
      ...(onChangeEvent
        ? {
            [onChangeName || 'onChange']: eventWrapper(
              onChangeEvent,
              'onChange',
            ),
          }
        : { onChange: handleChange }),
      ...(isOnBlur
        ? onBlurEvent
          ? { [onBlurName || 'onBlur']: eventWrapper(onBlurEvent, 'onBlur') }
          : { onBlur: handleBlur }
        : {}),
      value: value || inputValue,
      ...(isCheckbox ? { checked: inputValue } : {}),
      ...rest,
    });

i am going to close this issue now, feel free to leave more questions and suggestions tho. 👍

from input.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.