5.1.0 • Published 11 months ago

typesafe-react v5.1.0

Weekly downloads
-
License
MIT
Repository
github
Last release
11 months ago

Framework agnostic functions, hooks and components.

Functions

when

Exhaustive switch statement with return value.

// handle all cases
const result1 = when(AorB, { A: () => 'a', B: () => 'b' });

// provide a fallback
const result2 = when(ABC, { A: () => 'a', B: () => 'b' }, () => 'abc');

// run some code
when(AorB, { A: () => console.log('a'), B: () => console.log('b') });

// even simpler
console.log(when(AorB, { A: () => 'a', B: () => 'b' }));
ParameterTypeDescription
expressionEExpression to evaluate.
casesRecord<CE, () => R>Record that maps some or all possible values of expression to a branch.
fallback(() => F) \| neverDefault branch. Mandatory (and only allowed) if cases does not cover all possible values of expression.
Generic TypeExtendsDescription
Estring \| numberexpression's type. Union of strings or numbers.
CEEUnion of covered cases. Subunion of E or same as E.
const RunknownUnion of return values.
const FunknownFallback value.

switchTrue

Switch true statement with return value.

const result = switchTrue(
  [
    [() => isA(), () => 'a'],
    [() => isB(), () => 'b'],
  ],
  () => 'c'
);
ParameterTypeDescription
casesReadonlyArray<C>Array of tuples. Each tuple has a callback that returns a condition and a callback that will run if the condition is truthy.
fallback(() => F) \| undefinedDefault branch.
Generic TypeExtendsDescription
const C[() => unknown, () => unknown]Tuple of condition and result callbacks.
const FunknownFallback value.

Components

When

Exhaustive switch component.

// handle all cases
const result1 = <When expression={AorB} cases={{ A: () => <A />, B: () => <B /> }} />;

// provide a fallback
const result2 = (
  <When expression={ABC} cases={{ A: () => <A />, B: () => <B /> }} fallback={() => <Abc />} />
);
PropTypeDescription
expressionEExpression to evaluate.
casesRecord<CE, () => ReactNode>Record that maps some or all possible values of expression to a branch that returns content.
fallback(() => ReactNode) \| neverDefault branch that returns content. Mandatory (and only allowed) if cases does not cover all possible values of expression.
Generic TypeExtendsDescription
Estring \| numberexpression's type. Union of strings or numbers.
CEEUnion of covered cases. Subunion of E or same as E.

SwitchTrue

Switch true component.

const result = (
  <SwitchTrue
    cases={[
      [() => isA(), () => <A />],
      [() => isB(), () => <B />],
    ]}
    fallback={() => <Fallback />}
  />
);
PropTypeDescription
casesReadonlyArray<[() => unknown, () => ReactNode]>Array of tuples. Each tuple has a callback that returns a condition and a callback that will run if the condition is truthy and return content.
fallback(() => ReactNode) \| undefinedDefault branch that returns content.

Show

Component to conditionally render content.

// simple check
const result1 = (
  <Show when={show} fallback={`I am visible if 'show' is falsy.`}>
    I am visible if 'show' is truthy.
  </Show>
);

// multiple conditions
const result1 = <Show when={[show, !hidden]}>I am visible if all conditions are truthy.</Show>;

// use when's value
const result2 = <Show when={nullableArray}>{(data) => data.length}</Show>;

// use whenAll's values
const result2 = (
  <Show when={[nullableArray, nullableObject]} fallback={'Something went wrong!'}>
    {([array, object]) => array.length + object.numberProperty}
  </Show>
);
PropTypeDescription
whenWSingle condition to evaluate.
whenAllTMultiple conditions to evaluate.
fallbackReactNodeContent to render if when is falsy or whenAll contains some falsy elements.
childrenReactNode \| ((data: NoInfer<IsEqual<W, never> extends true ? NonNullableElements<T> : NonNullable<W>>) => ReactNode)Children callback that receives when or whenAll and returns content to render if when is truthy or whenAll only contains truthy elements.
Generic TypeExtendsDescription
Wunknownwhen's type. Single condition.
const TReadonlyArray<unknown>whenAll's type. Tuple of conditions.

For

Component for rendering lists.

const result = (
  <For each={array} keyFn={(item) => item.id} fallback={`The array is empty.`}>
    {(item, index) => `#${index} ${item.value}`}
  </For>
);
PropTypeDescription
eachReadonlyArray<I>Array to iterate over.
keyFn(item: NoInfer<I>, index: number) => KeyFunction that returns the key for each item.
children(item: NoInfer<I>, index: number) => ReactNodeFunction that returns the content for each item. No need to define key here.
fallbackReactNodeContent to render if each is empty.
Generic TypeExtendsDescription
INonNullable<unknown>each's elements' type.
5.0.6

11 months ago

5.0.5

12 months ago

5.0.4

12 months ago

5.0.3

12 months ago

5.0.2

12 months ago

5.1.0

11 months ago

5.0.1

12 months ago

5.0.0

12 months ago

2.1.0

1 year ago

2.0.1

1 year ago

2.0.0

1 year ago

3.1.0

1 year ago

3.0.0

1 year ago

4.0.1

12 months ago

4.0.0

12 months ago

1.0.1

1 year ago

1.0.0

1 year ago

0.5.2

1 year ago

0.5.0

1 year ago

0.4.1

1 year ago

0.4.3

1 year ago

0.5.1

1 year ago

0.4.2

1 year ago

0.4.0

1 year ago

0.3.0

1 year ago

0.2.1

1 year ago

0.2.0

1 year ago

0.3.1

1 year ago

0.2.2

1 year ago

0.1.0

1 year ago

0.0.1

1 year ago