From f18cfe9233031cefc4087b53bcdeaac4c58c4c07 Mon Sep 17 00:00:00 2001 From: Federico Ruggi <1081051+ruggi@users.noreply.github.com> Date: Wed, 9 Oct 2024 12:45:27 +0200 Subject: [PATCH 1/2] use auto/unset for the default value --- .../src/components/inspector/flex-section.tsx | 6 ++-- editor/src/uuiui/radix-components.tsx | 34 ++++++++++++++++--- 2 files changed, 33 insertions(+), 7 deletions(-) diff --git a/editor/src/components/inspector/flex-section.tsx b/editor/src/components/inspector/flex-section.tsx index 6eed0980a59a..6d019d643517 100644 --- a/editor/src/components/inspector/flex-section.tsx +++ b/editor/src/components/inspector/flex-section.tsx @@ -967,8 +967,8 @@ function selectOption(value: GridAutoFlow) { } const unsetSelectOption = regularRadixSelectOption({ - label: 'unset', - value: 'unset', + label: (isOpen) => (isOpen ? 'unset' : 'auto'), + value: 'auto', placeholder: true, }) @@ -1026,7 +1026,7 @@ const AutoFlowControl = React.memo(() => { selectededViewsRef.current.map((path) => applyCommandsAction([ updateBulkProperties('always', path, [ - value === 'unset' + value === 'auto' ? propertyToDelete(PP.create('style', 'gridAutoFlow')) : propertyToSet(PP.create('style', 'gridAutoFlow'), value), ]), diff --git a/editor/src/uuiui/radix-components.tsx b/editor/src/uuiui/radix-components.tsx index ad8787c65c6d..d0c36e2d009d 100644 --- a/editor/src/uuiui/radix-components.tsx +++ b/editor/src/uuiui/radix-components.tsx @@ -228,7 +228,7 @@ Separator.displayName = 'Separator' type RegularRadixSelectOption = { type: 'REGULAR' value: string - label: string + label: string | ((isOpen: boolean) => string) icon?: IcnProps placeholder?: boolean } @@ -254,6 +254,19 @@ export function separatorRadixSelectOption(): Separator { export type RadixSelectOption = RegularRadixSelectOption | Separator +function optionLabelToString( + option: RegularRadixSelectOption | null, + isOpen: boolean, +): string | null { + if (option == null) { + return null + } + + const label = typeof option.label === 'string' ? option.label : option.label(isOpen) + + return `${label.charAt(0).toUpperCase()}${label.slice(1)}` +} + export const RadixSelect = React.memo( (props: { id: string @@ -269,11 +282,24 @@ export const RadixSelect = React.memo( e.stopPropagation() }, []) + const { onOpenChange: propsOnOpenChange } = props + + const [isOpen, setIsOpen] = React.useState(false) + const onOpenChange = React.useCallback( + (open: boolean) => { + setIsOpen(open) + propsOnOpenChange?.(open) + }, + [propsOnOpenChange], + ) + + const valueLabel = optionLabelToString(props.value ?? null, isOpen) + return ( - + @@ -339,7 +365,7 @@ export const RadixSelect = React.memo( ) } - const label = `${option.label.charAt(0).toUpperCase()}${option.label.slice(1)}` + const label = optionLabelToString(option, isOpen) return ( Date: Wed, 9 Oct 2024 12:53:11 +0200 Subject: [PATCH 2/2] tweak --- editor/src/components/inspector/flex-section.tsx | 2 +- editor/src/uuiui/radix-components.tsx | 9 +++++---- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/editor/src/components/inspector/flex-section.tsx b/editor/src/components/inspector/flex-section.tsx index 6d019d643517..8afca94bc45f 100644 --- a/editor/src/components/inspector/flex-section.tsx +++ b/editor/src/components/inspector/flex-section.tsx @@ -967,7 +967,7 @@ function selectOption(value: GridAutoFlow) { } const unsetSelectOption = regularRadixSelectOption({ - label: (isOpen) => (isOpen ? 'unset' : 'auto'), + label: (isOpen, currentValue) => (isOpen && currentValue !== 'auto' ? 'unset' : 'auto'), value: 'auto', placeholder: true, }) diff --git a/editor/src/uuiui/radix-components.tsx b/editor/src/uuiui/radix-components.tsx index d0c36e2d009d..86ed787124af 100644 --- a/editor/src/uuiui/radix-components.tsx +++ b/editor/src/uuiui/radix-components.tsx @@ -228,7 +228,7 @@ Separator.displayName = 'Separator' type RegularRadixSelectOption = { type: 'REGULAR' value: string - label: string | ((isOpen: boolean) => string) + label: string | ((isOpen: boolean, currentValue: string | null) => string) icon?: IcnProps placeholder?: boolean } @@ -257,12 +257,13 @@ export type RadixSelectOption = RegularRadixSelectOption | Separator function optionLabelToString( option: RegularRadixSelectOption | null, isOpen: boolean, + currentValue: string | null, ): string | null { if (option == null) { return null } - const label = typeof option.label === 'string' ? option.label : option.label(isOpen) + const label = typeof option.label === 'string' ? option.label : option.label(isOpen, currentValue) return `${label.charAt(0).toUpperCase()}${label.slice(1)}` } @@ -293,7 +294,7 @@ export const RadixSelect = React.memo( [propsOnOpenChange], ) - const valueLabel = optionLabelToString(props.value ?? null, isOpen) + const valueLabel = optionLabelToString(props.value ?? null, isOpen, props.value?.value ?? null) return (