Search code examples
javascriptreactjstiptap

How to update value in dropdown based on text selection


I am working on a React-based Google Docs clone as a side project. I'm using TipTap as the base.

I wanted to try to mimic the functionality in Google Docs where the select field would update with the appropriate value based on whatever text the user selects (ex., "heading 1" would show up on the select field if a user highlights text with <h1></h1> styling).

Here's a small video of the functionality I want to mimic: https://i.gyazo.com/18cc57f0285c8c5cd80d5dd6051f0ee7.mp4

This is the code I have so far with the React Hook used for the select fields:

const [font, setFont] = useState('sans-serif')
const handleFontChange = (e) => {
   setFont(e.target.value)
   editor.chain().focus().setFontFamily(e.target.value).run()
}

return (
   <select value={font} onChange={handleFontChange}>
    {fontFamilies.map((item) => <option key={item.value} value={item.value}>{item.label}</option>)}
   </select>
) 

I've tried to wrap my head around React Hooks and the Selection API, but I'm stuck. Is this at all possible in Javascript/React?

EDIT:

I found a function from Tiptap's API that does exactly what I'm looking for. I guess now the problem is how do I update that value into the select? Here's my new code:

const font = [
     { value: 'sans-serif', label: 'Sans-serif' }, ...
   ]

const [selectedFont, setSelectedFont] = useState([])

const handleFontChange = (obj) => {
    setSelectedFont(obj)
    editor.chain().focus().setFontFamily(obj.value).run()
    console.log(editor.isActive('textStyle', {fontFamily: selectedFont.value})) // prints true if the user clicked on text with the property active, otherwise prints false
}

return (
    <Select
       name="fontFamily"
       options={font}
       isClearable="true"
       isSearchable="true"
       value={selectedFont}
       onChange={(option) => handleFontChange(option)}
    />
)

It feels like the solution is really simple but I'm just overthinking it.


Solution

  • So I finally figured out what to do. I needed to pass a function in value that iterated over the array:

    const fontList = [
        { label: 'Sans-serif', value: 'sans-serif' },
        ...
    ]
    
    <Select
        name="fontFamily"
        placeholder="Select font..."
        value={
          editor.isActive('textStyle', {fontFamily: font}) ? (
            selectedFont.find(index => fontList[index])
          ) : ''
        }
        options={fontList}
        onChange={({value: fontSel}) => {
          setFont(fontSel)
          editor.chain().focus().setFontFamily(fontSel).run()
        }}
      />
    

    It only took me a whole week of tearing my hair out... Glad to finally get this done.