1.5.0 • Published 1 year ago

@stein197/react-ui v1.5.0

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

React UI

A tiny package that contains a few UI components written in and for React. Demo can be found here

Installation

npm install @stein197/react-ui

Then add index.css to your stylesheet:

@import "@stein197/react-ui/index";

Components

Dropdown

Simple dropdown component. Works the same as plain <select /> element except that this dropdown can be stylized. The dropdown offers a search functionality by which items can be filtered out. Throws an error if defaultValue is not contained in data.

import React from "react";
import Dropdown from "@stein197/react-ui/Dropdown";

const data = [
	"First",
	"Second",
	"Third"
];
<Dropdown defaultValue="First" placeholder="Select a place" data={data} onChange={console.log} editable={false} />

Options

NameTypeDescription
datastring[]List of items for dropdown to show
name?stringName of the inner input
enabled?booleanEnable or disable dropdown input. true by default
required?booleanMake the input required. false by default
editable?booleanMake input editable in order to allow searching among items. true by default
placeholder?stringText to be shown as a placeholder for the input
defaultValue?stringDefault value. Should be the one of the values included in data
className?stringCSS class
onChange(value: string, state: "valid" \| "invalid" \| "match" \| "empty") => voidFires each time input changes it's value (typing or selecting values from dropdown)

Async

Renders its children only when the promise is resolved.

import React from "react";
import Async from "@stein197/react-ui/Async";

<Async promise={new Promise(resolve => setTimeout(resolve, 1000))} fallback={reason => <p>Rejected! Reason: {reason}</p>} stub={<p>Pending...</p>}>
	{result => (
		<p>Resolved! Result: {result}</p>
	)}
</Async>

Options

NameTypeDescription
promisePromise<T>Promise to wait for resolving
children((result: T) => React.ReactNode) \| React.ReactNodeContent to render after the promise is resolved
fallback?((reason: U) => React.ReactNode) \| React.ReactNodeContent to render in case of rejected promise
stub?React.ReactNodeContent to render while promise is pending

Switch, Case, Default

Component for implementing switch-case-default clause but in JSX. The usage is the same as the usage of ordinary switch statement.

import React from "react";
import {Switch, Case, Default} from "@stein197/react-ui/Switch";

<Switch value="2">
	<Case value="1">First</Case>
	<Case value="2">Second</Case>
	<Case value="3">Third</Case>
	<Default>None</Default>
</Switch>

Options

NameTypeDescription
valueanyValue to test

If, Then, Else

Component for conditional rendering, instead of using JS operators

import React from "react";
import {If, Then, Else} from "@stein197/react-ui/If";

<If value={false}>
	<Then>yes</Then>
	<Else>no</Else>
</If>

Options

NameTypeDescription
valuebooleanValue to test

Foreach

Replacement for items.map(...) construction

<Foreach items={["First", "Second", "Third"]}>
	{(item, index) => (
		<p key={index}>item: {item}, index: {index}</p>
	)}
</Foreach>

Options

NameTypeDescription
dataT[]Data to walk through
children(item: T, index: number) => React.ReactNodeCallback to be called per every item

For

For-loop as a react component

<For from="1" to="3">
	{i => (
		<p key={i}>index: {i}</p>
	)}
</For>

Options

NameTypeDescription
fromnumber | \${number}``From which to start counting. 0 by default
tonumber | \${number}``A number until which to proceed counting including the number itself
children(index: number) => React.ReactNodeCallback to be called per every loop

Spinner

Animated circle spinner component

<Spinner r="50" />

options

NameTypeDescription
rnumberRadius of the circle
strokeWidthnumberWidth of the circle's stroke
strokeColorstringColor of the circle's stroke
bgStrokeColorstringColor of the background ring stroke
roundLinecapbooleanMakes linecaps round for circle
lengthnumberLength of the segment. Accepts values from 0 to 1
durationnumberDuration of one loop of animation in seconds
clockwisebooleanDirection in which to spin the circle
classNamestringAdditional CSS classname

Hooks

useAsync

useToggle

usePrev

useCounter

useHover

useResize

useFocus

useOnline

useImage

NOTE: More detailed documentation can be found in TSDoc blocks in source code.

NPM scripts

  • clean clean the directory out of compiled files
  • build compile source code
  • test run unit tests

Demo

Info to build and run demo can be found here

1.5.0

1 year ago

1.4.0

1 year ago

1.3.0

1 year ago

1.2.0

1 year ago

1.1.0

1 year ago

1.0.0

2 years ago