4.3.2 • Published 1 month ago

realue v4.3.2

Weekly downloads
54
License
MIT
Repository
github
Last release
1 month ago

Realue

⚛️ Simple value management for React components.

Features

  • Enforces reusable components based on the { name, error, value, onChange(value, name), onChangeError(error, name) } properties, also called the "NEVO" pattern.
  • Provides helpers for effortlessly handling complex structured value types (arrays and objects, aka lists and maps) and their potential errors.
  • Considerably reduces boilerplate code by handling the application state directly in components, and shaping it through the component structure.
  • Brings efficient asynchronous handlers for sending data to persistence systems.

Installation

Install with the Node Package Manager:

npm install realue

Usage

Everything is exported from the main entry-point through an ES6 module.

Quick start

Use useObject to automatically handle an object property, or useArray for handling array items. Note how all the values are handled gracefully, without having to write boilerplate code.

import { useArray, useObject, useRemove, useDebounce } from "realue";
import type { NevoProps, ValueRemover } from "realue";

type UserType = {
  name?: string;
  level?: number;
};

function User(props: NevoProps<UserType> & { onRemove?: ValueRemover }) {
  const property = useObject(props);
  const onRemove = useRemove(props);
  return (
    <>
      <Input label="Name" {...property("name")} />
      <InputNumber label="Level" {...property("level")} />
      <button onClick={onRemove}>Delete user</button>
    </>
  );
}

function UserList(props: NevoProps<UserType[]>) {
  const item = useArray(props);
  return (
    <>
      {item.loop(User, { onRemove: item.remove })}
      <button onClick={item.add && (() => item.add({}))}>Add user</button>
    </>
  );
}

const USER_LIST: UserType[] = [
  { name: "Alice", level: 5 },
  { name: "Bob", level: -2 },
];

const USER_LIST_ERROR: ErrorReport<UserType[]> | undefined = undefined;

export default function App() {
  const props = useSyncedProps({
    name: "userList",
    error: USER_LIST_ERROR,
    value: USER_LIST,
  });
  return (
    <>
      <h1>Users</h1>
      <UserList {...props} />
    </>
  );
}
function extractInputValue<T extends string | undefined>({
  value,
}: HTMLInputElement) {
  return (value === "" ? undefined : value) as T;
}

function Input(
  props: NevoProps<string | undefined> & { label: string; delay?: number },
) {
  const { value = "", name, onChange } = useDebounce(props, delay);
  const onInput = useInput(props, extractInputValue);
  return (
    <div class="flex flex-col space-y-1">
      <label>{label}</label>
      {error && <p class="text-red-500 dark:text-red-300">{error.join(" ")}</p>}
      <input
        autoComplete="new-password"
        disabled={!onChange}
        name={name}
        onInput={onChange ? onInput : undefined}
        placeholder={placeholder}
        value={value}
      />
    </div>
  );
}

function extractInputNumberValue({ value }: HTMLInputElement) {
  const parsedValue = parseFloat(value);
  return isNaN(parsedValue) ? undefined : parsedValue;
}

export const InputNumber = memo(function InputNumber(props: InputNumberProps) {
  const {
    value: currentValue,
    name,
    onChange,
    label,
    placeholder,
    onValidate,
    error,
    onChangeError,
  } = props;
  const value =
    currentValue === undefined
      ? undefined
      : isNaN(currentValue)
      ? 0
      : currentValue;
  const onInput = useInput(props, extractInputNumberValue);
  return (
    <div class="flex flex-col space-y-1">
      <label>{label}</label>
      {error && <p class="text-red-500 dark:text-red-300">{error.join(" ")}</p>}
      <input
        autoComplete="off"
        disabled={!onChange}
        name={name}
        onInput={onChange ? onInput : undefined}
        placeholder={placeholder}
        type="number"
        value={value === undefined ? "" : value}
      />
    </div>
  );
});

Error handling

Changing the USER_LIST_ERROR in the example above to the following value, will automatically add a an error message for Bob's level value. These errors can be either passed down to components through the error prop, but also communicated back up through the onChangeError property.

const USER_LIST_ERROR: ErrorReport<UserType[]> | undefined = {
  1: { level: ["The level must be a positive number."] },
};

