Home > OS >  how to obtain the value of the input?
how to obtain the value of the input?

Time:09-12

I am trying to make a clinic locator that when you put where you are, you get as a result the nearby clinics, I can not enter data in the imput, not even let me write and I can not find the problem.

import React,  { useState, useEffect } from 'react'
import { getClinic } from '../../api/drupalAPI'
import {Clinic} from '@icofcv/common';
import Spinner from '../spinner/Spinner';



interface Props {
    showModalLocator: boolean,
    closeModalLocator: () => void
}


export const ClinicLocator: React.FC<Props> = ({ children, showModalLocator, closeModalLocator }) => {

    const [clinicList, setClinicList] = useState<Clinic[]>([]);
    const [clinicListFiltered, setClinicListFiltered] = useState<Clinic[]>([]);
    const [searchClinic, setSearchClinic] = useState("");


    const handleChange= (e) => async () => {
        getClinic().then((response)=>{
            console.log(response)
            setClinicList(response);
            setClinicListFiltered(response)
        }).catch ( (error)  => {
            console.error(error);
            throw error;
        });
        setSearchClinic(e.target.value);
        filter(e.target.value);
      }
      
      const filter=(termSearch)=>{
        const resultSearch= clinicList.filter((element)=>{
          if(element.address?.toString().toLowerCase().includes(termSearch.toLowerCase())
          || element.province?.toString().toLowerCase().includes(termSearch.toLowerCase())
          || element.town?.toString().toLowerCase().includes(termSearch.toLowerCase())
          ){
            return element;
          }
        });
        setClinicListFiltered(resultSearch);
      }

    return (
        <>
              <div>
                {showModalLocator ? (
                    <>
                        <div className="justify-center items-center flex overflow-x-hidden overflow-y-auto fixed inset-0 z-50 outline-none focus:outline-none">
                            <div className="relative p-2 w-full max-w-3xl h-full md:h-auto">
                                {/*content*/}
                                <div className="relative bg-white rounded-lg shadow">
                                    {/*header*/}
                                    <div className="flex justify-between items-start px-4 py-3 rounded-t border-b">
                                        <h3 className="text-lg font-medium">Localizador de clinicas</h3>
                                        <button className="text-gray-400 bg-transparent hover:bg-gray-200 hover:text-gray-900 rounded-lg text-sm p-1.5 ml-auto inline-flex items-center" onClick={closeModalLocator}>
                                            <svg aria-hidden="true" className="w-5 h-5" fill="currentColor" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg">
                                                <path fill-rule="evenodd" d="M4.293 4.293a1 1 0 011.414 0L10 8.586l4.293-4.293a1 1 0 111.414 1.414L11.414 10l4.293 4.293a1 1 0 01-1.414 1.414L10 11.414l-4.293 4.293a1 1 0 01-1.414-1.414L8.586 10 4.293 5.707a1 1 0 010-1.414z" clip-rule="evenodd"></path>
                                            </svg>
                                        </button>
                                    </div>
                                    {/*body*/}
                                    <div className="relative px-3 py-3 flex-auto overflow-auto modal-body">
                                        <h2 className="text-sm font-medium mb-2">¿Dónde te encuentras?</h2>
                                        <input 
                                        value={searchClinic}
                                        onChange={handleChange}
                                        type="search" 
                                        className="w-100 bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block p-2" 
                                        placeholder="Introduce una ubicación"
                                        />
                                        <div className="py-3 border-b flex flex-col md:flex-row items-start md:items-center md:justify-between justify-start gap-2">
                                            <div className="flex items-center">
                                                <label className="inline-flex relative items-center mr-5 cursor-pointer">
                                                    <input type="checkbox" className="sr-only peer" checked/>
                                                    <div className="w-11 h-6 bg-gray-200 rounded-full peer peer-focus:ring-teeal-600 
                                                    peer-checked:after:translate-x-full peer-checked:after:border-white after:content-[''] 
                                                    after:absolute after:top-0.5 after:left-[2px] after:bg-white after:border-gray-300 after:border after:rounded-full after:h-5 after:w-5 after:transition-all peer-checked:bg-teal-600"></div>
                                                    <span className="ml-2 text-xs font-medium text-gray-900">Centro privado</span>
                                                </label>
                                            </div>
                                            <div className="flex items-center">
                                                <label className="inline-flex relative items-center mr-5 cursor-pointer">
                                                    <input type="checkbox" className="sr-only peer" readOnly/>
                                                    <div className="w-11 h-6 bg-gray-200 rounded-full peer peer-focus:ring-teeal-600 
                                                    peer-checked:after:translate-x-full peer-checked:after:border-white after:content-[''] 
                                                    after:absolute after:top-0.5 after:left-[2px] after:bg-white after:border-gray-300 after:border after:rounded-full after:h-5 after:w-5 after:transition-all peer-checked:bg-teal-600"></div>
                                                    <span className="ml-2 text-xs font-medium text-gray-900">con Mútuas</span>
                                                </label>
                                            </div>
                                            <div className="flex items-center">
                                                <label className="inline-flex relative items-center mr-5 cursor-pointer">
                                                    <input type="checkbox" className="sr-only peer" readOnly/>
                                                    <div className="w-11 h-6 bg-gray-200 rounded-full peer peer-focus:ring-teeal-600 
                                                    peer-checked:after:translate-x-full peer-checked:after:border-white after:content-[''] 
                                                    after:absolute after:top-0.5 after:left-[2px] after:bg-white after:border-gray-300 after:border after:rounded-full after:h-5 after:w-5 after:transition-all peer-checked:bg-teal-600"></div>
                                                    <span className="ml-2 text-xs font-medium text-gray-900">con Compañías de Salud</span>
                                                </label>
                                            </div>                                            
                                        </div>
                                        <div>
                                            <h2 className="text-sm font-medium my-3">Resultados</h2>
                                            <div className="w-100">
                                                <iframe className="w-100" src="https://www.google.com/maps/embed?pb=!1m18!1m12!1m3!1d2664.3238269926374!2d-0.3805919350162851!3d39.46959682083709!2m3!1f0!2f0!3f0!3m2!1i1024!2i768!4f13.1!3m3!1m2!1s0xd604f4bee0957f3:0x6686ff7d230b3965!2zQy4gZGUgU2FudC
                                                BWaWNlbnQgTcOgcnRpciwgNjEsIHBpc28gMsK6LCBwdGEgMsKqLCA0NjAwMiBWYWzDqG5jaWEsIEVzcGHDsWE!5e0!3m2!1ses!2sus!4v1662388390673!5m2!1ses!2sus" loading="lazy"></iframe>
                                            </div>
                                            <div className="md:mt-4 overflow-auto relative py-2">
                                                <div className="grid grid-cols-1 md:grid-cols-2 gap-2">
                                                    <div className="card bg-white px-2 py-3 h-36 md:h-32">
                                                        {!clinicListFiltered ? <Spinner /> :
                                                            clinicListFiltered.map((clinic) => (
                                                        <button key={clinic.id} type="button" className="text-left">
                                                            <div className="flex items-center gap-2 md:gap-4 md:gap-4">
                                                                <img className="h-24 w-2/5 min-w-40 object-cover object-center rounded-lg" src="../carousel-1.svg" alt="#"/>
                                                                <div className="w-3/5">
                                                                    <div className="text-md font-medium leading-5 clinic-title uppercase">{clinic.title}</div>
                                                                    <div className="flex items-center gap-2">
                                                                        <div className="text-neutral-500 text-sm">{clinic.propsPhone}</div>
                                                                        <div className="text-neutral-500 text-sm">{clinic.mobile}</div>
                                                                    </div>
                                                                    <div className="text-teal-600 text-sm underline clinic-mail">{clinic.email}</div>
                                                                    <div className="text-neutral-500 text-sm">{clinic.registry}</div>
                                                                </div>
                                                            </div>
                                                        </button>
                                                            ))
                                                        }
                                                    </div>
                                                    {/* <div className="card bg-white px-2 py-3 h-36 md:h-32">
                                                        <button type="button" className="text-left">
                                                            <div className="flex items-center gap-2 md:gap-4 md:gap-4">
                                                                <img className="h-24 w-2/5 min-w-40 object-cover object-center rounded-lg" src="../carousel-1.svg" alt="#"/>
                                                                <div className="w-3/5">
                                                                    <div className="text-md font-medium leading-5 clinic-title uppercase">AlamedaVLC</div>
                                                                    <div className="flex items-center gap-2">
                                                                        <div className="text-neutral-500 text-sm">963255652</div>
                                                                        <div className="text-neutral-500 text-sm">963255652</div>
                                                                    </div>
                                                                    <div className="text-teal-600 text-sm underline clinic-mail">[email protected]</div>
                                                                    <div className="text-neutral-500 text-sm">9947</div>
                                                                </div>
                                                            </div>
                                                        </button>
                                                    </div> */}
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                    {/*footer*/}
                                    <div className="flex items-center justify-end px-4 py-2 border-t border-solid border-slate-200 rounded-b gap-2">
                                        <button className="btn text-black text-sm background-transparent px-8 outline-none focus:outline-none focus:ring-teal-600 focus:border-teal-600" type="button" onClick={closeModalLocator}>Cancelar</button>
                                        <button className="btn bg-teal-600 hover:bg-teal-700 text-white text-sm active:bg-teal-700 px-8 outline-none focus:outline-none" type="button" onClick={closeModalLocator}>Buscar</button>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div className="opacity-25 fixed inset-0 z-40 bg-black"></div>
                    </>
                ) : null}

            </div>

       </>
    )
}

CodePudding user response:

Try to change the line

const handleChange= (e) => async () => {

to

const handleChange = (e) => {

You're currently returning a function, which means that it doesn't get called. There is also no need for async as you don't have any await in the function.

CodePudding user response:

handleChange returns a function which is never executed; remove the currying as it is not needed:

const handleChange= (e) => {
    getClinic().then((response)=>{
        console.log(response)
        setClinicList(response);
        setClinicListFiltered(response)
    }).catch ( (error)  => {
        console.error(error);
        throw error;
    });
    setSearchClinic(e.target.value);
    filter(e.target.value);
}

The reason you cannot input anything is that React treats the input field as controlled component, which means that it itself won't take care of updating the fields value since you passed an initial value (of ""). With controlled components, you as the developer are required to handle the state outside of the input field, i.e. update the value attribute of the input component. Since that failed due to the mistake outlined above, the value of the input field remains fixed at "", thus not allowing you to type in the field.

Further reading:

  • Related