datepicker

package module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 5, 2024 License: MIT Imports: 7 Imported by: 1

README

Bubble Datepicker

Overview

A custom interactive component for Bubbletea applications. This bubble is designed to offer datepicker functionality similar to the jQuery Datepicker widget from the web world. Easily select dates within your Bubbletea application!

Features

  • Interactive date selection.
  • Customizable appearance.
  • Support for keyboard navigation.
  • Easily integrates with Bubbletea applications.

Installation

To use this library in your Bubbletea project, you can add it as a dependency using go get:

go get github.com/ethanefung/bubble-datepicker

Usage

  1. Import the library in your Bubbletea application:

    import (
        tea "github.com/charmbracelet/bubbletea"
        "github.com/ethanefung/bubble-datepicker"
    )
    
  2. Create a new instance of the datepicker bubble:

    dp := datepicker.New(time.Now())
    dp.SelectDate() // datepicker date is not selected by default
    
  3. In your Model struct, include the datepicker as one of the fields:

    type Model struct {
        // Other fields in your model...
        DatePicker datepicker.Model
    }
    
  4. If not already done so, setup your application bubbletea.Model to satisfy the interface:

    func (m Model) Init() tea.Model {
        // Initialization logic here...
        return m
    }
    
    func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
        // Update logic here...
        return m, nil
    }
    
    func (m Model) View() string {
        // View logic here...
        return ""
    }
    
  5. In your View method, include the datepicker view:

    func (m Model) View() string {
        // Other view elements...
    
        // Include the datepicker component
        dpView := m.DatePicker.View()
        return fmt.Sprintf("%s\n%s", otherViews, dpView)
    }
    
  6. Finally, handle user interactions and updates in your Update method. Utilize the datepicker's .Update function for sane defaults and/or interact directly with the bubble using a plethora of the datepicker's Methods:

    func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
        // Handle datepicker events and updates
        dp, cmd := m.DatePicker.Update(msg)
        m.DatePicker = dp
    
        // or just modify the datepicker directly
        xmas := time.Date(2023, time.December, 25, 0, 0, 0, 0, time.UTC)
        m.DatePicker.SetTime(xmas)
    
        // Your other update logic here...
    
        return m, cmd
    }
    

You can customize the appearance and behavior of the datepicker component by modifying its settings and styles. For more detailed usage and customization options, refer to the library's documentation.

Examples

To see examples of how to use this datepicker component in a Bubbletea application, check the examples directory in the library's repository.

License

This library is licensed under the MIT License. See the LICENSE file for details.

Contributing

We welcome contributions! If you find a bug or have an enhancement in mind, please open an issue or create a pull request on the GitHub repository.

Support

If you have any questions or need assistance, please feel free to reach out to me.

Acknowledgments

This library was inspired by the jQuery Datepicker widget and is made possible thanks to the Bubbletea framework and the open-source community.

Happy coding with your new bubble datepicker! 📅🎉

Documentation

Overview

Package datepicker provides a bubble tea component for viewing and selecting a date from a monthly view.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Focus

type Focus int

Focus is a value passed to `model.SetFocus` to indicate what component controls should be available.

const (
	// FocusNone is a value passed to `model.SetFocus` to ignore all date altering key msgs
	FocusNone Focus = iota
	// FocusHeaderMonth is a value passed to `model.SetFocus` to accept key msgs that change the month
	FocusHeaderMonth
	// FocusHeaderYear is a value passed to `model.SetFocus` to accept key msgs that change the year
	FocusHeaderYear
	// FocusCalendar is a value passed to `model.SetFocus` to accept key msgs that change the week or date
	FocusCalendar
)

func (Focus) String

func (i Focus) String() string

type KeyMap

type KeyMap struct {
	Up        key.Binding
	Right     key.Binding
	Down      key.Binding
	Left      key.Binding
	FocusPrev key.Binding
	FocusNext key.Binding
	Quit      key.Binding
}

KeyMap is the key bindings for different actions within the datepicker.

func DefaultKeyMap

func DefaultKeyMap() KeyMap

DefaultKeyMap returns a KeyMap struct with default values

type Model

type Model struct {
	// Time is the `time.Time` struct that represents the selected date month and year
	Time time.Time

	// KeyMap encodes the keybindings recognized by the model
	KeyMap KeyMap

	// Styles represent the Styles struct used to render the datepicker
	Styles Styles

	// Focused indicates the component which the end user is focused on
	Focused Focus

	// Selected indicates whether a date is Selected in the datepicker
	Selected bool
}

Model is a struct that contains the state of the datepicker component and satisfies the `tea.Model` interface

func New

func New(time time.Time) Model

New returns the Model of the datepicker

func (*Model) Blur

func (m *Model) Blur()

Blur sets the datepicker focus to `FocusNone`

func (Model) Init

func (m Model) Init() tea.Cmd

Init satisfies the `tea.Model` interface. This sends a nil cmd

func (*Model) LastMonth

func (m *Model) LastMonth()

LastMonth sets the model's `Time` struct back 1 month

func (*Model) LastWeek

func (m *Model) LastWeek()

LastWeek sets the model's `Time` struct back 7 days

func (*Model) LastYear

func (m *Model) LastYear()

LastYear sets the model's `Time` struct back 1 year

func (*Model) NextMonth

func (m *Model) NextMonth()

NextMonth sets the model's `Time` struct forward 1 month

func (*Model) NextWeek

func (m *Model) NextWeek()

NextWeek sets the model's `Time` struct forward 7 days

func (*Model) NextYear

func (m *Model) NextYear()

NextYear sets the model's `Time` struct forward 1 year

func (*Model) SelectDate

func (m *Model) SelectDate()

SelectDate changes the model's Selected to true

func (*Model) SetFocus

func (m *Model) SetFocus(f Focus)

SetsFocus focuses one of the datepicker components. This can also be used to blur the datepicker by passing the Focus `FocusNone`.

func (*Model) SetTime

func (m *Model) SetTime(t time.Time)

SetTime sets the model's `Time` struct and is used as reference to the selected date

func (*Model) Tomorrow

func (m *Model) Tomorrow()

Tomorrow sets the model's `Time` struct forward 1 day

func (*Model) UnselectDate

func (m *Model) UnselectDate()

UnselectDate changes the model's Selected to false

func (Model) Update

func (m Model) Update(msg tea.Msg) (Model, tea.Cmd)

Update changes the state of the datepicker. Update satisfies the `tea.Model` interface

func (Model) View

func (m Model) View() string

View renders a month view as a multiline string in the bubbletea application. View satisfies the `tea.Model` interface.

func (*Model) Yesterday

func (m *Model) Yesterday()

Yesterday sets the model's `Time` struct back 1 day

type Styles

type Styles struct {
	Header lipgloss.Style
	Date   lipgloss.Style

	HeaderText   lipgloss.Style
	Text         lipgloss.Style
	SelectedText lipgloss.Style
	FocusedText  lipgloss.Style
}

Styles is a struct of lipgloss styles to apply to various elements of the datepicker

func DefaultStyles

func DefaultStyles() Styles

DefaultStyles returns a default `Styles` struct

Directories

Path Synopsis
examples
textinput
An interesting problem to solve in this example is acheiving two way data binding.
An interesting problem to solve in this example is acheiving two way data binding.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL