@accessible/slider 中文文档教程

发布于 4年前 浏览 22 项目主页 更新于 3年前


<Slider>

捆绑恐惧症 Types 代码覆盖率 构建状态 NPM 版本 MIT 许可证

npm i @accessible/slider

React 的可访问且多功能的滑块组件。 这些组件是可触摸的, 键盘可导航。

该库包含开箱即用的单拇指滑块组件,尽管您可以可行地设计一个 来自此处的双拇指滑块。

Quick Start

查看 CodeSandbox 上的示例

```jsx 和谐 import {Slider, Track, Thumb}

从 '@accessible/slider' const Component = () => ( <滑块方向="垂直">

)

## API

### `<Slider>`

Creates the context for your slider and configures it. This also creates the underlying
`<input type='range'>` component. Any props not listed below are provided to the `<input>`.

#### Props

| Prop         | Type                                         | Default        | Required? | Description                                                                                                                                                                              |
| ------------ | -------------------------------------------- | -------------- | --------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| min          | `number`                                     | `0`            | Yes       | The minimum value of the slider                                                                                                                                                          |
| max          | `number`                                     | `100`          | Yes       | The maximum value of the slider                                                                                                                                                          |
| step         | `number`                                     | `1`            | Yes       | The amount the slider increases or decreases each time it is ticked                                                                                                                      |
| value        | `number`                                     | `undefined`    | No        | Makes this component controlled. This is always the value of the slider. `incr`, `decr`, `set` and keyboard steps have no effect.                                                        |
| defaultValue | `number`                                     | `50`           | No        | Sets the default value of the slider                                                                                                                                                     |
| orientation  | <code>"horizontal" &#0124; "vertical"</code> | `"horizontal"` | Yes       | Sets the orientation of the slider. When `horizontal` the minimum value is on the left, max on the right. When `vertical`, the minimum value is on the bottom and the max is on the top. |
| disabled     | `boolean`                                    | `false`        | No        | Disables the slider `incr`, `decr`, `set`, and keyboard controls. You can still update the slider's value using the `value` prop.                                                        |
| onChange     | `(value?: number) => any`                    | `undefined`    | No        | Called each time the `value` changes.                                                                                                                                                    |

### `<Track>`

This component attaches event handlers to its child that make it act like a native `<input type="range">` track. It
is your responsibility to style it.

#### Props

| Prop     | Type                 | Default     | Required? | Description                                                                                                                       |
| -------- | -------------------- | ----------- | --------- | --------------------------------------------------------------------------------------------------------------------------------- |
| children | `React.ReactElement` | `undefined` | Yes       | Provides events to its child component that control the state of the slider's value depending on the current drag/click position. |

### `<Thumb>`

This component adds styles to its child that update the child's position within a track based upon the
progress of the slider i.e. `(value - min) / (max - min)`.

#### Props

| Prop     | Type                 | Default     | Required? | Description                                             |
| -------- | -------------------- | ----------- | --------- | ------------------------------------------------------- |
| children | `React.ReactElement` | `undefined` | Yes       | The child component you want to add progress styles to. |

### `useSlider()`

A hook that provides the slider's [`SliderContext`](#slidercontextvalue)

### `SliderContextValue`

打字稿jsx 接口 SliderContextValue { 增加:(按?:数字)=> 空白 decr: (by?: number) => 空白 设置:(值:数字)=> 空白 值:数字 最小值:数字 最大:数字 步骤:数字 重点:布尔值 禁用:布尔值 方向:'水平' | '垂直的' inputRef: React.MutableRefObject} ```

useValue()

一个返回滑块当前值

useProgress()

的钩子 value 一个返回 (value - min) / (max - min)

useOrientation()

的钩子返回滑块的当前方向

useFocused()

如果滑块聚焦

useDisabled()

则返回true的钩子如果滑块禁用<则返回true的钩子/code>

useControls()

提供滑块的 incrdecrset 函数的挂钩

LICENSE

MIT


<Slider>

Bundlephobia Types Code coverage Build status NPM Version MIT License

npm i @accessible/slider

An accessible and versatile slider component for React. These components are touchable and keyboard navigable.

This library contains components for single-thumb sliders out of the box, though you could feasibly fashion a dual-thumb slider from what's here.

Quick Start

Check out the example on CodeSandbox

```jsx harmony import {Slider, Track, Thumb} from '@accessible/slider'

const Component = () => (

)

## API

### `<Slider>`

Creates the context for your slider and configures it. This also creates the underlying
`<input type='range'>` component. Any props not listed below are provided to the `<input>`.

#### Props

| Prop         | Type                                         | Default        | Required? | Description                                                                                                                                                                              |
| ------------ | -------------------------------------------- | -------------- | --------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| min          | `number`                                     | `0`            | Yes       | The minimum value of the slider                                                                                                                                                          |
| max          | `number`                                     | `100`          | Yes       | The maximum value of the slider                                                                                                                                                          |
| step         | `number`                                     | `1`            | Yes       | The amount the slider increases or decreases each time it is ticked                                                                                                                      |
| value        | `number`                                     | `undefined`    | No        | Makes this component controlled. This is always the value of the slider. `incr`, `decr`, `set` and keyboard steps have no effect.                                                        |
| defaultValue | `number`                                     | `50`           | No        | Sets the default value of the slider                                                                                                                                                     |
| orientation  | <code>"horizontal" &#0124; "vertical"</code> | `"horizontal"` | Yes       | Sets the orientation of the slider. When `horizontal` the minimum value is on the left, max on the right. When `vertical`, the minimum value is on the bottom and the max is on the top. |
| disabled     | `boolean`                                    | `false`        | No        | Disables the slider `incr`, `decr`, `set`, and keyboard controls. You can still update the slider's value using the `value` prop.                                                        |
| onChange     | `(value?: number) => any`                    | `undefined`    | No        | Called each time the `value` changes.                                                                                                                                                    |

### `<Track>`

This component attaches event handlers to its child that make it act like a native `<input type="range">` track. It
is your responsibility to style it.

#### Props

| Prop     | Type                 | Default     | Required? | Description                                                                                                                       |
| -------- | -------------------- | ----------- | --------- | --------------------------------------------------------------------------------------------------------------------------------- |
| children | `React.ReactElement` | `undefined` | Yes       | Provides events to its child component that control the state of the slider's value depending on the current drag/click position. |

### `<Thumb>`

This component adds styles to its child that update the child's position within a track based upon the
progress of the slider i.e. `(value - min) / (max - min)`.

#### Props

| Prop     | Type                 | Default     | Required? | Description                                             |
| -------- | -------------------- | ----------- | --------- | ------------------------------------------------------- |
| children | `React.ReactElement` | `undefined` | Yes       | The child component you want to add progress styles to. |

### `useSlider()`

A hook that provides the slider's [`SliderContext`](#slidercontextvalue)

### `SliderContextValue`

typescript jsx interface SliderContextValue { incr: (by?: number) => void decr: (by?: number) => void set: (value: number) => void value: number min: number max: number step: number focused: boolean disabled: boolean orientation: 'horizontal' | 'vertical' inputRef: React.MutableRefObject} ```

useValue()

A hook that returns the slider's current value

useProgress()

A hook that returns the value of (value - min) / (max - min)

useOrientation()

A hook that returns the slider's current orientation

useFocused()

A hook that returns true if the slider is focused

useDisabled()

A hook that returns true if the slider is disabled

useControls()

A hook that provide's the slider's incr, decr, and set functions

LICENSE

MIT

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文