Live Search

General

You need to use a fetchCallback property to format the data of the API callback, which will match the data structure of the component.


Show Code
import React from "react";
import LiveSearch from 'funda-ui/LiveSearch';
 
// component styles
import 'funda-ui/LiveSearch/index.css';
 
export default () => {
 
    return (
        <>
            <LiveSearch
                name="app-livesearch-name"
                label="Food List (Enter the search character)"
                options={`
                [
                    {"label": "Option 1","value": "value-1","queryString": "option1"},
                    {"label": "<del style=color:red>deprecate</del>Option 2","value": "value-2","queryString": "option2"},
                    {"label": "Option 3","value": "value-3","queryString": "option3"},
                    {"label": "Option 4","value": "value-4","queryString": "option4", "disabled":true}
                ]  
                `}
            />
 
 
 
            <LiveSearch
                name="app-livesearch-name"
                label="Food List (Direct display)"
                hideIcon
                autoShowOptions
                options={`
                [
                    {"label": "Option 1","listItemLabel":"Option 1 (No: 001)","value": "value-1","queryString": "option1"},
                    {"label": "Option 2","listItemLabel":"<del style=color:red>deprecate</del>Option 2 (No: 002)","value": "value-2","queryString": "option2"},
                    {"label": "Option 3","listItemLabel":"Option 3 (No: 003)","value": "value-3","queryString": "option3"},
                    {"label": "Option 4","listItemLabel":"Option 4 (No: 004)","value": "value-4","queryString": "option4", "disabled":true}
                ]  
                `}
            />
 
        </>
    );
}

No spacing


Show Code
import React from "react";
import LiveSearch from 'funda-ui/LiveSearch';
 
// component styles
import 'funda-ui/LiveSearch/index.css';
 
export default () => {
 
 
    return (
        <>
 
            <LiveSearch
                ...
                wrapperClassName="position-relative"
                ...
            />
 
             <LiveSearch
                ...
                wrapperClassName=""
                ...
            />
 
        </>
    );
}

Asynchronous loading option

You need to use a fetchCallback property to format the data of the API callback, which will match the data structure of the component.


Show Code
import React from "react";
import LiveSearch from 'funda-ui/LiveSearch';
import axios from 'axios';
 
// component styles
import 'funda-ui/LiveSearch/index.css';
 
class DataService {
    
    // `getList()` must be a Promise Object
    async getList(searchStr = '', limit = 0, otherParam = '') {
 
        console.log('searchStr: ', searchStr);
        console.log("limit: ", limit);
        console.log("otherParam: ", otherParam);
 
 
        if ( searchStr === '------') return {
            code: 0,
            message: 'OK',
            data: []
        };
 
 
        return {
            code: 0,
            message: 'OK',
            data: [
                {item_name: 'banana', item_code: 'b', kb_code: 'banana,xiangjiao,xj'},
                {item_name: 'apple', item_code: 'a', kb_code: 'apple,pingguo,pg'},
                {item_name: 'lemon', item_code: 'l', kb_code: 'lemon,ningmeng,nm'},
                {item_name: 'juice', item_code: 'j', kb_code: 'juice,guozhi,gz'},
                {item_name: 'coffee', item_code: 'c', kb_code: 'coffee,kafei,kf'}
            ]
        };
    }
 
 
 
    async getListUseAxios(searchStr = '', limit = 0) {
        let _data = null;
        const res = await axios.get(`https://api`, {
            params: {
                s: searchStr,
                limit: limit
            },
            headers: {
                'Authorization': 'Bearer xxxx-xxxxxxxx-xxxxxxxx'
                'Content-Type': 'application/json'
            }
        }).catch(function (error) {
            console.log(error);
        });
 
        if (res && res.status == 200) _data = res.data;
 
 
        // result
        if (_data === null) {
            return {
                code: 0,
                message: 'OK',
                data: []
            };
        } else {
            return {
                code: 0,
                message: 'OK',
                data: _data
            };
        }
 
    }
 
    	
}
 
