@etsoo/react 1.5.74 → 1.5.77

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.
@@ -1,4 +1,3 @@
1
- import { IdLabelDto } from '@etsoo/appscript';
2
1
  import { DataTypes } from '@etsoo/shared';
3
2
  import { Autocomplete, AutocompleteRenderInputParams } from '@mui/material';
4
3
  import React from 'react';
@@ -11,14 +10,14 @@ import { SearchField } from './SearchField';
11
10
  /**
12
11
  * Tiplist props
13
12
  */
14
- export interface TiplistProps<T extends {}>
15
- extends Omit<AutocompleteExtendedProps<T>, 'open'> {
13
+ export interface TiplistProps<T extends {}, D extends DataTypes.Keys<T>>
14
+ extends Omit<AutocompleteExtendedProps<T, D>, 'open'> {
16
15
  /**
17
16
  * Load data callback
18
17
  */
19
18
  loadData: (
20
19
  keyword?: string,
21
- id?: DataTypes.IdType
20
+ id?: T[D]
22
21
  ) => PromiseLike<T[] | null | undefined>;
23
22
  }
24
23
 
@@ -35,11 +34,14 @@ interface States<T extends {}> {
35
34
  * @param props Props
36
35
  * @returns Component
37
36
  */
38
- export function Tiplist<T extends {} = IdLabelDto>(props: TiplistProps<T>) {
37
+ export function Tiplist<
38
+ T extends {} = DataTypes.IdLabelItem,
39
+ D extends DataTypes.Keys<T> = DataTypes.Keys<T>
40
+ >(props: TiplistProps<T, D>) {
39
41
  // Destruct
40
42
  const {
41
43
  search = false,
42
- idField = 'id',
44
+ idField = 'id' as D,
43
45
  idValue,
44
46
  inputAutoComplete = 'off',
45
47
  inputError,
@@ -69,7 +71,7 @@ export function Tiplist<T extends {} = IdLabelDto>(props: TiplistProps<T>) {
69
71
 
70
72
  // One time calculation for input's default value (uncontrolled)
71
73
  const localIdValue =
72
- idValue ?? (localValue && Reflect.get(localValue, idField));
74
+ idValue ?? DataTypes.getValue(localValue, idField ?? 'id');
73
75
 
74
76
  // Changable states
75
77
  const [states, stateUpdate] = React.useReducer(
@@ -86,7 +88,7 @@ export function Tiplist<T extends {} = IdLabelDto>(props: TiplistProps<T>) {
86
88
 
87
89
  // Input value
88
90
  const inputValue = React.useMemo(
89
- () => states.value && Reflect.get(states.value, idField),
91
+ () => states.value && states.value[idField],
90
92
  [states.value]
91
93
  );
92
94
 
@@ -130,7 +132,7 @@ export function Tiplist<T extends {} = IdLabelDto>(props: TiplistProps<T>) {
130
132
  };
131
133
 
132
134
  // Directly load data
133
- const loadDataDirect = (keyword?: string, id?: DataTypes.IdType) => {
135
+ const loadDataDirect = (keyword?: string, id?: T[D]) => {
134
136
  // Reset options
135
137
  // setOptions([]);
136
138
 
@@ -181,7 +183,7 @@ export function Tiplist<T extends {} = IdLabelDto>(props: TiplistProps<T>) {
181
183
  }
182
184
  };
183
185
 
184
- if (localIdValue != null && localIdValue !== '') {
186
+ if (localIdValue != null && (localIdValue as any) !== '') {
185
187
  if (state.idLoaded) {
186
188
  // Set default
187
189
  if (!state.idSet && states.options.length == 1) {
@@ -211,7 +213,7 @@ export function Tiplist<T extends {} = IdLabelDto>(props: TiplistProps<T>) {
211
213
  type="text"
212
214
  style={{ display: 'none' }}
213
215
  name={name}
214
- value={inputValue ?? ''}
216
+ value={`${inputValue ?? ''}`}
215
217
  readOnly
216
218
  onChange={inputOnChange}
217
219
  />
@@ -250,7 +252,7 @@ export function Tiplist<T extends {} = IdLabelDto>(props: TiplistProps<T>) {
250
252
  undefined,
251
253
  states.value == null
252
254
  ? undefined
253
- : Reflect.get(states.value, idField)
255
+ : states.value[idField]
254
256
  );
255
257
  }}
256
258
  onClose={() => {
@@ -291,7 +293,7 @@ export function Tiplist<T extends {} = IdLabelDto>(props: TiplistProps<T>) {
291
293
  />
292
294
  )
293
295
  }
294
- isOptionEqualToValue={(option: any, value: any) =>
296
+ isOptionEqualToValue={(option: T, value: T) =>
295
297
  option[idField] === value[idField]
296
298
  }
297
299
  {...rest}