-
Notifications
You must be signed in to change notification settings - Fork 290
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
235 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
125 changes: 125 additions & 0 deletions
125
src/hooks/useDebounceEvent/__stories__/useDebounceEvent.stories.mdx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
import { useState } from 'react'; | ||
import { Story, Canvas, Meta, ArgsTable } from "@storybook/addon-docs"; | ||
import useDebounceEvent from ".."; | ||
import "../../__stories__/general-hooks-stories.scss"; | ||
|
||
<Meta title="Hooks/useDebounceEvent" /> | ||
|
||
# useDebounceEvent | ||
|
||
- [Overview](#overview) | ||
- [Arguments](#arguments) | ||
- [Returns](#returns) | ||
- [Usage](#usage) | ||
- [Use cases and examples](#use-cases-and-examples) | ||
- [Feedback](#feedback) | ||
|
||
## Overview | ||
|
||
This hook generates an easy to use debounced value updater. | ||
|
||
<Canvas> | ||
<Story name="Overview"> | ||
{() => { | ||
const { inputValue, onEventChanged } = useDebounceEvent({ delay: 100 }); | ||
return ( | ||
<input type="text" value={inputValue} onChange={onEventChanged}/> | ||
); | ||
}} | ||
</Story> | ||
</Canvas> | ||
|
||
## Arguments | ||
|
||
<FunctionArguments> | ||
<FunctionArgument name="options" type="Object"> | ||
<FunctionArgument name="trim" type="boolean" description="Whether to trim the value or not." default="false" /> | ||
<FunctionArgument | ||
name="onChange" | ||
type="(value: string) => void" | ||
description="Callback function to execute on changes." | ||
default="() => null" | ||
/> | ||
<FunctionArgument | ||
name="delay" | ||
type="number" | ||
description="The amount of time (in ms) to delay the value's update." | ||
default="0" | ||
/> | ||
<FunctionArgument name="initialStateValue" type="any" description="The initial value." /> | ||
</FunctionArgument> | ||
</FunctionArguments> | ||
|
||
## Returns | ||
|
||
<FunctionArguments> | ||
<FunctionArgument name="result" type="Object"> | ||
<FunctionArgument name="inputValue" type="any" description="The hook's value." /> | ||
<FunctionArgument | ||
name="onEventChanged" | ||
type="(event: Event) => void" | ||
description={<>A wrapper around the passed <code>onChange</code> function.</>} | ||
/> | ||
<FunctionArgument | ||
name="clearValue" | ||
type="() => void" | ||
description="Clears the current value." | ||
/> | ||
<FunctionArgument | ||
name="updateValue" | ||
type="(value: any) => void" | ||
description="Updates the current value." | ||
/> | ||
</FunctionArgument> | ||
</FunctionArguments> | ||
|
||
## Usage | ||
|
||
<UsageGuidelines | ||
guidelines={["Use this hook when you need to debounce value updates (for example, text inputs)."]} | ||
/> | ||
|
||
## Use cases and examples | ||
|
||
### Passing an initial value | ||
|
||
<Canvas> | ||
<Story name="Passing an initial value"> | ||
{() => { | ||
const { inputValue, onEventChanged } = useDebounceEvent({ initialStateValue: 'bla bla bla' }); | ||
return ( | ||
<input type="text" value={inputValue} onChange={onEventChanged}/> | ||
); | ||
}} | ||
</Story> | ||
</Canvas> | ||
|
||
### Passing an `onChange` handler | ||
|
||
<Canvas> | ||
<Story name="Passing an `onChange` handler"> | ||
{() => { | ||
const [length, setLength] = useState(0); | ||
const { inputValue, onEventChanged } = useDebounceEvent({ onChange: (value) => setLength(value.length) }); | ||
return ( | ||
<> | ||
<input type="text" value={inputValue} onChange={onEventChanged}/> | ||
{ <span>Input has {length} characters</span> } | ||
</> | ||
); | ||
}} | ||
</Story> | ||
</Canvas> | ||
|
||
### Trimming the value | ||
|
||
<Canvas> | ||
<Story name="With trim"> | ||
{() => { | ||
const { inputValue, onEventChanged } = useDebounceEvent({ trim: true }); | ||
return ( | ||
<input type="text" value={inputValue} onChange={onEventChanged}/> | ||
); | ||
}} | ||
</Story> | ||
</Canvas> |
3 changes: 2 additions & 1 deletion
3
src/hooks/useDebounceEvent.js → src/hooks/useDebounceEvent/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# FunctionArguments | ||
|
||
A list that documents a function's signature. | ||
|
||
## Guidelines | ||
|
||
- There is no unique syntax for creating a UsageGuideline component in MDX files. Therefore, use the component as you would in a regular JSX file. | ||
|
||
## Props | ||
|
||
| Prop | Description | | ||
| ---------- | ------------------------------------------------ | | ||
| `children` | `<li>`s that represent the function's arguments. | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import React from "react"; | ||
import PropTypes from "prop-types"; | ||
import { Code } from "@storybook/components"; // eslint-disable-line import/no-extraneous-dependencies | ||
import classes from "./index.module.scss"; | ||
|
||
export const FunctionArgument = ({ children, name, type, description, default: defaultValue }) => ( | ||
<li className={classes.argument}> | ||
<code className={classes["argument-name"]}>{name}</code> | ||
<code className={classes["argument-type"]}>{type}</code> | ||
{description && <> - {description}</>} | ||
{defaultValue && ( | ||
<> | ||
{" "} | ||
Defaults to: <Code>{defaultValue}</Code> | ||
</> | ||
)} | ||
{children && <ul>{children}</ul>} | ||
</li> | ||
); | ||
|
||
export const FunctionArguments = ({ children }) => { | ||
return <ul>{children}</ul>; | ||
}; | ||
|
||
FunctionArguments.propTypes = { | ||
children: PropTypes.oneOfType([FunctionArgument, PropTypes.arrayOf(FunctionArgument)]) | ||
}; | ||
|
||
FunctionArguments.defaultProps = { | ||
children: [] | ||
}; |
41 changes: 41 additions & 0 deletions
41
src/storybook/components/function-arguments/index.module.scss
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
@import "../../../styles/global-css-settings.scss"; | ||
@import "../../styles/content-spacing"; | ||
@import "../../styles/typography"; | ||
|
||
.argument { | ||
ul { | ||
padding: 0; | ||
} | ||
|
||
.argument { | ||
margin-left: 20px; | ||
} | ||
} | ||
|
||
.argument-name, | ||
.argument-type, | ||
.default-value { | ||
font-family: monaco, monospace; | ||
font-size: 85%; | ||
} | ||
|
||
.argument-name { | ||
margin-right: 5px; | ||
} | ||
|
||
.argument-type { | ||
color: var(--color-purple); | ||
|
||
&:before, | ||
&:after { | ||
color: var(--color-winter); | ||
} | ||
|
||
&:before { | ||
content: "<"; | ||
} | ||
|
||
&:after { | ||
content: ">"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters