Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use auto as value for Auto Flow dropdown #6504

Merged
merged 3 commits into from
Oct 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions editor/src/components/inspector/flex-section.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -967,8 +967,8 @@ function selectOption(value: GridAutoFlow) {
}

const unsetSelectOption = regularRadixSelectOption({
label: 'unset',
value: 'unset',
label: (isOpen, currentValue) => (isOpen && currentValue !== 'auto' ? 'unset' : 'auto'),
value: 'auto',
placeholder: true,
})

Expand Down Expand Up @@ -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),
]),
Expand Down
35 changes: 31 additions & 4 deletions editor/src/uuiui/radix-components.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ Separator.displayName = 'Separator'
type RegularRadixSelectOption = {
type: 'REGULAR'
value: string
label: string
label: string | ((isOpen: boolean, currentValue: string | null) => string)
icon?: IcnProps
placeholder?: boolean
}
Expand All @@ -254,6 +254,20 @@ export function separatorRadixSelectOption(): Separator {

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, currentValue)

return `${label.charAt(0).toUpperCase()}${label.slice(1)}`
}

export const RadixSelect = React.memo(
(props: {
id: string
Expand All @@ -269,11 +283,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, props.value?.value ?? null)

return (
<Select.Root
value={props.value?.value}
onValueChange={props.onValueChange}
onOpenChange={props.onOpenChange}
onOpenChange={onOpenChange}
>
<Select.Trigger
style={{
Expand All @@ -297,7 +324,7 @@ export const RadixSelect = React.memo(
},
}}
>
<Select.Value placeholder={props.value?.label} />
<Select.Value placeholder={valueLabel} />
<Select.Icon style={{ width: 12, height: 12 }}>
<SmallerIcons.ExpansionArrowDown />
</Select.Icon>
Expand Down Expand Up @@ -339,7 +366,7 @@ export const RadixSelect = React.memo(
)
}

const label = `${option.label.charAt(0).toUpperCase()}${option.label.slice(1)}`
const label = optionLabelToString(option, isOpen, props.value?.value ?? null)
return (
<Select.Item
key={`select-option-${props.id}-${index}`}
Expand Down
Loading