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

Question: How to add icon to DayPickerInput #998

Closed
diegodesouza opened this issue Mar 4, 2020 · 3 comments
Closed

Question: How to add icon to DayPickerInput #998

diegodesouza opened this issue Mar 4, 2020 · 3 comments

Comments

@diegodesouza
Copy link

diegodesouza commented Mar 4, 2020

I have tried several things in an attempt to add a calendar icon to the DayPickerInput without success. I'd be great if we had an easier way to do so. Perhaps a prop on it?

Can you provide an example using a component or a className? I'm currently using the react-icons package.

thanks in advance

@marcus-kindred
Copy link

You can pass a custom component to render in place of the provided input.

See the component property in the documentation: http://react-day-picker.js.org/api/DayPickerInput

Usage example:

<DayPickerInput
    component={(props: InputHTMLAttributes<HTMLInputElement>) => (
        <CustomInput icon={<MyIcon />} {...props} />
    )}
/>

Your custom input might look something like this:

interface InputProps extends InputHTMLAttributes<HTMLInputElement> {
  icon?: React.ReactNode;
}

const Input: React.FC<InputProps> = ({ icon, className, ...rest }) => {
  return (
    <div className={className}>
      {icon === undefined ? (
        <input {...rest} />
      ) : (
        <>
          <input {...rest} />
          {icon}
        </>
      )}
    </div>
  );
};

Your base html inputs will need some styling on of course.

@marcus-kindred
Copy link

Also note that the code above will be buggy, you will want to also handle forwarding the ref on to your custom input.

@marcus-kindred
Copy link

Handling the refs in TypeScript if it helps anyone:

Also fixes the issue #977

const CustomInputCore: React.RefForwardingComponent<
  HTMLInputElement,
  InputHTMLAttributes<HTMLInputElement>
> = ({ value, ...props }, ref) => (
  <MyInput ref={ref} icon={<MyIcon />} value={value ?? ''} {...props} />
);

const CustomInput = React.forwardRef(CustomInputCore);

<DayPickerInput component={CustomInput} />

With your "MyInput" component looking like:

interface InputProps extends InputHTMLAttributes<HTMLInputElement> {
  icon?: React.ReactNode;
}

const Input: React.RefForwardingComponent<HTMLInputElement, InputProps> = (
  { icon, className, ...rest },
  ref
) => {
  return (
    <div className={className}>
      {icon === undefined ? (
        <input ref={ref} {...rest} />
      ) : (
        <>
          <input ref={ref} {...rest} />
          {icon}
        </>
      )}
    </div>
  );
};

export default React.forwardRef(Input);

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