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

Feat(list): enable filtering programmatically #201

Conversation

fkhadra
Copy link

@fkhadra fkhadra commented Aug 5, 2022

Hey! This PR aims to address #85. The names for the method I've added are coming from #129.

  • set filter value programmatically
  • enable filtering programmatically. Apply filter if needed
Screen.Recording.2022-08-05.at.09.30.46.mov

Below is the code used in the video. I tried to keep it as simple as possible

package main

import (
	"github.com/charmbracelet/bubbles/list"
	tea "github.com/charmbracelet/bubbletea"
)

type item struct {
	title string
}

func (i item) Title() string       { return i.title }
func (i item) Description() string { return i.title }
func (i item) FilterValue() string { return i.title }

type Model struct {
	list list.Model
}

func New() Model {
	items := []list.Item{
		item{title: "red"},
		item{title: "blue"},
		item{title: "orange"},
		item{title: "purple"},
		item{title: "green"},
		item{title: "magenta"},
	}
	l := list.New(items, list.NewDefaultDelegate(), 100, 100)

	// uncomment to set default filter value
	// l.SetFilterValue("purple")

	return Model{
		list: l,
	}
}

func (m Model) Init() tea.Cmd {
	// used to enable filtering programmatically
	return list.EnableLiveFiltering
}

func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
	var (
		cmd  tea.Cmd
		cmds []tea.Cmd
	)

	switch msg := msg.(type) {
	case tea.WindowSizeMsg:
		m.list.SetSize(msg.Width, msg.Height)
	}

	m.list, cmd = m.list.Update(msg)
	cmds = append(cmds, cmd)

	return m, tea.Batch(cmds...)
}

func (m Model) View() string {
	return m.list.View()
}

I did not implement a way to disable the filter programmatically as I believe this can already be done using ResetFilter.

I've also tested my implementation with a custom input by setting l.SetShowFilter(false).

@fkhadra fkhadra changed the title Feat/enable filtering programmatically Feat(list): enable filtering programmatically Aug 5, 2022
@muesli muesli added the enhancement New feature or request label Oct 5, 2022
@fkhadra fkhadra force-pushed the feat/enable-filtering-programmatically branch from ae3d528 to ce90f6f Compare January 3, 2023 08:08
@omerxx
Copy link

omerxx commented Aug 25, 2023

Hi! This is awesome and I'm waiting for this anxiously! Currently thinking about using raw FZF but a bubble with default fuzzy search would be SO GOOD TO HAVE!

@fkhadra
Copy link
Author

fkhadra commented Sep 3, 2023

Hey @omerxx, it's quick handy idd, you can see it in action here https://github.com/fkhadra/caniuse. But I think my PR will be superseded by #201. Let's wait for the maintainers :)

reiki4040 added a commit to reiki4040/rnss that referenced this pull request May 5, 2024
bubbletea has not been support initial filter. using feature request PR.
charmbracelet/bubbles#201
@reiki4040
Copy link

This is excellent! I want to this feature too.

@bashbunni
Copy link
Member

@fkhadra is there anything you're looking to achieve with this PR that wasn't addressed with #335 ? If so, please let us know. It seems the linked issue for this one is already resolved.

Thanks!

@fkhadra
Copy link
Author

fkhadra commented Jan 15, 2025

Hey @bashbunni indeed you're right. I'll close my pr then. Thank you

@fkhadra fkhadra closed this Jan 15, 2025
@fkhadra fkhadra deleted the feat/enable-filtering-programmatically branch January 15, 2025 16:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants