diff --git a/.storybook/preview.js b/.storybook/preview.js index 04f296d0b1..5417c6e3ae 100644 --- a/.storybook/preview.js +++ b/.storybook/preview.js @@ -9,6 +9,8 @@ import { ComponentRules, ComponentName, UsageGuidelines, + FunctionArguments, + FunctionArgument, RelatedComponents, DocFooter, MultipleStoryElementsWrapper, @@ -37,6 +39,8 @@ addParameters({ Tip, ComponentRules, UsageGuidelines, + FunctionArguments, + FunctionArgument, RelatedComponents } }, diff --git a/src/components/TextField/__tests__/__snapshots__/textField-snapshot-tests.jest.js.snap b/src/components/TextField/__tests__/__snapshots__/textField-snapshot-tests.jest.js.snap index 850bb24387..a13123cbee 100644 --- a/src/components/TextField/__tests__/__snapshots__/textField-snapshot-tests.jest.js.snap +++ b/src/components/TextField/__tests__/__snapshots__/textField-snapshot-tests.jest.js.snap @@ -31,6 +31,7 @@ exports[`TextField renders correctly when disabled 1`] = ` required={false} role="" type="text" + value="" />
+ +# 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. + + + + {() => { + const { inputValue, onEventChanged } = useDebounceEvent({ delay: 100 }); + return ( + + ); + }} + + + +## Arguments + + + + + + + + + + +## Returns + + + + + A wrapper around the passed onChange function.} + /> + + + + + +## Usage + + + +## Use cases and examples + +### Passing an initial value + + + + {() => { + const { inputValue, onEventChanged } = useDebounceEvent({ initialStateValue: 'bla bla bla' }); + return ( + + ); + }} + + + +### Passing an `onChange` handler + + + + {() => { + const [length, setLength] = useState(0); + const { inputValue, onEventChanged } = useDebounceEvent({ onChange: (value) => setLength(value.length) }); + return ( + <> + + { Input has {length} characters } + + ); + }} + + + +### Trimming the value + + + + {() => { + const { inputValue, onEventChanged } = useDebounceEvent({ trim: true }); + return ( + + ); + }} + + \ No newline at end of file diff --git a/src/hooks/useDebounceEvent.js b/src/hooks/useDebounceEvent/index.js similarity index 88% rename from src/hooks/useDebounceEvent.js rename to src/hooks/useDebounceEvent/index.js index d8352f4452..eb755c8b75 100644 --- a/src/hooks/useDebounceEvent.js +++ b/src/hooks/useDebounceEvent/index.js @@ -1,7 +1,8 @@ import { useMemo, useCallback, useState, useRef, useEffect } from "react"; import debounce from "lodash/debounce"; +import noop from "lodash/noop"; -export default function useDebounceEvent({ delay = 0, onChange, initialStateValue, trim }) { +export default function useDebounceEvent({ delay = 0, onChange = noop, initialStateValue = "", trim }) { const [inputValue, setValue] = useState(initialStateValue); const previousValue = useRef(null); diff --git a/src/storybook/components/function-arguments/README.md b/src/storybook/components/function-arguments/README.md new file mode 100644 index 0000000000..0e94ddd8ce --- /dev/null +++ b/src/storybook/components/function-arguments/README.md @@ -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` | `
  • `s that represent the function's arguments. | diff --git a/src/storybook/components/function-arguments/index.jsx b/src/storybook/components/function-arguments/index.jsx new file mode 100644 index 0000000000..f459596886 --- /dev/null +++ b/src/storybook/components/function-arguments/index.jsx @@ -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 }) => ( +
  • + {name} + {type} + {description && <> - {description}} + {defaultValue && ( + <> + {" "} + Defaults to: {defaultValue} + + )} + {children &&
      {children}
    } +
  • +); + +export const FunctionArguments = ({ children }) => { + return
      {children}
    ; +}; + +FunctionArguments.propTypes = { + children: PropTypes.oneOfType([FunctionArgument, PropTypes.arrayOf(FunctionArgument)]) +}; + +FunctionArguments.defaultProps = { + children: [] +}; diff --git a/src/storybook/components/function-arguments/index.module.scss b/src/storybook/components/function-arguments/index.module.scss new file mode 100644 index 0000000000..527a2e9937 --- /dev/null +++ b/src/storybook/components/function-arguments/index.module.scss @@ -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: ">"; + } +} diff --git a/src/storybook/components/index.jsx b/src/storybook/components/index.jsx index c42d8eca94..f1c78eff5f 100644 --- a/src/storybook/components/index.jsx +++ b/src/storybook/components/index.jsx @@ -14,6 +14,7 @@ import { Frame } from "./frame/frame"; import { VisualDescription } from "./visual-description/visual-description"; import { ColorsDescription } from "./colors-description/colors-description"; +export * from "./function-arguments"; export { SectionName, Title,