Next.js Discord

Discord Forum

undefined value

Unanswered
Marble gall posted this in #help-forum
Open in Discord
Marble gallOP
Hello, I have this function:
const handleSearch = (e) => {
debounceSetSearch(e.target.value);
};

And I have this component in Material UI:
<TextField
sx={{ my: '0px', pt: '0px', pb: '0px', mb: '3px', width: '11.4rem' }}
label="Search"
size="small"
onKeyUp={(e) => {
if (e.key === 'Enter') {
handleSearch(e);
}
}}
InputProps={{
startAdornment: (
<InputAdornment
position="start"
onClick={(e) => {
console.log('e:', e.target.value);
handleSearch(e);
}}
style={{ cursor: 'pointer' }}
>
<SearchTwoToneIcon />
</InputAdornment>
),
}}
/>

The problem with the onClick prop is always return 'undefined', I don't know how to fix it

38 Replies

The console.log is fired and is not undefined? @Marble gall
@B33fb0n3 The console.log is fired and is not undefined? <@983812722284183612>
Marble gallOP
Hi, the console.log give me undefined every time I click
@Marble gall Hi, the console.log give me undefined every time I click
then the event on the InputAdornment doesn't exists. You want to put the event somewhere where onClick is available
Marble gallOP
Yes
soo problem solved? lol xD
Marble gallOP
still doesn't work
Where do I have put the prop 'onClick' ?
where are the click event's for this icon?
Marble gallOP
here
 onClick={(e) => {
                            console.log('e:', e.target.value);
                            handleSearch(e);
                          }}
 <InputAdornment position="start">
  <IconButton
    onClick={() => { console.log("click") }}
    edge="end"
  >
    <SearchTwoToneIcon />
  </IconButton>
</InputAdornment>
Marble gallOP
I try it
and I try it this
<TextField
                    sx={{ my: '0px', pt: '0px', pb: '0px', mb: '3px', width: '11.4rem' }}
                    label="Search"
                    size="small"
                    onKeyUp={(e) => {
                      if (e.key === 'Enter') {
                        handleSearch(e);
                      }
                    }}
                    InputProps={{
                      startAdornment: (
                        <InputAdornment position="start">
                          <IconButton
                            onClick={(e) => {
                              handleSearch(e);
                              console.log('click', e.target.value);
                            }}
                            edge="end"
                          >
                            <SearchTwoToneIcon />
                          </IconButton>
                        </InputAdornment>
                      ),
                    }}
                  />
I think the problem is the "startAdornment", does not allow the event to travel
you should put the value to state or something
Marble gallOP
yes, the value is in the props of my component, for example, the prop onKeyUp works perfect
the click event would not have the value from the input
Marble gallOP
so how I can fix it?
@Marble gall so how I can fix it?
const searchTextRef = useRef();

  const handleSearch = () => {
    debounceSetSearch(searchTextRef.current);
  };

  return (
    <TextField
      ref={searchTextRef}
      sx={{ my: "0px", pt: "0px", pb: "0px", mb: "3px", width: "11.4rem" }}
      label="Search"
      size="small"
      onChange={e => {
        searchTextRef.current = e.target.value;
      }}
      onKeyUp={(e) => {
        if (e.key === "Enter") {
          handleSearch();
        }
      }}
      InputProps={{
        startAdornment: (
          <InputAdornment position="start">
            <IconButton
              onClick={(e) => {
                handleSearch();
                console.log("click", e.target.value);
              }}
              edge="end"
            >
              <SearchTwoToneIcon />
            </IconButton>
          </InputAdornment>
        ),
      }}
    />
  );
Marble gallOP
I made it and I don't have success, I see the problem is de onClick inside the "startAdornment" , because if I move the onClick outside it works, but inside doesn't work
ah ok
so it works now?
Marble gallOP
yes but I need the SearchTwoToneIcon have the onClick
why? it doesnt have onClick props
that's why you need to wrap it with IconButton
Marble gallOP
Because I need the icon to be clickable, the problem is that everything I put with onclick inside the InputProps does not work, if I put it outside it works
check their example
the password input
Marble gallOP
let's see
I see it but it doesn't have any onClick prop
have a type "password"
oh 1 minute
Marble gallOP
I make this like the example:
const searchInputRef = React.useRef(null);
<TextField
                    id="search-input"
                    inputRef={searchInputRef}
                    sx={{ my: '0px', pt: '0px', pb: '0px', mb: '3px', width: '11.4rem' }}
                    label="Search"
                    size="small"
                    onKeyUp={(e) => {
                      if (e.key === 'Enter') {
                        handleSearch(e);
                      }
                    }}
                    InputProps={{
                      startAdornment: (
                        <InputAdornment position="start">
                          <IconButton
                            onClick={() => {
                              handleSearch(searchInputRef.current?.value);
                              console.log('searchInputRef.current?.value', searchInputRef.current?.value);
                            }}
                            edge="end"
                          >
                            <SearchTwoToneIcon />
                          </IconButton>
                        </InputAdornment>
                      ),
                    }}
                  />

but doesn't work
 const searchInputRef = React.useRef(null);

  function handleSearch() {
    debounceSetSearch(searchInputRef.current);
  }

  return (
    <TextField
      id="search-input"
      inputRef={searchInputRef}
      sx={{ my: "0px", pt: "0px", pb: "0px", mb: "3px", width: "11.4rem" }}
      label="Search"
      size="small"
      onKeyUp={(e) => {
        if (e.key === "Enter") {
          handleSearch();
        }
      }}
      InputProps={{
        startAdornment: (
          <InputAdornment position="start">
            <IconButton
              onClick={() => {
                handleSearch();
                console.log(
                  "searchInputRef.current?.value",
                  searchInputRef.current?.value
                );
              }}
              edge="end"
            >
              <SearchTwoToneIcon />
            </IconButton>
          </InputAdornment>
        ),
      }}
    />
  );
Marble gallOP
it works, thank you!
please mark solution😆
Marble gallOP
where I do that?
right click > application > mark solution