TypeScript icon indicating copy to clipboard operation
TypeScript copied to clipboard

Suggestions for jsx attributes should include =""

Open yunti opened this issue 5 years ago • 6 comments

TS Template added by @mjbvz

TypeScript Version: 3.9.4

Search Terms

  • suggest, suggestion, completions
  • snippet
  • react, jsx

In vscode the autocomplete for className just adds className in webstorm it adds className="" is there a way in vscode to get it to add the additional =""?

I'm not sure currently where the autocomplete comes from, whether it is inbuilt into vscode or comes from the React plugin I use (although it doesn't appear to be in their snippets).

https://github.com/dsznajder/vscode-es7-javascript-react-snippets

yunti avatar May 20 '20 08:05 yunti

Potentially related to #25207

If there is enough interest in this, we should also consider what happens when a user wants to write: className={expr ? 'a': 'b'}

mjbvz avatar Jun 02 '20 02:06 mjbvz

@mjbvz webstorm/pycharm etc... has an option for completion on jsx attributes to be braces, quotes, none or based on type. Based on type defaults to className using quotes and the others using \{}. Which works pretty well. (pic of the jsx completion options below) image

yunti avatar Jun 02 '20 09:06 yunti

IMO the options should be

  • None
  • Braces
  • Quotes for strings, braces otherwise

(wording negotiable). An option for quotes always is nonsense. What to do for union types that include strings and non-strings, or for any and unknown, is debatable. Inserting braces seems fine; keeping existing behavior and not inserting the = seems fine; inserting just the = with no braces or quotes seems fine.

andrewbranch avatar Aug 26 '21 18:08 andrewbranch

Based on type makes sense to me as, it can also be used to avoid adding the suggestion to boolean attributes. For the union types scenario, unless is union with undefined, I think everything should be on braces.

Options:

  • None
  • Braces
  • Based on type

armanio123 avatar Sep 15 '21 01:09 armanio123

Just wanted to leave a big thank you for adding this, super helpful 😄

merlinaudio avatar Oct 08 '21 14:10 merlinaudio

If I have it set to braces, it will auto complete a given attribute with braces once. If I ctrl+z and try completing it again, it just completes the attribute name without braces.

shayded-exe avatar Aug 10 '22 00:08 shayded-exe

I can’t reproduce that behavior:

https://user-images.githubusercontent.com/3277153/184702590-2c8e37e7-7857-4e14-8812-29aebe874737.mp4

Feel free to file a new issue with more repro info. This one should have been closed as completed.

andrewbranch avatar Aug 15 '22 19:08 andrewbranch

It would be nice to auto add braces / quotes when = symbol pressed (like in webstorm).

This is VSCode:

https://user-images.githubusercontent.com/392644/203531303-31bac65f-58e4-4cab-a945-f5bf34be4c2d.mov

And this is WebStorm:

https://user-images.githubusercontent.com/392644/203531493-29b7fb6b-d144-4f9b-b5f0-652720db98e2.mov

Strate avatar Nov 23 '22 11:11 Strate

@Strate I think that would require a new VS Code API; there’s nothing that I know of where an editor asks us to auto-insert code upon insertion of a previous character. Feel free to leave your suggestion on the VS Code repo (be sure to search for duplicates first).

andrewbranch avatar Nov 23 '22 21:11 andrewbranch

@andrewbranch isn't auto-closing jsx tag almost same feature?

https://user-images.githubusercontent.com/392644/203761394-bbc74c6f-f190-4419-a7ca-e7b01fe5c023.mov

Strate avatar Nov 24 '22 10:11 Strate

That feature exists purely on the VS Code side; they don’t ask TypeScript for any info to make that happen.

andrewbranch avatar Nov 28 '22 18:11 andrewbranch