export default () => {
 
    return (
        <>
            <LiveSearch
                btnId="app-livesearch-btn"
                name="app-livesearch-name"
                label="Food List (Enter a,b, or c)"
                fetchTrigger={false}
                fetchUpdate={false}
                fetchNoneInfo="No match yet"
                fetchFuncAsync={new DataService}
                fetchFuncMethod="getList"
                fetchFuncMethodParams={['$QUERY_STRING',0]}
                fetchCallback={(res) => {
 
                    const formattedData = res.map((item: any, i: number) => {
                        return {
                            label: item.item_name,
                            listItemLabel: `${item.item_name} (No. ${i})`,
                            value: item.item_code,
                            queryString: item.kb_code,
                            disabled: item.item_code === 'j' ? true : false
                        }
                    }); 
 
                    console.log(formattedData);
                    /*
                    [
                        {
                            "label": "banana",
                            "listItemLabel": "banana (No. 0)",
                            "value": "b",
                            "queryString": "banana,xiangjiao,xj",
                            "disabled": false
                        },
                        {
                            "label": "apple",
                            "listItemLabel": "apple (No. 1)",
                            "value": "a",
                            "queryString": "apple,pingguo,pg",
                            "disabled": false
                        },
                        {
                            "label": "lemon",
                            "listItemLabel": "lemon (No. 2)",
                            "value": "l",
                            "queryString": "lemon,ningmeng,nm",
                            "disabled": false
                        },
                        {
                            "label": "juice",
                            "listItemLabel": "juice (No. 3)",
                            "value": "j",
                            "queryString": "juice,guozhi,gz",
                            "disabled": true
                        },
                        {
                            "label": "coffee",
                            "listItemLabel": "coffee (No. 4)",
                            "value": "c",
                            "queryString": "coffee,kafei,kf",
                            "disabled": false
                        }
                    ]
                    */
 
                    return formattedData;
                }}
                onFetch={(res: any[]) => {
                    console.log('onFetch: ', res);
 
                }}
                onChange={(input: HTMLInputElement, data: any[], selectedData: any) => {
                    console.log('onChange: ', data, selectedData);
 
                    const changeValue = selectedData !== '' ? selectedData.value : input.value;
                    document.querySelector('[name="app-livesearch-name-v"]').value = changeValue;
                    
                }}
                onBlur={(input: HTMLInputElement) => {
                    console.log('onBlur: ', input);
                }}
            />
            <input
                type="text"
                name="app-livesearch-name-v"
            />
        </>
    );
}

Create a non-retrieval <input> that can be input and selected

Proper use of autoShowOptions attribute. hideIcon is optional.


Show Code
import React from "react";
import LiveSearch from 'funda-ui/LiveSearch';
 
// component styles
import 'funda-ui/LiveSearch/index.css';
 
class DataService {
    
    // `getList()` must be a Promise Object
    async getList() {
 
  
        return {
            code: 0,
            message: 'OK',
            data: [
                {item_name: 'banana', item_code: 'b', kb_code: 'banana,xiangjiao,xj'},
                {item_name: 'apple', item_code: 'a', kb_code: 'apple,pingguo,pg'},
                {item_name: 'lemon', item_code: 'l', kb_code: 'lemon,ningmeng,nm'},
                {item_name: 'juice', item_code: 'j', kb_code: 'juice,guozhi,gz'},
                {item_name: 'coffee', item_code: 'c', kb_code: 'coffee,kafei,kf'}
            ]
        };
    }
 
}
 
export default () => {
 
    return (
        <>
            <LiveSearch
                btnId="app-livesearch-btn"
                name="app-livesearch-name"
                label="Food List (Enter a,b, or c)"
                hideIcon
                autoShowOptions
                fetchFuncAsync={new DataService}
                fetchFuncMethod="getList"
                fetchFuncMethodParams={[]}
                fetchCallback={(res) => {
 
                    const formattedData = res.map((item: any) => {
                        return {
                            label: item.item_name,
                            value: item.item_code,
                            queryString: item.kb_code
                        }
                    }); 
 
                    return formattedData;
                }}
                onFetch={(res: any[]) => {
                    console.log('onFetch: ', res);
                }}
                onChange={(input: HTMLInputElement, data: any[], selectedData: any) => {
                    console.log('onChange: ',input.value, data, selectedData);
 
                    const changeValue = selectedData !== '' ? selectedData.value : input.value;
                }}
            />
        </>
    );
}

