zavadil-react-common 2.0.1 → 2.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,12 @@
1
+ import { EntityBase } from "zavadil-ts-common";
2
+ export type EntityIdSelectProps<T extends EntityBase> = {
3
+ id?: number | null;
4
+ onChange: (n: number | null | undefined) => any;
5
+ options?: Array<T> | null;
6
+ showEmptyOption?: boolean;
7
+ disabled?: boolean;
8
+ emptyOptionLabel?: string;
9
+ sort?: boolean;
10
+ labelGetter: (item: T) => string;
11
+ };
12
+ export declare function EntityIdSelect<T extends EntityBase>({ id, sort, disabled, labelGetter, onChange, options, showEmptyOption, emptyOptionLabel }: EntityIdSelectProps<T>): import("react/jsx-runtime").JSX.Element;
@@ -0,0 +1,11 @@
1
+ import { EntityWithName } from "zavadil-ts-common";
2
+ export type EntityWithNameIdSelectProps<T extends EntityWithName> = {
3
+ id?: number | null;
4
+ onChange: (n: number | null | undefined) => any;
5
+ options?: Array<T> | null;
6
+ showEmptyOption?: boolean;
7
+ disabled?: boolean;
8
+ emptyOptionLabel?: string;
9
+ sort?: boolean;
10
+ };
11
+ export declare function EntityWithNameIdSelect<T extends EntityWithName>({ id, sort, disabled, onChange, options, showEmptyOption, emptyOptionLabel }: EntityWithNameIdSelectProps<T>): import("react/jsx-runtime").JSX.Element;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "zavadil-react-common",
3
- "version": "2.0.1",
3
+ "version": "2.0.3",
4
4
  "description": "Common types for React Typescript UI apps.",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.esm.js",
@@ -0,0 +1,49 @@
1
+ import {useMemo} from "react";
2
+ import {NumberSelect} from "./NumberSelect";
3
+ import {EntityBase} from "zavadil-ts-common";
4
+ import {GenericSelectOption} from "./StringSelect";
5
+
6
+ export type EntityIdSelectProps<T extends EntityBase> = {
7
+ id?: number | null;
8
+ onChange: (n: number | null | undefined) => any;
9
+ options?: Array<T> | null;
10
+ showEmptyOption?: boolean;
11
+ disabled?: boolean;
12
+ emptyOptionLabel?: string;
13
+ sort?: boolean;
14
+ labelGetter: (item: T) => string;
15
+ }
16
+
17
+ export function EntityIdSelect<T extends EntityBase>(
18
+ {id, sort, disabled, labelGetter, onChange, options, showEmptyOption, emptyOptionLabel}: EntityIdSelectProps<T>
19
+ ) {
20
+ const lOptions: GenericSelectOption<number>[] = useMemo(
21
+ () => {
22
+ let result: Array<T> = []
23
+ if (options) {
24
+ result = sort ? options.sort((a, b) => labelGetter(a) > labelGetter(b) ? 1 : -1) : options;
25
+ }
26
+ return result
27
+ .map(
28
+ (o) => {
29
+ return {
30
+ id: o.id,
31
+ label: labelGetter(o)
32
+ }
33
+ }
34
+ );
35
+ },
36
+ [options]
37
+ );
38
+
39
+ return (
40
+ <NumberSelect
41
+ disabled={disabled}
42
+ value={id}
43
+ options={lOptions}
44
+ onChange={onChange}
45
+ showEmptyOption={showEmptyOption}
46
+ emptyOptionLabel={emptyOptionLabel}
47
+ />
48
+ );
49
+ }
@@ -0,0 +1,29 @@
1
+ import {EntityWithName} from "zavadil-ts-common";
2
+ import {EntityIdSelect} from "./EntityIdSelect";
3
+
4
+ export type EntityWithNameIdSelectProps<T extends EntityWithName> = {
5
+ id?: number | null;
6
+ onChange: (n: number | null | undefined) => any;
7
+ options?: Array<T> | null;
8
+ showEmptyOption?: boolean;
9
+ disabled?: boolean;
10
+ emptyOptionLabel?: string;
11
+ sort?: boolean;
12
+ }
13
+
14
+ export function EntityWithNameIdSelect<T extends EntityWithName>(
15
+ {id, sort, disabled, onChange, options, showEmptyOption, emptyOptionLabel}: EntityWithNameIdSelectProps<T>
16
+ ) {
17
+ return (
18
+ <EntityIdSelect
19
+ id={id}
20
+ labelGetter={(e) => e.name}
21
+ disabled={disabled}
22
+ sort={sort}
23
+ options={options}
24
+ onChange={onChange}
25
+ showEmptyOption={showEmptyOption}
26
+ emptyOptionLabel={emptyOptionLabel}
27
+ />
28
+ );
29
+ }