Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Missing TypeScript types #26

Closed
williamdes opened this issue Aug 21, 2023 · 13 comments
Closed

Missing TypeScript types #26

williamdes opened this issue Aug 21, 2023 · 13 comments

Comments

@williamdes
Copy link

Hi 👋🏻
I found this library and it really seems interesting !
That said I use TypeScript with VueJS and it complains about the lack of .d.ts files.
Could you add a types file or would you be open to migrating to TypeScript (but still generating .js files) ?

@williamdes
Copy link
Author

I did some transformation of your annotations with vscode

 \* @property \{(.*)\} ([a-z]+) (.*)

to

$2: $1; // $3

Will continue posting as I advance on this

@lekoala
Copy link
Owner

lekoala commented Aug 21, 2023

i'm absolutely against typescript :-)

i haven't checked if there is a way to generate .d.ts because i have no need for them

@williamdes
Copy link
Author

williamdes commented Aug 21, 2023

Yay 😄 ..

type ItemCallback = (item: Object, inst: Autocomplete) => void;
type RenderCallback = (item: Object, label: string, inst: Autocomplete) => string;
type ServerCallback = (response: Response, inst: Autocomplete) => Promise

interface Config {
    showAllSuggestions?: Boolean; // Show all suggestions even if they don't match
    suggestionsThreshold?: Number; // Number of chars required to show suggestions
    maximumItems?: Number; // Maximum number of items to display
    autoselectFirst?: Boolean; // Always select the first item
    ignoreEnter?: Boolean; // Ignore enter if no items are selected (play nicely with autoselectFirst=0)
    updateOnSelect?: Boolean; // Update input value on selection (doesn't play nice with autoselectFirst)
    highlightTyped?: Boolean; // Highlight matched part of the label
    highlightClass?: String; // Class added to the mark label
    fullWidth?: Boolean; // Match the width on the input field
    fixed?: Boolean; // Use fixed positioning (solve overflow issues)
    fuzzy?: Boolean; // Fuzzy search
    startsWith?: Boolean; // Must start with the string. Defaults to false (it matches any position).
    preventBrowserAutocomplete?: Boolean; // Additional measures to prevent browser autocomplete
    itemClass?: String; // Applied to the 'li'. Accepts space separated classes.
    activeClasses?: Array; // By default: ["bg-primary", "text-white"]
    labelField?: String; // Key for the label
    valueField?: String; // Key for the value
    searchFields?: Array; // Key for the search
    queryParam?: String; // Key for the query parameter for server
    items?: Array | Object; // An array of label/value objects or an object with key/values
    source?: Function; // A function that provides the list of items
    hiddenInput?: Boolean; // Create an hidden input which stores the valueField
    hiddenValue?: String; // Populate the initial hidden value. Mostly useful with liveServer.
    datalist?: String; // The id of the source datalist
    server?: String; // Endpoint for data provider
    serverMethod?: String; // HTTP request method for data provider, default is GET
    serverParams?: String | Object; // Parameters to pass along to the server. You can specify a "related" key with the id of a related field.
    serverDataKey?: String; // By default: data
    fetchOptions?: Object; // Any other fetch options (https://developer.mozilla.org/en-US/docs/Web/API/fetch#syntax)
    liveServer?: Boolean; // Should the endpoint be called each time on input
    noCache?: Boolean; // Prevent caching by appending a timestamp
    debounceTime?: Number; // Debounce time for live server
    notFoundMessage?: String; // Display a no suggestions found message. Leave empty to disable
    onRenderItem?: RenderCallback; // Callback function that returns the label
    onSelectItem?: ItemCallback; // Callback function to call on selection
    onServerResponse?: ServerCallback; // Callback function to process server response. Must return a Promise
    onChange?: ItemCallback; // Callback function to call on change-event. Returns currently selected item if any
}

declare module 'bootstrap5-autocomplete' {
    export default class Autocomplete {
        /**
         * Attach to all elements matched by the selector
         */
        public static init(selector: string, config: Config);
    }
}

Okay, then you can include a file with the types. Here is what I managed to do.
And it works !

@lekoala
Copy link
Owner

lekoala commented Aug 21, 2023

it seems the proper way would be to generate that during the build process

https://www.typescriptlang.org/docs/handbook/declaration-files/dts-from-js.html
evanw/esbuild#95

@williamdes
Copy link
Author

Awesome 💯 , I can test the result for you if needed

@lekoala
Copy link
Owner

lekoala commented Aug 21, 2023

@williamdes have a look a2a7d87

@williamdes
Copy link
Author

Very cool, could you change all occurrences of config to Config and not Object ?
Also, the current Config object forces to define all the config keys.
See my example where I changed them all to <name>?: so they all can be missing since they all have default values

@williamdes
Copy link
Author

PS: the README should probably say import Autocomplete from "bootstrap5-autocomplete";
Maybe it does not apply to JS installations 🤔

@lekoala
Copy link
Owner

lekoala commented Aug 21, 2023

yes, the optional keys are kinda of annoying. the way i designed it, is that the full config object must have all the keys, but obviously you can also pass partial configs since they are going to be merged anyway.
And i don't really want to declare two config interface (one partial, one with mandatory fields) for the sake of it.
That's why i also allow regular objects

@williamdes
Copy link
Author

yes, the optional keys are kinda of annoying. the way i designed it, is that the full config object must have all the keys, but obviously you can also pass partial configs since they are going to be merged anyway. And i don't really want to declare two config interface (one partial, one with mandatory fields) for the sake of it. That's why i also allow regular objects

Only one could be enough I think, not sure if default values can be embed into an interface.
Why do you not want to make them all not required since it's the actual truth?

@lekoala
Copy link
Owner

lekoala commented Aug 21, 2023

@williamdes internally, they are all required by the class (values are expected to be set), but as an argument, you can pass anything

@lekoala
Copy link
Owner

lekoala commented Sep 5, 2023

closing this, if anyone has a good proposal to deal with partial config, i'm open to hear it, otherwise, as far as i'm concerned, i can live with how it's done :-)

@lekoala lekoala closed this as completed Sep 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants