Skip to content

Commit 7f114f4

Browse files
authored
fix: Allow reset optional items (#4759)
## Description Allow resetting optional items in the following cases: 1. The item has a value and undefined defaultValue 2. The item has a value, defaultValue is not undefined and is not equal to value - [x] - Fix placeholder is not shown ## Todo Next PR Remove empty prop check https://github.com/webstudio-is/webstudio/blob/42af9210934f0c5c8fb1f1943bd66cd086c85032/packages/sdk-components-react/src/head-meta.tsx#L27 Switch on undefined. ## Steps for reproduction Case 1. - [x] - HeadSlot/HeadLink Select value, reset value <img width="252" alt="image" src="https://github.com/user-attachments/assets/1944c6f0-32c5-4dc5-aa3e-f2b8cf1593a7" /> Case 2. - [x] - Youtube Component Unselect <img width="244" alt="image" src="https://github.com/user-attachments/assets/a55e9105-1c4b-4439-8f2b-f1af05a15964" /> Reset to default. ## Code Review - [ ] hi @kof, I need you to do - conceptual review (architecture, feature-correctness) - detailed review (read every line) - test it on preview ## Before requesting a review - [ ] made a self-review - [ ] added inline comments where things may be not obvious (the "why", not "what") ## Before merging - [ ] tested locally and on preview environment (preview dev login: 0000) - [ ] updated [test cases](https://github.com/webstudio-is/webstudio/blob/main/apps/builder/docs/test-cases.md) document - [ ] added tests - [ ] if any new env variables are added, added them to `.env` file
1 parent 3cf9f31 commit 7f114f4

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

apps/builder/app/builder/features/settings-panel/props-section/props-section.tsx

+5-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,11 @@ const renderProperty = (
6868
prop,
6969
computedValue: propValues.get(propName) ?? meta.defaultValue,
7070
propName,
71-
deletable: deletable ?? false,
71+
deletable:
72+
deletable ??
73+
((meta.defaultValue === undefined || meta.defaultValue !== prop?.value) &&
74+
meta.required === false &&
75+
prop !== undefined),
7276
onDelete: () => {
7377
if (prop) {
7478
logic.handleDelete(prop);

packages/design-system/src/components/select.tsx

+16
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import {
77
useMemo,
88
forwardRef,
99
useState,
10+
useEffect,
11+
useRef,
1012
} from "react";
1113
import { styled, theme } from "../stitches.config";
1214
import {
@@ -221,9 +223,23 @@ const SelectBase = <Option,>(
221223

222224
const descriptions = options.map((option) => getDescription?.(option));
223225

226+
// Allow reset select fix https://github.com/radix-ui/primitives/issues/2706
227+
const [selectResetKeyFix, setSelectResetKeyFix] = useState(0);
228+
const prevValue = useRef(value);
229+
230+
useEffect(() => {
231+
if (prevValue.current !== undefined && value === undefined) {
232+
setSelectResetKeyFix((prev) => prev + 1);
233+
}
234+
235+
prevValue.current = value;
236+
}, [value]);
237+
224238
return (
225239
<Primitive.Root
240+
key={selectResetKeyFix}
226241
name={name}
242+
// null because of https://github.com/radix-ui/primitives/issues/2706
227243
value={value === undefined ? undefined : getValue(value)}
228244
defaultValue={
229245
defaultValue === undefined ? undefined : getValue(defaultValue)

0 commit comments

Comments
 (0)