首页 > 解决方案 > Start search on Enter using React material-table

问题描述

I'm new to using React and material-table and am trying to sort out how to initiate search after the user presses Enter. I've been able to find the debounceInterval option, but the API I am using to search remote data currently does not return partial matches so I'd like the user to press "Enter" when they want the search to start.

Using monitorEvents($0) I can see that there is a keydown Event on the element input.MuiInputBase-input.MuiInput-input.MuiInputBase-inputAdornedStart.MuiInputBase-inputAdornedEnd but I'm not sure how to add an event listener here or is there is an option to specify which event.target.value to use to start the search. The material-table Search code is here: https://github.com/mbrn/material-table/blob/master/src/components/m-table-toolbar.js

Any help would be greatly appreciated.

标签: reactjsmaterial-table

解决方案


例子:

<TextField
  onKeyPress={(ev) => {
   console.log(`Pressed keyCode ${ev.key}`);
    if (ev.key === 'Enter') {
  // Do code here
     ev.preventDefault();
    }
  }}
  />

推荐阅读