@ackmann-dickenson/slim-pickens 中文文档教程

发布于 6年前 浏览 18 项目主页 更新于 3年前

npm

slim-pickens

React 的轻量级日期选择器实用程序。 使用我们的组件或轻松推出您自己的组件。 继续阅读。

Demo

http://outrageous-show.surge.sh/

Installation

npm

npm i --save @ackmann-dickenson/slim-pickens

yarn

yarn add @ackmann-dickenson/slim-pickens

Usage

我们公开了一些简单易用的日期选择器组件。 不喜欢他们? 我们公开方法 让您轻松编写自己的自定义组件。

Pre-made Components

我们目前有一个简单的日历组件以及一个日期输入,如下所述。

SlimPickens

这是一个常规的日历控件。

import React, { Component } from 'react';
import { SlimPickens } from '@ackmann-dickenson/slim-pickens';

export default class App extends Component {
  state = {
    selected: '4/20/2016'
  }

  select = selected => this.setState({ selected })

  render() {
    return (
      <SlimPickens onPick={this.select} value={this.state.selected} />
    );
  }
}

SlimPickens 组件接受以下道具:

Property NameTypeDescription
valueString | DateThe initially selected date. Can be a date object or a parsable date string.
onPickFunction(Date)Called with the chosen date when selected.

DateInput

点击时打开日历控件的输入。 选择一个日期关闭日历并显示 输入中选定的日期值。

import React, { Component } from 'react';
import { DateInput } from '@ackmann-dickenson/slim-pickens';

export default class App extends Component {
  state = {
    selected: '4/20/2016'
  }

  select = selected => this.setState({ selected })

  render() {
    return (
      <DateInput onPick={this.select} value={this.state.selected} />
    );
  }
}

DateInput 组件接受与 SlimPickens 相同的属性,以及可选的 format 属性。

Property NameTypeDescription
valueString | DateThe initially selected date. Can be a date object or a parsable date string.
onPickFunction(Date)Called with the chosen date when selected.
formatFunction(Date)Determines how to display the date in the input. Defaults to date => date.toLocaleString().

Custom Components

如果你不喜欢我们的,建立你自己的! 你可以使用装饰器或继承,拿走你的选择!

Decorators

只需使用 slimPickens 装饰器装饰它,然后 它将注入您需要的所有属性:

Property NameTypeDescription
monthIntegerCalendar month (1-12)
yearIntegerCalendar year (YYYY)
previousMonthFunctionDecrement the calendar month
nextMonthFunctionIncrement the calendar month
previousYearFunctionDecrement the calendar year
nextYearFunctionIncrement the calendar year
rowsArray<Columns>Row & column data containing date objects to populate the cells of the calendar
selectedDateThe currently selected date
export function MyCalendarComponent({ month, year, previousMonth, nextMonth, rows, selected }) {
  return (
    <table>
      ...
    </table>
  )
}

export default slimPickens(MyCalendarComponent)

事实上,SlimPickens 预制组件就是用这个装饰器编写的。 我们邀请您查看 灵感来源

Inheritance

您还可以继承自 SlimProto 组件。 这提供了与装饰器基本相同的界面, 有一些细微差别:

  1. month and year will be members of this.state
  2. previousMonth and nextMonth, previousYear, nextYear, rows, and selected will all be component methods

npm

slim-pickens

A lightweight date picker utility for React. Use our components or easily roll your own. Read on.

Demo

http://outrageous-show.surge.sh/

Installation

npm

npm i --save @ackmann-dickenson/slim-pickens

yarn

yarn add @ackmann-dickenson/slim-pickens

Usage

We expose some simple, usable datepicker components. Don't like 'em? We expose ways to let you easily write your own custom components.

Pre-made Components

We currently feature a simple calendar component as well as a date input, described below.

SlimPickens

This is a regular calendar control.

import React, { Component } from 'react';
import { SlimPickens } from '@ackmann-dickenson/slim-pickens';

export default class App extends Component {
  state = {
    selected: '4/20/2016'
  }

  select = selected => this.setState({ selected })

  render() {
    return (
      <SlimPickens onPick={this.select} value={this.state.selected} />
    );
  }
}

The SlimPickens component accepts the following props:

Property NameTypeDescription
valueString | DateThe initially selected date. Can be a date object or a parsable date string.
onPickFunction(Date)Called with the chosen date when selected.

DateInput

An input that opens a calendar control when clicked. Selecting a date closed the calendar and displays the selected date value in the input.

import React, { Component } from 'react';
import { DateInput } from '@ackmann-dickenson/slim-pickens';

export default class App extends Component {
  state = {
    selected: '4/20/2016'
  }

  select = selected => this.setState({ selected })

  render() {
    return (
      <DateInput onPick={this.select} value={this.state.selected} />
    );
  }
}

The DateInput component accepts the same props as SlimPickens, as well as an optional format prop.

Property NameTypeDescription
valueString | DateThe initially selected date. Can be a date object or a parsable date string.
onPickFunction(Date)Called with the chosen date when selected.
formatFunction(Date)Determines how to display the date in the input. Defaults to date => date.toLocaleString().

Custom Components

If you don't like ours, build your own! You can use decorators or inheritance, take your pickens!

Decorators

Just decorate it with the slimPickens decorator and it will inject all the properties you need:

Property NameTypeDescription
monthIntegerCalendar month (1-12)
yearIntegerCalendar year (YYYY)
previousMonthFunctionDecrement the calendar month
nextMonthFunctionIncrement the calendar month
previousYearFunctionDecrement the calendar year
nextYearFunctionIncrement the calendar year
rowsArray<Columns>Row & column data containing date objects to populate the cells of the calendar
selectedDateThe currently selected date
export function MyCalendarComponent({ month, year, previousMonth, nextMonth, rows, selected }) {
  return (
    <table>
      ...
    </table>
  )
}

export default slimPickens(MyCalendarComponent)

In fact, the SlimPickens premade component is written with this decorator. We invite you to look at the source for inspiration!

Inheritance

You can also inherit from the SlimProto component. This offers essentially the same interface as the decorator, with some minor differences:

  1. month and year will be members of this.state
  2. previousMonth and nextMonth, previousYear, nextYear, rows, and selected will all be component methods
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文