Git Product home page Git Product logo

react-tick-slider's Introduction

hackday Archived Repository
The code of this repository was written during a Hack Day by a Marmelab developer. It's part of the distributed R&D effort at Marmelab, where each developer spends 2 days a month for learning and experimentation.
This code is not intended to be used in production, and is not maintained.

TickSlider

Primitives to build simple React tick slider components


The problem

You need an tick slider experience in your application and you want it to be accessible. You also want it to be simple and flexible to account for your use cases.

This solution

This is a component that controls user interactions and state for you so you can create tick slider components. It uses a render prop which gives you maximum flexibility with a minimal API because you are responsible for the rendering of everything and you simply apply props to what you're rendering.

This differs from other solutions which render things for their use case and then expose many options to allow for extensibility resulting in a bigger API that is less flexible as well as making the implementation more complicated and harder to contribute to.

Table of Contents

Installation

This module is distributed via npm which is bundled with node and should be installed as one of your project's dependencies:

npm install --save react-tick-slider

This package also depends on react and prop-types. Please make sure you have those installed as well.

Usage

Try it out in the browser

import React from 'react'
import TickSlider from 'react-tick-slider'

<TickSlider
    rootStyle={containerStyle}
    options={[{label: 'value 1',value: 1}, {label: 'value 2',value: 2}]} // your tick
    value={2} // default tick selected
    onValueChange={handleValueChange}
>
    {({ choices, selectedChoice, selectChoice }) => (
    <Fragment>
        <div className="slider">
            ...render your slider here
        </div>
        <CircleContainer>
            {choices.map(choice => (
                ...render your tick here
            ))}
            <div className="cricle">
                ...render your circle here
            </div>
        </CircleContainer>
    </Fragment>
    )}
</TickSlider>

is the only component. It doesn't render anything itself, it just calls the render function and renders that. "Use a render prop!"

Props

rootStyle

object | defaults to {}

This is an object with any inline styles you want applied to the root div.

options

array of object | defaults to []

This represent your tick. The object you are passed to generate your tick:

property type description
label string/number Tick label, you can use it in title or aria-label property
value number Tick value

value

number | defaults to null

This is the default tick selected.

onValueChange

function(value: number) | optional, no useful default

This return the value of the tick selected.

children

function

This is calls the render function and renders that.

Render Prop Function

actions

These are functions you can call to change the state of the downshift component.

property type description
selectChoice function(choice: object) selects the selectedChoice and trigger onValueChange event

state

These are values that represent the current state of the tick-slider component.

property type description
choices object {label: string/number, value: number, position: number} This is your ticks with multiple props
selectedChoice object {label: string/number, value: number, position: number} This is tick selected

Inspiration

I was heavily inspired by Kent C. Dodds and this repository Awesome react render props.

Other Solutions

You can implement these other solutions using tick-slider, but if you'd prefer to use these out of the box solutions, then that's fine too:

LICENSE

MIT

react-tick-slider's People

Contributors

fzaninotto avatar zyhou avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

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.