API

Live Search

import LiveSearch from 'funda-ui/LiveSearch';
PropertyTypeDefaultDescriptionRequired
refReact.ForwardedRef-It is the return element of this component.-
contentRefReact.ForwardedRef-It exposes the following methods:
  1. contentRef.current.control()
  2. contentRef.current.clear(() => { console.log('callback') })
  3. contentRef.current.set('test value', () => { console.log('callback') })
DO NOT USE it in the onChange of this component, otherwise it will cause infinite rendering
-
popupRefReact.ForwardedRef-It exposes the following methods when the component's popup opens or closes:
  1. popupRef.current.close()
-
wrapperClassNamestringmb-3 position-relativeThe class name of the control wrapper.-
controlClassNamestringform-controlThe class name of the control.-
controlExClassNamestring-The extended class name of controlClassName.-
controlGroupWrapperClassNamestringinput-groupThe class name of the control group wrapper.-
controlGroupTextClassNamestringinput-group-textThe class name of the control group text.-
exceededSidePosOffsetnumber15Offset px that exceeds the far right or left side of the screen-
optionsJSON Object Literals | JSON Object-Set the default value using JSON string format for menu of options, like this: [{"label": "Option 1","value": "value-1","queryString": "option1"},{"label": "<del style=color:red>deprecate</del>Option 2","value": "value-2","queryString": "option2"},{"label": "Option 3","value": "value-3","queryString": "option3"},{"label": "Option 4","value": "value-4","queryString": "option4","disabled":true}]
Note: Use API data if database query exists. That is, the attribute fetchXXXX

When the attribute hierarchical is true, you need to use a hierarchical structure to pass data, such as: [{label:"Top level 1",value:'level-1',queryString:""},{label:"Top level 2",value:'level-2',queryString:""},{label:"Top level 3",value:'level-3',queryString:"",children:[{label:"Sub level 3_1",value:'level-3_1',queryString:""},{label:"Sub level 3_2",value:'level-3_2',queryString:"",children:[{label:"Sub level 3_2_1",value:'level-3_2_1',queryString:""}]},{label:"Sub level 3_3",value:'level-3_3',queryString:""}]}]
-
btnIdstring-ID of the specified button.-
appearancestring-The overlay style of the control. The optional values are:
corners:
pill
-
isSearchInputbooleanfalseWhether to enable the search input, it will have an clear button-
depthnumber1055Set the depth value of the control to control the display of the pop-up layer appear above. Please set it when multiple controls are used at the same time.-
valuestring-Set a default value for this control-
labelstring | ReactNode-It is used to specify a label for an element of a form.
Support html tags
-
namestring-Name is not deprecated when used with form fields.-
placeholderstring-Specifies a short hint that describes.-
autoCompletestringoffThe autocomplete attribute provides a hint to the user agent specifying how to, or indeed whether to, prefill a form control.-
autoCapitalizestringoffThe autocapitalize property of the HTMLElement interface represents the element's capitalization behavior for user input.-
spellCheckbooleanfalseThe spellcheck global attribute is an enumerated attribute that defines whether the element may be checked for spelling errors.-
unitsstring-Specify a unit identification string. Such as em, px, and so on.-
iconLeftReactNode-Set the left icon of this control-
iconRightReactNode-Set the right icon of this control-
maxLengthnumber-Defines the maximum string length that the user can enter into it.-
minLengthnumber-Defines the minimum string length that the user can enter into it.-
readOnlybooleanfalseWhen present, it specifies that this component field is read-only.-
disabledbooleanfalseWhether it is disabled-
requiredbooleanfalseWhen present, it specifies that a field must be filled out before submitting the form.-
noMatchPopupbooleantruePop-up window is not displayed when there is no matching content. If true, it is displayed by default.-
hideIconbooleanfalseForce hiding icons (including icon buttons)-
iconReactNode-Set the icon of search.
Valid when fetchTrigger is true.
-
winWidthnumber | functionautoSet the container width of options. Such as: 500px or () => window.innerWidth/2 + 'px'-
data
You could use key (opens in a new tab) instead of it
any-Incoming data, you can set the third parameter of onFetch.
Changes in the data value will cause the component to re-render. It will be used when the value or content does not change when switching routes and needs to re-render the component or get the request.

