Skip to content

Commit

Permalink
allow adding existing properties with it's value
Browse files Browse the repository at this point in the history
  • Loading branch information
kof committed Feb 8, 2025
1 parent d54f44b commit 32bd01f
Showing 1 changed file with 21 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { colord } from "colord";
import {
memo,
useEffect,
useMemo,
useRef,
useState,
type ComponentProps,
Expand Down Expand Up @@ -161,6 +160,13 @@ const insertStyles = (text: string) => {
return true;
};

const sortedProperties = Object.keys(propertiesData)
.sort(Intl.Collator().compare)
.map((property) => ({
value: property,
label: hyphenateProperty(property),
}));

/**
*
* Advanced search control supports following interactions
Expand All @@ -172,36 +178,19 @@ const insertStyles = (text: string) => {
*
*/
const AddProperty = ({
usedProperties,
onSelect,
onClose,
}: {
usedProperties: string[];
onSelect: (value: StyleProperty) => void;
onClose: () => void;
}) => {
const availableProperties = useMemo(() => {
const properties = Object.keys(propertiesData).sort(
Intl.Collator().compare
) as StyleProperty[];
const availableProperties: SearchItem[] = [];
for (const property of properties) {
if (usedProperties.includes(property) === false) {
availableProperties.push({
value: property,
label: hyphenateProperty(property),
});
}
}
return availableProperties;
}, [usedProperties]);
const [item, setItem] = useState<SearchItem>({
value: "",
label: "",
});

const combobox = useCombobox<SearchItem>({
getItems: () => availableProperties,
getItems: () => sortedProperties,
itemToString: (item) => item?.label ?? "",
value: item,
defaultHighlightedIndex: 0,
Expand Down Expand Up @@ -545,10 +534,10 @@ const AdvancedProperty = memo(
export const Section = () => {
const [isAdding, setIsAdding] = useState(false);
const advancedProperties = useStore($advancedProperties);
const recentProperties = useRef<StyleProperty[]>([]);
let [recentProperties, setRecentProperties] = useState<StyleProperty[]>([]);

Check failure on line 537 in apps/builder/app/builder/features/style-panel/sections/advanced/advanced.tsx

View workflow job for this annotation

GitHub Actions / checks (empty)

'setRecentProperties' is never reassigned. Use 'const' instead

Check failure on line 537 in apps/builder/app/builder/features/style-panel/sections/advanced/advanced.tsx

View workflow job for this annotation

GitHub Actions / checks (development)

'setRecentProperties' is never reassigned. Use 'const' instead

// In case the property was deleted, it will be removed from advanced, so we need to remove it from recent too.
recentProperties.current = recentProperties.current.filter((property) =>
recentProperties = recentProperties.filter((property) =>
advancedProperties.includes(property)
);

Expand All @@ -559,7 +548,7 @@ export const Section = () => {
onAdd={() => setIsAdding(true)}
>
<Box css={{ paddingInline: theme.panel.paddingInline }}>
{recentProperties.current.map((property, index, properties) => (
{recentProperties.map((property, index, properties) => (
<AdvancedProperty
key={property}
property={property}
Expand All @@ -571,25 +560,25 @@ export const Section = () => {
))}
{isAdding && (
<AddProperty
usedProperties={advancedProperties}
onSelect={(property) => {
recentProperties.current.push(property);
setIsAdding(false);
setProperty(property)(
{ type: "guaranteedInvalid" },
{ listed: true }
);
const isNew = advancedProperties.includes(property) === false;
if (isNew) {
setProperty(property)(
{ type: "guaranteedInvalid" },
{ listed: true }
);
}
setRecentProperties([...recentProperties, property]);
}}
onClose={() => setIsAdding(false)}
/>
)}
</Box>
{recentProperties.current.length > 0 && <Separator />}
{recentProperties.length > 0 && <Separator />}
<Box css={{ paddingInline: theme.panel.paddingInline }}>
{advancedProperties
.filter(
(property) => recentProperties.current.includes(property) === false
)
.filter((property) => recentProperties.includes(property) === false)
.map((property) => (
<AdvancedProperty key={property} property={property} />
))}
Expand Down

0 comments on commit 32bd01f

Please sign in to comment.