Skip to content

Commit ad55482

Browse files
Merge pull request #136 from linked-planet/dev
updated global state to use eval func
2 parents b477d78 + b358c3c commit ad55482

File tree

7 files changed

+823
-716
lines changed

7 files changed

+823
-716
lines changed

library/src/components/inputs/Select.tsx

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import {
2323
type AriaOnFilter,
2424
type AriaLiveMessages,
2525
} from "react-select"
26+
import ReactSelectAsync from "react-select/async"
2627

2728
// usage aria stuff:
2829
// https://react-select.com/advanced
@@ -312,6 +313,7 @@ type InnerProps<
312313
IsMulti extends boolean = boolean,
313314
> = PickedCreateableProps<ValueType, IsMulti> & {
314315
isCreateable?: boolean
316+
isAsync?: boolean
315317
testId?: string
316318
innerRef?: React.Ref<SelectInstance<
317319
OptionType<ValueType>,
@@ -328,6 +330,7 @@ type InnerProps<
328330

329331
const SelectInner = <ValueType, IsMulti extends boolean = boolean>({
330332
isCreateable,
333+
isAsync,
331334
formatCreateLabel,
332335
testId,
333336
innerRef,
@@ -525,6 +528,12 @@ const SelectInner = <ValueType, IsMulti extends boolean = boolean>({
525528
_components,
526529
])
527530

531+
if (isAsync && isCreateable) {
532+
throw new Error(
533+
"Select - isAsync and isCreateable cannot be true at the same time",
534+
)
535+
}
536+
528537
if (isCreateable) {
529538
return (
530539
<ReactSelectCreatable<
@@ -560,6 +569,31 @@ const SelectInner = <ValueType, IsMulti extends boolean = boolean>({
560569
)
561570
}
562571

572+
if (isAsync) {
573+
return (
574+
<ReactSelectAsync<
575+
OptionType<ValueType>,
576+
IsMulti,
577+
OptionGroupType<ValueType>
578+
>
579+
placeholder={
580+
(props.placeholder ?? locale.startsWith("de"))
581+
? "Auswahl..."
582+
: "Select..."
583+
}
584+
ref={locRef}
585+
unstyled
586+
inputId={inputId}
587+
classNames={classNamesConfig}
588+
data-testid={testId}
589+
data-invalid={invalid}
590+
styles={customStyles}
591+
components={components}
592+
{...props}
593+
/>
594+
)
595+
}
596+
563597
return (
564598
<RSelect<OptionType<ValueType>, IsMulti, OptionGroupType<ValueType>>
565599
placeholder={
@@ -613,6 +647,7 @@ type SelectPropsProtoOld<
613647
usePortal?: boolean
614648
disabled?: boolean
615649
isCreateable?: boolean
650+
isAsync?: boolean
616651
dropdownLabel?: (isOpen: boolean) => string
617652
clearValuesButtonLabel?: string
618653
removeValueButtonLabel?: string
@@ -764,6 +799,7 @@ function SelectInForm<
764799
const innerProps: InnerProps<ValueType, IsMulti> = {
765800
...props,
766801
isCreateable: props.isCreateable,
802+
isAsync: props.isAsync,
767803
testId,
768804
innerRef: ref,
769805
invalid,

library/src/utils/GlobalState.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const proxies: Record<string, { value: any }> = {}
77
export function useGlobalState<T>(
88
typeName: string,
99
initialValue: T,
10-
): readonly [T, (value: T) => void] {
10+
): readonly [T, (prevEval: (prev: T) => T) => void] {
1111
if (!proxies[typeName]) {
1212
proxies[typeName] = proxy<{ value: T }>({ value: initialValue })
1313
}
@@ -16,7 +16,8 @@ export function useGlobalState<T>(
1616
const snapshot = useSnapshot(store)
1717

1818
const setter = useCallback(
19-
(value: T) => {
19+
(prevEval: (prev: T) => T) => {
20+
const value = prevEval(store.value)
2021
store.value = value
2122
},
2223
[store],

0 commit comments

Comments
 (0)