The error value follows a simple structure to map error messages to specific values:

  • An error report for a primitive value (e.g., a boolean, number, or string value) consists of a list of one or several strings (e.g., ["This is not an email address."]).
  • Error reports for objects consist of a mapping of property names to error reports (e.g. { name: ["The name is missing."] }). In case of an error that pertains to the entire value, it can either be stored at the empty string key in case that there are also errors for specific properties (e.g., { "": ["Either the username or the email address must be set."], "level": ["The level must be positive."] }), or directly as a list of strings: ["Either the username or the email address must be set."].
  • Error reports for arrays are similar to those for objects, except that they map item indices to error reports (e.g., { 1: { "name": ["The name is required and cannot be empty."] } }). As well as in error reports for objects, an error report for the entire array could be set at the empty string key if there are errors for the array items, or directly as a list of strings.

Property name conventions

Components handle a value and render it. That value is received through the value property.

It also gets an optional error report that can be provided through the error property.

Any callback name starts with the "on" prefix: onChange is used to report a value change, onChangeError serves to update the error.

Documentation

Checkout the API documentation.

Demo

A demo application can be run in the browser with:

npm run build
npm start
open http://localhost:1234

You can then inspect and edit the code in the demo/ folder.

4.3.2

1 month ago

4.3.1

1 month ago

4.3.0

1 month ago

4.2.0

1 month ago

4.1.1

1 month ago

4.1.0

1 month ago

4.0.5

1 month ago

4.0.4

1 month ago

4.0.3

1 month ago

4.0.2

1 month ago

4.0.1

2 months ago

4.0.0

2 months ago

4.0.0-alpha.42

2 months ago

4.0.0-alpha.41

2 months ago

4.0.0-alpha.40

2 months ago

4.0.0-alpha.39

2 months ago

4.0.0-alpha.38

2 months ago

4.0.0-alpha.37

2 months ago

4.0.0-alpha.36

2 months ago

4.0.0-alpha.35

2 months ago

4.0.0-alpha.34

5 months ago

4.0.0-alpha.33

5 months ago

4.0.0-alpha.31

5 months ago

4.0.0-alpha.30

5 months ago

4.0.0-alpha.32

5 months ago

4.0.0-alpha.29

5 months ago

4.0.0-alpha.17

6 months ago

4.0.0-alpha.16

7 months ago

4.0.0-alpha.15

7 months ago

4.0.0-alpha.14

7 months ago

4.0.0-alpha.19

6 months ago

4.0.0-alpha.18

6 months ago

4.0.0-alpha.13

7 months ago

4.0.0-alpha.12

7 months ago

4.0.0-alpha.28

5 months ago

4.0.0-alpha.27

5 months ago

4.0.0-alpha.26

6 months ago

4.0.0-alpha.25

6 months ago

4.0.0-alpha.20

6 months ago

4.0.0-alpha.24

6 months ago

4.0.0-alpha.23

6 months ago

4.0.0-alpha.22

6 months ago

4.0.0-alpha.21

6 months ago

4.0.0-alpha.9

7 months ago

4.0.0-alpha.11

7 months ago

4.0.0-alpha.10

7 months ago

4.0.0-alpha.7

8 months ago

4.0.0-alpha.8

8 months ago

4.0.0-alpha.5

8 months ago

4.0.0-alpha.3

9 months ago

4.0.0-alpha.4

9 months ago

3.6.5

2 years ago

3.6.4

2 years ago

3.6.3

2 years ago

3.6.2

2 years ago

3.6.1

3 years ago

3.6.0

4 years ago

3.5.0

4 years ago

3.4.0

4 years ago

3.3.1

4 years ago

3.3.0

4 years ago

3.2.2

4 years ago

3.2.1

4 years ago

3.2.0

4 years ago

3.1.3

4 years ago

3.1.4

4 years ago

3.1.2

4 years ago

3.1.1

4 years ago

3.1.0

4 years ago

3.0.1

4 years ago

3.0.0

4 years ago

3.0.0-beta.35

4 years ago

3.0.0-beta.34

4 years ago

3.0.0-beta.33

4 years ago

3.0.0-beta.32

4 years ago

3.0.0-beta.31

4 years ago

3.0.0-beta.30

4 years ago

3.0.0-beta.29