!!!Note: Using data and value at the same time may cause two different parameter transfers, which will affect the final rendering. Please choose the appropriate usage based on your business. Generally speaking, if the multiSelect exists, it is not recommended to use the data.
-
allowSpacingRetrivebooleanfalseAllow Spaces to return all results.
The condition is that the database interface can retrieve whitespace data. By default, using spaces will return no results.
-
autoShowOptionsbooleanfalseForce display of the option list.-
fetchTriggerbooleanfalseUse buttons to trigger data queries.-
fetchNoneInfostring-The text of the data not fetched.
Only takes effect when fetchTrigger is true.
-
fetchUpdatebooleanfalseWhen the property is true, every time the input changes or the search button is clicked, a data request will be triggered.-
fetchFuncAsyncConstructor-A method as a string from the constructor.-
fetchFuncMethodstring-When the property is true, every time the input changes or the search button is clicked, a data request will be triggered.
The methord must be a Promise Object.
-
fetchFuncMethodParamsarray-The parameter passed by the method, it is an array.
Note: the first element is a query string, the second element is the number of queried data (usually a number), and then you can increase the third, or fourth, and more parameters.
Such as ['',0], ['',99,'string 1','string 2'], ['',99,'string 1','$QUERY_STRING']
There should be at least one parameter which is the query string.
$QUERY_STRING identifies the ID of the automatic query, and its value depends on the user input string.
-
fetchCallbackfunction-Return value from fetchCallback property to format the data of the API callback, which will match the data structure of the component.
At the same time it returns the original data, you will use this function and use the return keyword to return a new value.
-
onFetchfunction-Call a function when data is successfully fetched. It returns one callback value which is the fetched data (Array)-
onClickfunction-Call a function when the value of an HTML element is changed. It returns two callback values.
  1. The first is the control (HTML Element)
  2. The last is the pop-up element(HTML Element)
-
onChangefunction-Call a function when the value of an HTML element is changed. It returns four callback values.
  1. The first is the control (HTML Element)
  2. The second is the fetched data(Array)
  3. The third is the selected data (JSON Object)
  4. The last is the pop-up element(HTML Element)
-
onBlurfunction-Call a function when a user leaves an form field. It returns two callback values.
  1. The first is the control (HTML Element)
  2. The last is the pop-up element(HTML Element)
-
onKeyboardInputfunction-Call a function when the keyboard is pressed. It returns three callback values.
  1. The first is the Control Event (KeyboardEvent)
  2. The second is the control (HTML Element)
  3. The last is the pop-up element(HTML Element)
-
onPressEnterfunction-The callback function that is triggered when Enter key is pressed. It returns two callback values.
  1. The first is the control (HTML Element)
  2. The last is the pop-up element(HTML Element)
-

It accepts all props which this control support. Such as style, data-*, tabIndex, id, and so on.


JSON Object Literals configuration properties of the options and callback from fetchCallback:

PropertyTypeDefaultDescriptionRequired
labelstring-Specify the label text for each option.
Support html tags.
listItemLabelstring-Specify the label text for pop-up list items.
Support html tags
-
valuestring-Specify the value for each option
queryStringstring-Quick query string, such as Chinese pinyin or English initials
disabledboolean-When present, it specifies that an option should be disabled.-

Create Callback

A successful response returns the details of the callback such as Sample Request Body:

Among them, label, listItemLabel, value, queryString and disabled are attributes used by the system, and other attributes can be added freely.