@inseefr/lunatic 3.8.1-rc.0 → 3.8.1-rc.1

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.
@@ -15,6 +15,8 @@ export function Suggester(props: LunaticComponentProps<'Suggester'>) {
15
15
  return <WrappedSuggester {...props} key={suggesterKey} />;
16
16
  }
17
17
 
18
+ const ARBITRARY_ID = 'OTHER';
19
+
18
20
  export function WrappedSuggester({
19
21
  storeName,
20
22
  id,
@@ -44,7 +46,7 @@ export function WrappedSuggester({
44
46
  // so we can break the rule of hooks here
45
47
  const computeSelectedOptions = (): [SuggesterOptionType] | [] => {
46
48
  if (arbitraryValue) {
47
- return [{ id: 'OTHER', label: arbitraryValue, value: 'OTHER' }];
49
+ return [{ id: ARBITRARY_ID, label: arbitraryValue, value: ARBITRARY_ID }];
48
50
  }
49
51
  if (!value) {
50
52
  return [];
@@ -154,14 +156,15 @@ export function WrappedSuggester({
154
156
  useEffect(() => {
155
157
  // Fix display issue (when handleChanges is called outside this component (in management mode, return to FORCED value by example)
156
158
  // "value" does'nt match selectedOption's "id"
157
- if (value && selectedOptions[0]?.id !== value) {
159
+ if (selectedOptions[0]?.id !== value) {
158
160
  const actualSelection = computeSelectedOptions();
159
- const selectedOptionsWithLabel = [
160
- {
161
- ...actualSelection[0],
162
- label: getLabelById(actualSelection[0]?.id),
163
- },
164
- ] as [SuggesterOptionType];
161
+ const selectedOptionsWithLabel = actualSelection.map((selection) => {
162
+ if (selection.id === ARBITRARY_ID) return selection;
163
+ return {
164
+ ...selection,
165
+ label: getLabelById(selection.id),
166
+ };
167
+ }) as [SuggesterOptionType];
165
168
  setSelectedOptions(selectedOptionsWithLabel);
166
169
  }
167
170
  // eslint-disable-next-line react-hooks/exhaustive-deps
@@ -51,7 +51,7 @@ export function useStore({ storeName }: { storeName: string }) {
51
51
  setStoreState: setState,
52
52
  getLabelById: (id: any) => {
53
53
  if (!id) return '';
54
- return store.search?.getFieldsById(id).label ?? '';
54
+ return store.search?.getFieldsById(id)?.label ?? '';
55
55
  },
56
56
  };
57
57
  }