-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
305 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import type { HTMLAttributes } from 'preact/compat'; | ||
|
||
type Props = HTMLAttributes<HTMLButtonElement>; | ||
|
||
export default function ({ children, ...props }: Props) { | ||
return ( | ||
<button {...props} className="rounded border border-neutral-700 bg-neutral-800 px-6 py-2 text-sm text-neutral-200 hover:ring"> | ||
{children} | ||
</button> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { effect } from '@preact/signals'; | ||
import { useState } from 'preact/hooks'; | ||
import { codeToHtml } from 'shiki'; | ||
|
||
import type { Signal } from '@preact/signals'; | ||
import type { FieldItemType } from '../types'; | ||
|
||
interface Props { | ||
data: Signal<Signal<FieldItemType>[]>; | ||
} | ||
|
||
const item = (value: FieldItemType) => { | ||
const items = Object.entries(value) | ||
.filter(([key]) => key !== 'key') | ||
.map(([key, val]) => { | ||
return `'${key}' => '${val}'`; | ||
}); | ||
|
||
return `'${value.key}' => array( | ||
${items.join(',\n\t\t')}, | ||
)`; | ||
}; | ||
|
||
const wrap = (value: Signal<FieldItemType>[]) => { | ||
const items = Object.values(value).map((val) => `${item(val.value)}`); | ||
|
||
return `array( | ||
${items.join(',\n\t')}, | ||
);`; | ||
}; | ||
|
||
export default function ({ data }: Props) { | ||
const [html, setHtml] = useState(''); | ||
|
||
effect(() => { | ||
codeToHtml(wrap(data.value), { | ||
lang: 'php', | ||
theme: 'dark-plus', | ||
}).then(setHtml); | ||
}); | ||
|
||
return <div className="" dangerouslySetInnerHTML={{ __html: html }}></div>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import { FIELD_TYPES, type FieldItemType } from '../types'; | ||
|
||
type Props = { | ||
values: FieldItemType; | ||
save: (values: FieldItemType) => void; | ||
}; | ||
|
||
const classes = { | ||
label: 'grid grid-cols-4 gap-4 items-baseline', | ||
field: 'appearance-none col-span-3 rounded bg-neutral-600 px-3 py-1', | ||
}; | ||
export default function ({ values, save }: Props) { | ||
const { title, key, description, type } = values; | ||
|
||
const handleChange = (event: any) => { | ||
const { name, value } = event.currentTarget; | ||
|
||
save({ | ||
...values, | ||
[name]: value, | ||
}); | ||
}; | ||
|
||
return ( | ||
<> | ||
<label className={classes.label}> | ||
Key | ||
<input | ||
className={classes.field} | ||
type="text" | ||
name="key" | ||
onInput={handleChange} | ||
value={key} | ||
/> | ||
</label> | ||
|
||
<label className={classes.label}> | ||
Type | ||
<select | ||
className={classes.field} | ||
name="type" | ||
onChange={handleChange} | ||
value={type} | ||
> | ||
{FIELD_TYPES.map((fType, index) => ( | ||
<option key={index} value={fType}> | ||
{fType} | ||
</option> | ||
))} | ||
</select> | ||
</label> | ||
|
||
<label className={classes.label}> | ||
Title | ||
<input | ||
className={classes.field} | ||
type="text" | ||
name="title" | ||
onInput={handleChange} | ||
value={title} | ||
/> | ||
</label> | ||
|
||
<label className={classes.label}> | ||
Description | ||
<textarea className={classes.field} name="description" onInput={handleChange}> | ||
{description} | ||
</textarea> | ||
</label> | ||
</> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { useComputed, useSignal } from '@preact/signals'; | ||
import FieldForm from './FieldForm'; | ||
|
||
import type { Signal } from '@preact/signals'; | ||
import type { FieldItemType } from '../types'; | ||
|
||
type Props = { | ||
defaultOpen: boolean; | ||
id: number; | ||
data: Signal<FieldItemType>; | ||
}; | ||
|
||
export default function ({ defaultOpen, id, data }: Props) { | ||
const title = useComputed(() => data.value.title); | ||
const type = useComputed(() => data.value.type.toUpperCase()); | ||
const collapsed = useSignal(!defaultOpen); | ||
const isCollapsed = useComputed(() => (collapsed.value ? 'true' : 'false')); | ||
const className = useComputed( | ||
() => `grid gap-2 border-neutral-600 p-4 ${collapsed.value ? 'hidden' : ''}`, | ||
); | ||
|
||
return ( | ||
<li key={id} className="rounded bg-neutral-800 text-white shadow"> | ||
<h2 id={`preview-${id}`} aria-hidden={isCollapsed} aria-controls={`form-${id}`}> | ||
<button | ||
className="p-4 grid w-full grid-cols-4 items-center gap-4 hover:ring" | ||
onClick={() => { | ||
collapsed.value = !collapsed.value; | ||
}} | ||
> | ||
<span className="bg-neutral-600 p-1 text-xs">{type}</span> | ||
<span className="col-span-3 text-left">{title}</span> | ||
</button> | ||
|
||
<form id={`form-${id}`} className={className} aria-labelledby={`preview-${id}`}> | ||
<FieldForm values={data.peek()} save={(values) => (data.value = values)} /> | ||
</form> | ||
</h2> | ||
</li> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { signal, useComputed } from '@preact/signals'; | ||
import { FIELD_TYPES } from '../types'; | ||
import Button from './Button'; | ||
import FieldItem from './FieldItem'; | ||
|
||
import type { Signal } from '@preact/signals'; | ||
import type { FieldItemType } from '../types'; | ||
|
||
type Props = { | ||
items: Signal<Signal<FieldItemType>[]>; | ||
initLength: number; | ||
}; | ||
|
||
export default function ({ items, initLength }: Props) { | ||
const nextIndex = useComputed(() => items.value.length + 1); | ||
|
||
const addField = () => { | ||
items.value = [ | ||
...items.value, | ||
signal({ | ||
key: `index-${nextIndex}`, | ||
type: FIELD_TYPES[0], | ||
title: `Title #${nextIndex}`, | ||
description: `Description #${nextIndex}`, | ||
}), | ||
]; | ||
}; | ||
|
||
return ( | ||
<div className="grid auto-rows-min gap-4 p-4 lg:p-8"> | ||
<ol className="grid gap-4"> | ||
{items.value.map((field, index) => ( | ||
<FieldItem id={index} data={field} defaultOpen={index + 1 >= initLength} /> | ||
))} | ||
</ol> | ||
|
||
<div className="mt-2"> | ||
<Button onClick={() => addField()}>Add</Button> | ||
</div> | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
export const FIELD_TYPES = [ | ||
'input', | ||
'textarea', | ||
'date', | ||
'select', | ||
'radio', | ||
'checkbox', | ||
'color', | ||
'file', | ||
'number', | ||
'editor', | ||
'html', | ||
'link', | ||
] as const; | ||
|
||
export type FieldItemType = { | ||
key: string; | ||
type: (typeof FIELD_TYPES)[number]; | ||
title: string; | ||
description: string; | ||
}; |