Skip to content

Commit

Permalink
add defaultValue prop to editable field components
Browse files Browse the repository at this point in the history
  • Loading branch information
GamerGirlandCo committed Jul 20, 2024
1 parent 305a96e commit ab55316
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/api/ui/basics.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import "./basics.css";

/** Various intents for buttons and other interactive elements. */
export type Intent = "error" | "warn" | "info" | "success";
type Omittable = "value" | "defaultValue"
export type Omittable = "value" | "defaultValue"
export const INTENT_CLASSES: Record<Intent, string> = {
error: "dc-intent-error",
warn: "dc-intent-warn",
Expand Down
31 changes: 18 additions & 13 deletions src/ui/fields/editable-fields.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Checkbox, Slider, Switch } from "api/ui/basics";
import { Checkbox, Omittable, Slider, Switch } from "api/ui/basics";
import { Field } from "expression/field";
import { Dispatch, useCallback, useMemo, useState } from "preact/hooks";
import { useFinalizer, useSetField } from "utils/fields";
Expand All @@ -12,12 +12,12 @@ export function FieldCheckbox(
dispatch: Dispatch<EditableAction<Field>>;
} & React.HTMLProps<HTMLInputElement>
) {
const { field, defaultChecked, dispatch, ...rest } = props;
const { field, defaultChecked = false, dispatch, ...rest } = props;
return (
<Checkbox
{...rest}
disabled={undefined}
defaultChecked={defaultChecked}
defaultChecked={(field?.value ?? defaultChecked) as boolean}
onCheckChange={useSetField(field, (b) => dispatch({type: "content-changed", newValue: {...field, value: b}}))}
checked={undefined}
/>
Expand All @@ -27,11 +27,12 @@ export function FieldCheckbox(
export function EditableTextField(props: {
field: Field;
inline: boolean;
defaultValue: string;
dispatch: Dispatch<EditableAction<string>>;
}) {
const { field, inline, dispatch } = props;
const { field, defaultValue = "", inline, dispatch } = props;

return <ControlledEditableTextField text={field.value as string} inline={inline} dispatch={dispatch} />;
return <ControlledEditableTextField text={(field?.value ?? defaultValue) as string} inline={inline} dispatch={dispatch} />;
}

export function ControlledEditableTextField(props: {
Expand Down Expand Up @@ -66,16 +67,17 @@ export function FieldSlider(
max: number;
step: number;
field: Field;
defaultValue: number;
dispatch: Dispatch<EditableAction<Field>>;
} & React.HTMLProps<HTMLInputElement>
} & Omit<React.HTMLProps<HTMLInputElement>, Omittable>
) {
const { field, dispatch, min, max, step, ...rest } = props;
const defaultValue = field.value as number;
const { field, dispatch, defaultValue = 0, min, max, step, ...rest } = props;
const value = (field?.value ?? defaultValue) as number;
return (
<Slider
{...rest}
disabled={false}
defaultValue={defaultValue}
defaultValue={value}
min={min}
max={max}
step={step}
Expand All @@ -90,15 +92,16 @@ export function FieldSwitch(
className?: string;
disabled?: boolean;
field: Field;
defaultValue: boolean;
dispatch: Dispatch<EditableAction<Field>>;
} & React.HTMLProps<HTMLInputElement>
} & Omit<React.HTMLProps<HTMLInputElement>, Omittable>
) {
const { field, dispatch, ...rest } = props;
const { field, dispatch, defaultValue = false, ...rest } = props;
return (
<Switch
{...rest}
onToggleChange={useSetField(field, (b) => dispatch({type: "content-changed", newValue: {...field, value: b}}))}
defaultChecked={field.value as boolean}
defaultChecked={(field.value ?? defaultValue) as boolean}
checked={undefined}
/>
);
Expand All @@ -108,10 +111,12 @@ export function FieldSelect({
field,
multi = false,
options,
defaultValue,
dispatch
}: {
field: Field;
multi?: boolean;
defaultValue: string | string[];
options: { value: string; label: string }[];
dispatch: Dispatch<EditableAction<Field>>;
}) {
Expand All @@ -126,7 +131,7 @@ export function FieldSelect({
innerCallback(normalized)
}, []);

const arrayVal = useMemo(() => Array.isArray(field.value) ? field.value : [field.value], [field])
const arrayVal = useMemo(() => Array.isArray(field.value) ? field.value : !!field ? [field.value] : [defaultValue], [field])
const defVal = useMemo(
() =>
multi
Expand Down

0 comments on commit ab55316

Please sign in to comment.