Skip to content

Commit

Permalink
handle boolean and defaults
Browse files Browse the repository at this point in the history
  • Loading branch information
kermage committed Apr 10, 2024
1 parent 1c3b3ed commit 7ea3f04
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
14 changes: 12 additions & 2 deletions src/components/CodePreview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,19 @@ interface Props {

const item = (value: FieldItemType) => {
const items = Object.entries(value)
.filter(([key]) => key !== 'key')
.filter(([key, value]) => {
if (key === 'key') {
return false;
}

return undefined !== value;
})
.map(([key, val]) => {
return `'${key}' => '${val}'`;
if (typeof val === 'string') {
val = `'${val}'`;
}

return `'${key}' => ${val}`;
});

return `'${value.key}' => array(
Expand Down
18 changes: 17 additions & 1 deletion src/components/FieldForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,22 @@ export const classes = {
field: 'col-span-3 rounded bg-neutral-600 px-3 py-1',
};

const DEFAULTS = {
multiple: false,
none: false,
style: '',
repeatable: false,
required: false,
};

const prepare = (name: string, value?: string | boolean) => {
if (name in DEFAULTS && value === DEFAULTS[name as keyof typeof DEFAULTS]) {
value = undefined;
}

return value;
};

export default function ({ values, save }: Props) {
const [showAdvanced, setShowAdvanced] = useState(false);

Expand All @@ -23,7 +39,7 @@ export default function ({ values, save }: Props) {

save({
...values,
[name]: saveValue,
[name]: prepare(name, saveValue),
});
};

Expand Down

0 comments on commit 7ea3f04

Please sign in to comment.