ionic-framework icon indicating copy to clipboard operation
ionic-framework copied to clipboard

feat: Support `input#list`

Open oliveryasuna opened this issue 1 year ago • 3 comments

Prerequisites

Describe the Feature Request

Add the list property to <ion-input>.

<ion-input list="my-list"/>
<!-- OR React -->
<IonInput list="my-list"/>
<!-- OR whatever syntax for other frameworks. -->

<datalist id="my-list">
  ...
</datalist>

Is there a reason it is not supported? Perhaps because it does not represent native text field behavior? Note: There is very little styling possible on <datalist>.

Describe the Use Case

To connect to a <datalist> in order to show suggestions.

Describe Preferred Solution

No response

Describe Alternatives

Here is my current workaround:

<IonItem className="ion-no-padding">
  {/*This is a hack to make sure the input below is styled.*/}
  <IonInput style={{display: 'none'}}/>
  {/*TODO: Platform-dependent class names.*/}
  <div className="sc-ion-input-ios-h sc-ion-input-ios-s ios input-label-placement-start in-item">
    <label htmlFor="code" className="input-wrapper sc-ion-input-ios">
      <div className="label-text-wrapper label-text-wrapper-hidden sc-ion-input-ios sc-ion-input-ios-s"></div>
      <div className="native-wrapper sc-ion-input-ios sc-ion-input-ios-s">
        <input
            name="code"
            type="text"
            autoCapitalize="none"
            autoComplete="off"
            autoCorrect="off"
            spellCheck="false"
            placeholder="or enter code"
            list="codes"
            className="native-input sc-ion-input-ios"/>
        <datalist id="codes">
          <option>aaa</option>
          <option>aab</option>
          <option>aac</option>
          <option>cca</option>
          <option>ccb</option>
          <option>ccc</option>
        </datalist>
      </div>
    </label>
  </div>
</IonItem>

Related Code

No response

Additional Information

No response

oliveryasuna avatar Mar 25 '24 19:03 oliveryasuna

I'm looking for this too. In my React project, I've solved it like this for now:

<IonInput
  ref={async (node) => {
    const input = await node?.getInputElement();
    input?.setAttribute("list", "codes");
  }}
  label="Code"
  type="text"
/>
<datalist id={"codes"}>
  <option value={"aaa"} />
  <option value={"aab"} />
  <option value={"aac"} />
  ...
</datalist>

jberglinds avatar Apr 06 '24 12:04 jberglinds

I'm looking for this too. In my React project, I've solved it like this for now:

<IonInput
  ref={async (node) => {
    const input = await node?.getInputElement();
    input?.setAttribute("list", "codes");
  }}
  label="Code"
  type="text"
/>
<datalist id={"codes"}>
  <option value={"aaa"} />
  <option value={"aab"} />
  <option value={"aac"} />
  ...
</datalist>

I feel silly for not thinking of this!

oliveryasuna avatar Apr 24 '24 19:04 oliveryasuna

@jberglinds Actually, your solution seems to cause a bug. You have to click the input multiple times for it to show the datalist.

oliveryasuna avatar Apr 26 '24 18:04 oliveryasuna