简体   繁体   English

以编程方式点击反应组件

[英]clicking on react component programmatically

I have found similar question in some regard , I guarantee this ain't a duplicate.在某些方面发现了类似的问题,我保证这不是重复的。

Background :背景
The component I'm using is the all popular react day picker .我使用的组件是所有流行的react day picker Since, I found it's <DayPickerInput/> so hard to customize, I ended up customizing the <DayPicker/> instead.因为,我发现<DayPickerInput/>很难自定义,所以我最终自定义了<DayPicker/> I'm also using a react <Select> element for a dropdown, the default <select> (note the case) doesn't even work.我还在下拉列表中使用 react <Select>元素,默认的<select> (注意大小写)甚至不起作用。 To use this we had to pass some component to their captionElement prop, so the overall code till now looks like this:要使用它,我们必须将一些组件传递给他们的captionElement道具,所以captionElement的整体代码如下所示:

//required variables defined
const [display, setDisplay] = useState(false);
<input onFocus={()=>setDisplay(true)/>
const MyCustomComponent = (props)=>{
 <Select name="monthDropdown" options={...options}/>
 <Select name="yearDropdown" options={...options}/>
}

...
display && <DayPicker
          {...props}
          captionElement={({ date, localeUtils }) => (
            <MyCustomComponent date={date} localeUtils={localeUtils} onChange={handleChange} />
          )}

The Question:问题:
The problem is, when I click the ` component, it seems to get re-rendered, and there's a flicker on the first click(only the first click):问题是,当我点击 ` 组件时,它似乎被重新渲染,并且在第一次点击时有闪烁(只有第一次点击): 在此处输入图片说明

It appears it doesn't flicker once it gets the focus.一旦获得焦点,它似乎不会闪烁。 The only "hackaround" I can think of is by programmatically clicking the datepicker as it appears, so that when the user clicks with a mouse there is no flicker.我能想到的唯一“hackaround”是通过以编程方式单击出现的日期选择器,以便当用户用鼠标单击时没有闪烁。 But, how do I do that with a library component, the ref property is not exposed, and I don't know any other way.但是,我如何使用库组件来做到这一点, ref属性没有公开,而且我不知道任何其他方式。

What can be a solution to this problem?什么可以解决这个问题?

EDIT: The layout code is as so:编辑:布局代码如下:

import 'Select' from 'react-select';
import 'DayPicker' from 'react-day-picker';

//main function:
const [popup, setPopup] = useState(false);
const [customDate, setCustomDate] = useState();
const [selectedMonth, setMySelectionOfMonth] = useState();
const [selectedDay, setMySelectionOfDay] = useState();
const [focusState, setFocusState] = useState();
const [focusDatePicker , setMyDatePickerFocus] = useState();

const handleInputFocusHere = ()=>{
    setPopup(true);
}

<input type="date" value={customDate||''} onFocus={handleInputFocusHere}/>

    popup && <MyCustomDatePicker
    //pass all the above context variables here/>

//more code 
//end



const MyCustomDatePicker (props) => {
    const {customDate, setCustomDate} = props;
    const {selectedMonth, setMySelectionOfMonth} = useState();
    const {selectedDay, setMySelectionOfDay} = useState();
    const {focusState, setFocusState} = useState();
    const {focusDatePicker , setMyDatePickerFocus} = useState();

  function MyCustomCalendar(props) {
    const { onChange } = props;

    function changeMyMonth(selectedOption) {
     //some code
    }

    function changeMyYear(selectedOption) {
        //some code
    }

    return (
      <div className="DayPicker" style={{ border: '0' }}>
        <div style={{ display: 'flex', flexDirection: 'row' }}>
          <div className="mr-4x" style={{ minWidth: '100px' }}>
            <Select
              options={monthData}
              styles={customStyles}
              value={monthValue}
              onChange={changeMyMonth}
            />
          </div>
          <div style={{ minWidth: '70px' }}>
            <Select
              options={yearData}
              styles={customStyles}
              value={yearValue}
              onChange={changeMyYear}
            />
          </div>
        </div>
      </div>
    );
  }

  
  function changeMyYearMonth(month: Date) {
    setMySelectionOfMonth(month);
  }

  const handleMyCustomDayClick = (day: Date) => {
    setMySelectionOfDay(day);
    setCustomDate(day);
  };

  const onCalendarFocusChange = (focusedInput: FocusedInputShape | null) => {
    setFocusState(focusedInput);
    setMyDatePickerFocus(true);
  };

  const handleDefocus = () => {
    setFocusState(null);
    setMyDatePickerFocus(false);
  };

  const handleCalendarFocus = () => {
    setMyDatePickerFocus(true);
  };


  return (
    <div>
      
      {(focusState || focusDatePicker) && (
        <div>
          <DayPicker
            onDayClick={handleMyCustomDayClick}
            month={selectedMonth}
            className={dateFocusClass}
            fromMonth={fromMonth}
            toMonth={toMonth}
            selectedDays={selectedDay}
            canChangeMonth={false}
            onFocus={handleCalendarFocus}
            captionElement={({ date, localeUtils }) => (
              <MyCustomCalendar date={date} localeUtils={localeUtils} onChange={changeMyYearMonth} />
            )}
          />
        </div>
      )}
    </div>
  );
};

export default MyCustomDatePicker;

You can either try wrapping your element in a div and clicking it (pretty sure it won't work, but you can try), or clicking one of the custom components you made should have the same effect.您可以尝试将元素包装在div并单击它(很确定它不起作用,但您可以尝试),或者单击您制作的自定义组件之一应该具有相同的效果。

However, what you seem to have here is more of a layout issue and I would personally try to solve that.但是,您在这里似乎更多的是布局问题,我个人会尝试解决该问题。 Maybe give your day picker component more space wherever it is on the page so it doesn't have to jitter like that.也许给你的日期选择器组件更多的空间在页面上的任何地方,这样它就不必像那样抖动。 Perhaps if you showed the layout of the page where you use it, maybe I could offer some ideas.也许如果你展示了你使用它的页面布局,也许我可以提供一些想法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM