Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom Input Field on datepicker

i implemented the react-datepicker in my app.

everythings fine except i want to customize the input field of the datepicker and adapt it to my other custom fields like title input.

when i change the datepicker via the

customInput={<Input />}

field, the looks of it have changed but i cant select dates anymore (the picker isnt working anymore).

heres my code:

<DatePicker
    customInput={<Input />}
    dateFormat="DD.MM.YYYY"
    selected=...
    onChange=...

/>

any ideas?

    export namespace Input { 
        export interface Props { 
            onChange: (event: any) => void; 
            placeholder?: string; 
            value?: string | number; 
            isSecure?: any; 
            id?: string; 
        }

        // ...
    } 

So I added the clock event in the following way:

    export namespace Input {
        export interface Props {
            onChange: (event: any) => void;
            placeholder?: string;
            value?: string | number;
            isSecure?: any;
            id?: string;
            onClick: (event: any) => void;
        }
    }

is that right?

component code:

export class Input extends React.Component<Input.Props, Input.State> {
  public render() {
    const controlClass = classNames(
      [style.control]
    );
    const inputClass = classNames(
      [style.input]
    );
    return (
      <p className={controlClass} >
        <input
          id={this.props.id}
          onChange={this.props.onChange}
          className={inputClass}
          type={this.props.isSecure ? "password" : "text"}
          placeholder={this.props.placeholder}
          value={this.props.value}
        />
      </p>
    );
  }
}
like image 608
sleepysimon Avatar asked Dec 17 '22 23:12

sleepysimon


1 Answers

Your Input component needs to implement an onClick event and make that available as a prop because that is what triggers the date picker to open itself.

const Input = ({onChange, placeholder, value, isSecure, id, onClick}) => (
    <input
        onChange={onChange}
        placeholder={placeholder}
        value={value}
        isSecure={isSecure}
        id={id}
        onClick={onClick}
    />
);

const NoClickInput = ({onClick, ...props}) => <Input {...props} />;

class App extends Component {
    state = {
        value: moment(),
    };

    render() {
        return (
            <div>
                <DatePicker
                    value={this.state.value}
                    dateFormat="DD.MM.YYYY"
                    customInput={<Input />}
                    selected={this.state.date}
                    onChange={date => this.setState({date})}
                />
                <DatePicker
                    value={this.state.value}
                    dateFormat="DD.MM.YYYY"
                    customInput={<NoClickInput />} {/* Will not work */}
                    selected={this.state.date}
                    onChange={date => this.setState({date})}
                />
            </div>
        );
    }
}

Edit q7j9jl75pj

EDIT:

A possible work around without touching the implementation of your Input component would be to wrap it into a container and handle a click on that instead:

const ClickableInput = ({onClick, ...props}) => (
    <div onClick={onClick}>
        <Input {...props}>
    </div>
);

Then use ClickableInput instead of Input as your custom input.

like image 197
trixn Avatar answered Dec 28 '22 22:12

trixn