GithubHelp home page GithubHelp logo

spacejack / mithril-select Goto Github PK

View Code? Open in Web Editor NEW
13.0 3.0 0.0 206 KB

Custom Select Component for Mithril.js

License: MIT License

CSS 6.99% HTML 1.96% TypeScript 55.45% JavaScript 35.60%
mithril component javascript typescript select dropdown

mithril-select's Introduction

Mithril Custom Select Component

A custom select widget component for Mithril.js. Built to mimic native browser select behaviour (if not looks) as closely as possible, including acessibility and keyboard features. The minimal CSS included can be easily overridden and customized by your own styles.

Try a live demo here.

Install:

npm install mithril-select

(TypeScript types are included.)

You will need to include the css file for some basic working styles.

Using PostCSS with postcss-import allows you to import the stylesheet from node_modules:

@import "mithril-select";

If you're using a sass compiler, you can add:

@import "node_modules/mithril-select/index";

to one of your sass files.

Otherwise you can copy the node_modules/mithril-select/index.css file to your project and add it to your html page.

See the example in the git repository for examples of style customization.

Example use:

import mithrilSelect from "mithril-select"
// var mithrilSelect = require("mithril-select").default

// Data to be used in select
const colours = [
  {id: 'red', text: 'Red'},
  {id: 'blue', text: 'Blue'},
  {id: 'green', text: 'Green'},
  {id: 'yellow', text: 'Yellow'},
  {id: 'orange', text: 'Orange'},
  {id: 'pink', text: 'Pink'}
]

let colour = ""

const component = {
  view() {
    return m(mithrilSelect, {
      options: [
        {value: null, view: 'Select a colour...'}
      ].concat(
        colours.map(c => ({value: c.id, view: c.text}))
      ),
      // A CSS class to add to the root element of the select
      class: 'my-select',
      // Respond to selection changes
      onchange: (val) => {
        colour = val != null
          ? colours.find(c => c.id === val).text
          : ""
      }
    })
  }
}

All options and component Attrs:

(See src/index.ts)

/** Represents a single option in a select */
interface Option {
  /**
   * Unique value that identifies this option.
   * Can be any type except `undefined`.
   */
  value: any
  /**
   * Either a string to display for the option or a callback
   * that renders vnodes.
   */
  view?: string | (() => m.Children)
}

/** Attrs object for Select component */
interface Attrs {
  /**
   * Array of `Option` objects
   */
  options: Option[]
  /**
   * Optional prompt to display until the user selects an option.
   * Supply either a string to display or a callback that renders vnodes.
   */
  promptView?: string | (() => m.Children)
  /**
   * Optional value to use for element id attribute.
   */
  id?: string
  /**
   * Optional name of hidden input for form. If none supplied, no hidden
   * input will be rendered. Hidden input value will be coerced to string.
   */
  name?: string
  /**
   * Current selected option value. Omitting or setting to `undefined`
   * is the same as supplying no value. (`null` can be a value.)
   */
  value?: any
	/**
   * Optional aria-labelledby attribute
   */
	ariaLabelledby?: string
  /**
   * Value of option that will be selected on creation. Overridden
   * by `value` if supplied, otherwise will be first option.
   */
  initialValue?: any
  /**
   * Additional class string to use on containing element.
   */
  class?: string
  /**
   * Callback that will be passed the value of the selected option
   * when selection changes.
   */
  onchange?(value: any): void
}

Development Install:

First git clone this repo. Then:

npm install

Build module

npm run build

Serve, compile & watch example app:

npm start

Then go to http://localhost:3000/ in your browser.

Build a plain ES2015 version of the library:

npm run build-es2015

Will output src/index.js


Thanks to barneycarroll for providing an initial POC demo using global focus/blur listeners.

mithril-select's People

Contributors

spacejack avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

mithril-select's Issues

Scroll jank on popup

On the demo page , if you click "Mandarin" and then press the keyboard down arrow multiple times the popup's vertical scroll bar position will shift up or down.

scroll_issue

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.