4 years ago

3.0.0-beta.28

4 years ago

3.0.0-beta.27

4 years ago

3.0.0-beta.26

4 years ago

3.0.0-beta.25

4 years ago

3.0.0-beta.24

4 years ago

3.0.0-beta.23

4 years ago

3.0.0-beta.22

4 years ago

3.0.0-beta.21

4 years ago

3.0.0-beta.20

4 years ago

3.0.0-beta.19

4 years ago

3.0.0-beta.18

4 years ago

3.0.0-beta.17

5 years ago

3.0.0-beta.16

5 years ago

3.0.0-beta.15

5 years ago

3.0.0-beta.14

5 years ago

3.0.0-beta.13

5 years ago

3.0.0-beta.12

5 years ago

3.0.0-beta.11

5 years ago

3.0.0-beta.10

5 years ago

3.0.0-beta.9

5 years ago

3.0.0-beta.8

5 years ago

3.0.0-beta.7

5 years ago

3.0.0-beta.6

5 years ago

3.0.0-beta.5

5 years ago

3.0.0-beta.4

5 years ago

3.0.0-beta.3

5 years ago

2.22.1

5 years ago

3.0.0-beta.2

5 years ago

3.0.0-beta.1

5 years ago

3.0.0-beta.0

5 years ago

3.0.0-next.0

5 years ago

2.22.0

5 years ago

2.21.0

5 years ago

2.20.2

5 years ago

2.20.1

5 years ago

2.20.0

5 years ago

2.19.1

5 years ago

2.19.0

5 years ago

2.18.1

5 years ago

2.18.0

5 years ago

2.17.3

5 years ago

2.17.2

5 years ago

2.17.1

5 years ago

2.17.0

5 years ago

2.16.1

5 years ago

2.16.0

5 years ago

2.15.1

5 years ago

2.15.0

5 years ago

2.14.2

5 years ago

2.14.1

5 years ago

2.14.0

5 years ago

2.13.5

5 years ago

2.13.4

5 years ago

2.13.3

5 years ago

2.13.2

5 years ago

2.13.1

5 years ago

2.13.0

5 years ago

2.12.1

5 years ago

2.12.0

5 years ago

2.11.5

5 years ago

2.11.4

5 years ago

2.11.3

5 years ago

2.11.2

5 years ago

2.11.1

5 years ago

2.11.0

5 years ago

2.10.0

5 years ago

2.9.1

5 years ago

2.9.0

5 years ago

2.8.0

5 years ago

2.7.1

5 years ago

2.7.0

5 years ago

2.6.0

5 years ago

2.5.0

5 years ago

2.4.0

5 years ago

2.3.1

5 years ago

2.3.0

5 years ago

2.2.0

5 years ago

2.1.1

5 years ago

2.1.0

5 years ago

2.0.5

5 years ago

2.0.4

5 years ago

2.0.3

5 years ago

2.0.2

5 years ago

2.0.1

5 years ago

2.0.0

5 years ago

1.10.0

5 years ago

1.9.0

5 years ago

1.8.0

5 years ago

1.7.0

5 years ago

1.6.2

5 years ago

1.6.1

5 years ago

1.6.0

5 years ago

1.5.0

5 years ago

1.4.0

5 years ago

1.3.2

5 years ago

1.3.1

5 years ago

1.3.0

5 years ago

1.2.0

5 years ago

1.1.4

5 years ago

1.1.3

5 years ago

1.1.2

5 years ago

1.1.1

5 years ago

1.1.0

5 years ago

1.0.0

5 years ago

0.10.5

5 years ago

0.10.4

5 years ago

0.10.3

5 years ago

0.10.2

5 years ago

0.10.1

5 years ago

0.10.0

5 years ago

0.9.2

5 years ago

0.9.1

6 years ago

0.9.0

6 years ago

0.8.0

6 years ago

0.7.0

6 years ago

0.6.1

6 years ago

0.6.0

6 years ago

0.5.0

6 years ago

0.4.1

6 years ago

0.4.0

6 years ago

0.3.1

6 years ago

0.3.0

6 years ago

0.2.2

6 years ago

0.2.1

6 years ago

0.2.0

6 years ago

0.1.0

6 years ago

0.0.1

6 years ago