question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Tab selection and shouldSelect not working when Input is not first child of Hint Component

See original GitHub issue

Version

5.1.4

Steps to reproduce

Have a custom input with a label in renderInput method

       <Typeahead
              id={id}
              labelKey={labelKey}
              options={options}
              minLength={minLength}
              highlightOnlyResult={highlightOnlyResult}
              className={className}
              emptyLabel={emptyLabel}
              inputProps={{ 'aria-autocomplete': 'list', className: 'form-control', required }}
              onChange={(data) => onChange(data?.[0]?.[optionsValue])}
              inputRef={ref}
              defaultInputValue={defaultInputValue}
              renderInput={({ inputRef, referenceElementRef, ...inputProps }) => (
                <Hint shouldSelect={(shouldSelect, e) => e.keyCode === 9 || shouldSelect }>
                  <>
                    <InputTypeahead
                      id={id}
                      {...inputProps}
                      ref={(input) => {
                        inputRef(input);
                        referenceElementRef(input);
                      }}
                    />
                    <label className="form-control-placeholder" htmlFor={id}>
                      <span>{placeholder}</span>
                    </label>
                  </>
                </Hint>
              )}
              {...props}
            />

Expected Behavior

Item being selected when Tab key is pressed

Actual Behavior

Tab key actually moves to next item in DOM, without selecting any option

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
ericgiocommented, Jun 29, 2021

Hey @daniel-carrillo-globant, thanks for the question. Hint expects a single child, and for that child to either be your input or to correctly handle the keydown handler it adds. You could try taking the current children and making them a component to do that:

const InputWithLabel = (props) => {
  return (
    <>
      <InputTypeahead
        ...
        onKeyDown={props.onKeyDown}  
      />
      <label ... >
        <span>...</span>
      </label>
    </>
  );
};
1reaction
daniel-carrillo-globantcommented, Jun 29, 2021

@ericgio Thank you for your really fast reply! I’ve actually just found a workaround in here, I used the useHint hook and created this component with the label in it:

const CustomHint = (props) => {
  const { child, hintRef, hintText } = useHint(props);
  const { id, placeholder } = props;
  return (
    <div
      style={{
        display: 'flex',
        flex: 1,
        height: '100%',
        position: 'relative',
      }}
    >
      {child}
      <label className="form-control-placeholder" htmlFor={id}>
        <span>{placeholder}</span>
      </label>
      <input
        aria-hidden
        className="rbt-input-hint"
        ref={hintRef}
        readOnly
        style={{
          backgroundColor: 'transparent',
          borderColor: 'transparent',
          boxShadow: 'none',
          color: 'rgba(0, 0, 0, 0.35)',
          left: 0,
          pointerEvents: 'none',
          position: 'absolute',
          top: 0,
          width: '100%',
        }}
        tabIndex={-1}
        value={hintText}
      />
    </div>
  );
};

And inside the renderInput method I’ve just called it this way:

          renderInput={({ inputRef, referenceElementRef, ...inputProps }) => (
                <CustomHint id={id} placeholder={placeholder}>
                  <InputTypeahead
                    id={id}
                    {...inputProps}
                    ref={(input) => {
                      inputRef(input);
                      referenceElementRef(input);
                    }}
                  />
                </CustomHint>
              )}

However, your response seems to be also accurate, I’ll try that one as well. Thank you very much!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why is my first-child selector not working on the first element in ...
:first-child matches the first child of its parent. Your first a.trigger is not the first child — that would be the classless a...
Read more >
first-of-type - CSS: Cascading Style Sheets - MDN Web Docs
The :first-of-type CSS pseudo-class represents the first element of its type among a group of sibling elements.
Read more >
Advanced Marketplace Issues and Technical Support
This course provides training on how to help consumers in individual market FFMs with more complex eligibility and enrollment issues not previously covered...
Read more >
RAPIDS 7.3 User Guide - SSI LRC
customer may assume their CAC is not working. Their systems administrators can install the latest DISA root certificates if this occurs.
Read more >
Pipeline: Input Step - Jenkins
This step pauses Pipeline execution and allows the user to interact and control the flow of the build. Only a basic "proceed" or...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found