@kerf-js/veneer 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +16 -0
- package/README.md +1338 -2
- package/package.json +56 -3
- package/plugin.d.mts +187 -0
- package/plugin.mjs +399 -0
- package/src/Accordion.tsx +98 -0
- package/src/Alert.tsx +55 -0
- package/src/Badge.tsx +39 -0
- package/src/Button.tsx +175 -0
- package/src/Calendar-month.tsx +140 -0
- package/src/Calendar.tsx +335 -0
- package/src/Canvas.tsx +221 -0
- package/src/Card.tsx +85 -0
- package/src/Chart.tsx +278 -0
- package/src/Charts.tsx +158 -0
- package/src/Chip.tsx +61 -0
- package/src/Choice-checkbox.tsx +53 -0
- package/src/Choice-parts.tsx +7 -0
- package/src/Choice-radio.tsx +78 -0
- package/src/Choice-segmented.tsx +76 -0
- package/src/Choice-switch.tsx +51 -0
- package/src/Choice.tsx +4 -0
- package/src/CloseX.tsx +14 -0
- package/src/CodeBlock.tsx +81 -0
- package/src/CodeInput.tsx +256 -0
- package/src/Combobox.tsx +281 -0
- package/src/CommandPalette-controller.ts +314 -0
- package/src/CommandPalette-rows.tsx +137 -0
- package/src/CommandPalette.tsx +196 -0
- package/src/DataGrid-actions.ts +119 -0
- package/src/DataGrid-body.tsx +58 -0
- package/src/DataGrid-controller.ts +145 -0
- package/src/DataGrid-header.tsx +108 -0
- package/src/DataGrid-model.ts +248 -0
- package/src/DataGrid-pivot.tsx +108 -0
- package/src/DataGrid-rows.tsx +296 -0
- package/src/DataGrid-toolbar.tsx +127 -0
- package/src/DataGrid-tree.ts +149 -0
- package/src/DataGrid-viewport.tsx +37 -0
- package/src/DataGrid.tsx +145 -0
- package/src/DateField-presets.tsx +27 -0
- package/src/DateField-range.tsx +370 -0
- package/src/DateField.tsx +295 -0
- package/src/Dither.tsx +313 -0
- package/src/Drawer.tsx +123 -0
- package/src/DurationField.tsx +227 -0
- package/src/Feed.tsx +78 -0
- package/src/Field/view.tsx +147 -0
- package/src/Field.tsx +238 -0
- package/src/HamburgerMenu.tsx +119 -0
- package/src/Icon.tsx +15 -0
- package/src/Kbd.tsx +6 -0
- package/src/KeyValueEditor/actions.ts +75 -0
- package/src/KeyValueEditor/rows.tsx +104 -0
- package/src/KeyValueEditor.tsx +112 -0
- package/src/Layout.tsx +221 -0
- package/src/Markdown.tsx +127 -0
- package/src/MentionsInput/actions.ts +161 -0
- package/src/MentionsInput/end.tsx +82 -0
- package/src/MentionsInput.tsx +103 -0
- package/src/Menu.tsx +221 -0
- package/src/Modal.tsx +109 -0
- package/src/MonthPicker/month.tsx +117 -0
- package/src/MonthPicker/period.tsx +197 -0
- package/src/MonthPicker/shared.tsx +272 -0
- package/src/MonthPicker/week.tsx +103 -0
- package/src/MonthPicker.tsx +11 -0
- package/src/MultiSelect.tsx +139 -0
- package/src/Nav.tsx +36 -0
- package/src/NumberField.tsx +156 -0
- package/src/Pagination.tsx +335 -0
- package/src/PaymentCard.tsx +111 -0
- package/src/Picker.tsx +228 -0
- package/src/PlaceField.tsx +285 -0
- package/src/Popover.tsx +88 -0
- package/src/Presence.tsx +37 -0
- package/src/Progress.tsx +71 -0
- package/src/RecurrenceField.tsx +372 -0
- package/src/RichSelect/codes.tsx +94 -0
- package/src/RichSelect/list.tsx +380 -0
- package/src/RichSelect/phone-view.tsx +114 -0
- package/src/RichSelect/phone.tsx +305 -0
- package/src/RichSelect/search-keys.ts +28 -0
- package/src/RichSelect/timezone.tsx +53 -0
- package/src/RichSelect.tsx +13 -0
- package/src/RichTextEditor/controller.tsx +305 -0
- package/src/RichTextEditor/editor.tsx +61 -0
- package/src/RichTextEditor/tools.tsx +132 -0
- package/src/RichTextEditor/view.tsx +210 -0
- package/src/RichTextEditor.tsx +9 -0
- package/src/ScrollGrid/measure.ts +39 -0
- package/src/ScrollGrid/parts.tsx +112 -0
- package/src/ScrollGrid.tsx +375 -0
- package/src/ScrollRail.tsx +114 -0
- package/src/ScrollView.tsx +57 -0
- package/src/Segments/controller.tsx +148 -0
- package/src/Segments/events.ts +99 -0
- package/src/Segments/view.tsx +77 -0
- package/src/Segments.tsx +2 -0
- package/src/Select.tsx +343 -0
- package/src/Shape.tsx +41 -0
- package/src/Shell/branches.tsx +108 -0
- package/src/Shell/button.tsx +33 -0
- package/src/Shell/shared.tsx +220 -0
- package/src/Shell/shell.tsx +25 -0
- package/src/Shell/state.ts +138 -0
- package/src/Shell/view.tsx +280 -0
- package/src/Shell.tsx +17 -0
- package/src/SideNav/sections.tsx +50 -0
- package/src/SideNav/state.ts +140 -0
- package/src/SideNav.tsx +130 -0
- package/src/SideNavTitle.tsx +25 -0
- package/src/SignaturePad/state.ts +172 -0
- package/src/SignaturePad/view.tsx +69 -0
- package/src/SignaturePad.tsx +76 -0
- package/src/Skeleton.tsx +46 -0
- package/src/Slider.tsx +81 -0
- package/src/Spinner.tsx +21 -0
- package/src/Stage-director.ts +361 -0
- package/src/Stage-look.ts +75 -0
- package/src/Stage-poster.tsx +62 -0
- package/src/Stage-types.ts +206 -0
- package/src/Stage.tsx +147 -0
- package/src/TabBar.tsx +114 -0
- package/src/Table-actions.tsx +51 -0
- package/src/Table-base.ts +70 -0
- package/src/Table-body.tsx +84 -0
- package/src/Table-columns.tsx +58 -0
- package/src/Table-details.tsx +66 -0
- package/src/Table-editor.tsx +66 -0
- package/src/Table-head.tsx +52 -0
- package/src/Table-model.ts +82 -0
- package/src/Table-notice.ts +39 -0
- package/src/Table-paging.ts +50 -0
- package/src/Table-rows.tsx +308 -0
- package/src/Table-selection.ts +63 -0
- package/src/Table-sortbar.tsx +41 -0
- package/src/Table-source.ts +66 -0
- package/src/Table-toolbar.tsx +71 -0
- package/src/Table-types.ts +115 -0
- package/src/Table-view.tsx +87 -0
- package/src/Table.tsx +31 -0
- package/src/Tabs.tsx +129 -0
- package/src/Tag.tsx +40 -0
- package/src/Ticker.tsx +42 -0
- package/src/TimeField-date-model.ts +77 -0
- package/src/TimeField-date-view.tsx +152 -0
- package/src/TimeField-model.ts +80 -0
- package/src/TimeField-options.tsx +224 -0
- package/src/TimeField-slots.tsx +32 -0
- package/src/TimeField-types.ts +33 -0
- package/src/TimeField-view.tsx +124 -0
- package/src/TimeField.tsx +126 -0
- package/src/Toast.tsx +143 -0
- package/src/Toggle.tsx +75 -0
- package/src/Tooltip.tsx +69 -0
- package/src/Type.tsx +136 -0
- package/src/VirtualList-controls.ts +105 -0
- package/src/VirtualList-data.ts +43 -0
- package/src/VirtualList-measure.ts +169 -0
- package/src/VirtualList-metrics.ts +41 -0
- package/src/VirtualList-mount.ts +145 -0
- package/src/VirtualList-rows.tsx +91 -0
- package/src/VirtualList-types.ts +76 -0
- package/src/VirtualList-view.tsx +123 -0
- package/src/VirtualList.tsx +81 -0
- package/src/admin/ApiKeys-actions.ts +124 -0
- package/src/admin/ApiKeys-form.tsx +64 -0
- package/src/admin/ApiKeys-view.tsx +101 -0
- package/src/admin/ApiKeys.tsx +224 -0
- package/src/admin/Audit.tsx +246 -0
- package/src/admin/Billing.tsx +307 -0
- package/src/admin/Cart.tsx +177 -0
- package/src/admin/Checkout-address.tsx +125 -0
- package/src/admin/Checkout-cart.tsx +78 -0
- package/src/admin/Checkout-coupon.ts +34 -0
- package/src/admin/Checkout-done.tsx +31 -0
- package/src/admin/Checkout-model.ts +33 -0
- package/src/admin/Checkout-navigation.ts +76 -0
- package/src/admin/Checkout-payment-view.tsx +49 -0
- package/src/admin/Checkout-payment.ts +80 -0
- package/src/admin/Checkout-review.tsx +60 -0
- package/src/admin/Checkout-shipping.tsx +47 -0
- package/src/admin/Checkout-state.ts +122 -0
- package/src/admin/Checkout-steps.ts +10 -0
- package/src/admin/Checkout-summary.tsx +97 -0
- package/src/admin/Checkout-types.ts +60 -0
- package/src/admin/Checkout-view.tsx +51 -0
- package/src/admin/Checkout.tsx +72 -0
- package/src/admin/CsvImport-actions.ts +131 -0
- package/src/admin/CsvImport.tsx +379 -0
- package/src/admin/Danger.tsx +240 -0
- package/src/admin/Integrations/Card.tsx +82 -0
- package/src/admin/Integrations/Webhooks.tsx +341 -0
- package/src/admin/Integrations.tsx +169 -0
- package/src/admin/InviteDialog.tsx +153 -0
- package/src/admin/Members/columns.tsx +181 -0
- package/src/admin/Members.tsx +316 -0
- package/src/admin/Order.tsx +115 -0
- package/src/admin/PlainTable.tsx +34 -0
- package/src/admin/Plans.tsx +375 -0
- package/src/admin/SaveBar/actions.ts +86 -0
- package/src/admin/SaveBar.tsx +153 -0
- package/src/admin/Secret.tsx +74 -0
- package/src/admin/Settings.tsx +145 -0
- package/src/admin/StepBar.tsx +66 -0
- package/src/admin/TagsInput/actions.ts +60 -0
- package/src/admin/TagsInput.tsx +95 -0
- package/src/admin/atoms.tsx +73 -0
- package/src/admin/index.ts +67 -0
- package/src/agent/Actions/reasons.tsx +38 -0
- package/src/agent/Actions.tsx +166 -0
- package/src/agent/AgentPlan.tsx +86 -0
- package/src/agent/ArtifactPanel.tsx +299 -0
- package/src/agent/Assistant.tsx +168 -0
- package/src/agent/Checkpoint.tsx +19 -0
- package/src/agent/Cite.tsx +70 -0
- package/src/agent/ContextMeter.tsx +70 -0
- package/src/agent/Conversation.tsx +70 -0
- package/src/agent/Message.tsx +92 -0
- package/src/agent/ModelPicker.tsx +167 -0
- package/src/agent/Prompt-model.ts +118 -0
- package/src/agent/Prompt-view.tsx +246 -0
- package/src/agent/Prompt.tsx +128 -0
- package/src/agent/Prose.tsx +118 -0
- package/src/agent/Question.tsx +137 -0
- package/src/agent/Reasoning.tsx +31 -0
- package/src/agent/Shimmer.tsx +11 -0
- package/src/agent/Sources.tsx +23 -0
- package/src/agent/SpeechInput.tsx +243 -0
- package/src/agent/Suggestions.tsx +36 -0
- package/src/agent/ToolCall.tsx +219 -0
- package/src/agent/TraceViewer.tsx +96 -0
- package/src/agent/bits.tsx +88 -0
- package/src/agent/index.ts +38 -0
- package/src/bind.ts +57 -0
- package/src/collab/AutoTrack.tsx +91 -0
- package/src/collab/Comments.tsx +204 -0
- package/src/collab/Composer/state.ts +125 -0
- package/src/collab/Composer/view.tsx +155 -0
- package/src/collab/Composer.tsx +141 -0
- package/src/collab/ConversationList.tsx +120 -0
- package/src/collab/Cursors.tsx +71 -0
- package/src/collab/DataIntegrity.tsx +103 -0
- package/src/collab/Kanban-card.ts +74 -0
- package/src/collab/Kanban.tsx +368 -0
- package/src/collab/Lock.tsx +36 -0
- package/src/collab/PresenceStack.tsx +176 -0
- package/src/collab/ShareDialog.tsx +273 -0
- package/src/collab/Status.tsx +101 -0
- package/src/collab/SyncedForm.tsx +42 -0
- package/src/collab/Thread/Message.tsx +265 -0
- package/src/collab/Thread/rows.tsx +56 -0
- package/src/collab/Thread.tsx +323 -0
- package/src/collab/VersionHistory.tsx +173 -0
- package/src/collab/ViewportRail.tsx +73 -0
- package/src/collab/index.ts +37 -0
- package/src/collab/peer.tsx +42 -0
- package/src/fields.ts +5 -0
- package/src/flow/AuthScreen.tsx +81 -0
- package/src/flow/AuthSteps.tsx +296 -0
- package/src/flow/Checklist.tsx +211 -0
- package/src/flow/ConnBanner.tsx +304 -0
- package/src/flow/Flow.tsx +97 -0
- package/src/flow/MfaSetup.tsx +244 -0
- package/src/flow/Otp.tsx +243 -0
- package/src/flow/ReauthDialog.tsx +79 -0
- package/src/flow/ShortcutsOverlay.tsx +121 -0
- package/src/flow/SignGate.tsx +184 -0
- package/src/flow/SignIn.tsx +136 -0
- package/src/flow/SignUp.tsx +149 -0
- package/src/flow/Sso.tsx +96 -0
- package/src/flow/SystemPage/accounts.tsx +74 -0
- package/src/flow/SystemPage/art.tsx +113 -0
- package/src/flow/SystemPage.tsx +395 -0
- package/src/flow/Tour.tsx +207 -0
- package/src/flow/index.ts +29 -0
- package/src/flow/kit.tsx +334 -0
- package/src/index.ts +237 -0
- package/src/layout.css +4 -0
- package/src/links.css +4 -0
- package/src/live/ActivityFeed.tsx +88 -0
- package/src/live/Bell.tsx +108 -0
- package/src/live/Connection.tsx +113 -0
- package/src/live/Inbox.tsx +113 -0
- package/src/live/InboxFeed.tsx +67 -0
- package/src/live/InboxGestures.ts +41 -0
- package/src/live/InboxHandlers.ts +102 -0
- package/src/live/InboxHeader.tsx +81 -0
- package/src/live/InboxModel.ts +81 -0
- package/src/live/InboxRow.tsx +125 -0
- package/src/live/MeetingRoom.tsx +22 -0
- package/src/live/MeetingRoomControlBar.tsx +197 -0
- package/src/live/MeetingRoomHandlers.ts +53 -0
- package/src/live/MeetingRoomLayout.tsx +27 -0
- package/src/live/MeetingRoomModel.ts +49 -0
- package/src/live/MeetingRoomMount.ts +49 -0
- package/src/live/MeetingRoomParts.tsx +178 -0
- package/src/live/MeetingRoomPerson.tsx +21 -0
- package/src/live/MeetingRoomState.ts +88 -0
- package/src/live/MeetingRoomTile.tsx +84 -0
- package/src/live/MeetingRoomTypes.ts +68 -0
- package/src/live/MeetingRoomView.tsx +36 -0
- package/src/live/NotificationPreferences.tsx +99 -0
- package/src/live/NotificationPreferencesActions.ts +55 -0
- package/src/live/NotificationPreferencesCell.tsx +26 -0
- package/src/live/NotificationPreferencesCells.tsx +59 -0
- package/src/live/NotificationPreferencesGrid.tsx +129 -0
- package/src/live/NotificationPreferencesModel.tsx +91 -0
- package/src/live/NotificationPreferencesSave.ts +85 -0
- package/src/live/NotificationPreferencesWatch.ts +31 -0
- package/src/live/Prejoin.tsx +238 -0
- package/src/live/PrejoinMeter.tsx +26 -0
- package/src/live/PrejoinPreview.tsx +53 -0
- package/src/live/PushSoftAsk.tsx +63 -0
- package/src/live/TransportPicker.tsx +341 -0
- package/src/live/TransportPickerDetails.tsx +130 -0
- package/src/live/index.ts +31 -0
- package/src/live/notify.tsx +22 -0
- package/src/live/shared.tsx +110 -0
- package/src/parts.tsx +199 -0
- package/src/plugin/catalog.mjs +129 -0
- package/src/plugin/dtcg.mjs +79 -0
- package/src/plugin/icons.mjs +7 -0
- package/src/plugin/seed.mjs +40 -0
- package/src/plugin/theme-context.mjs +44 -0
- package/src/plugin/theme-css.mjs +143 -0
- package/src/plugin/theme-motion.mjs +43 -0
- package/src/plugin/theme-parts.d.mts +41 -0
- package/src/plugin/theme-parts.mjs +224 -0
- package/src/plugin/theme-text.mjs +93 -0
- package/src/plugin/theme-values.mjs +15 -0
- package/src/plugin/theme.d.mts +82 -0
- package/src/plugin/theme.mjs +316 -0
- package/src/schedule/Agenda.tsx +124 -0
- package/src/schedule/AgendaActions.ts +31 -0
- package/src/schedule/AgendaDay.tsx +124 -0
- package/src/schedule/AgendaGestures.ts +27 -0
- package/src/schedule/AgendaKeys.ts +52 -0
- package/src/schedule/AgendaModel.ts +82 -0
- package/src/schedule/AgendaNavigation.ts +38 -0
- package/src/schedule/AgendaPage.tsx +39 -0
- package/src/schedule/AgendaStrip.tsx +55 -0
- package/src/schedule/AvailabilityEditor-actions.ts +146 -0
- package/src/schedule/AvailabilityEditor-day.tsx +119 -0
- package/src/schedule/AvailabilityEditor-view.tsx +205 -0
- package/src/schedule/AvailabilityEditor.tsx +113 -0
- package/src/schedule/BookingCalendar.tsx +121 -0
- package/src/schedule/BookingCalendarActions.ts +85 -0
- package/src/schedule/BookingCalendarModel.ts +86 -0
- package/src/schedule/BookingCalendarParts.tsx +87 -0
- package/src/schedule/Calendar/AgendaView.tsx +45 -0
- package/src/schedule/Calendar/CalendarDialogs.tsx +105 -0
- package/src/schedule/Calendar/CalendarMain.tsx +47 -0
- package/src/schedule/Calendar/CalendarToolbar.tsx +106 -0
- package/src/schedule/Calendar/Column.tsx +129 -0
- package/src/schedule/Calendar/EventBlock.tsx +71 -0
- package/src/schedule/Calendar/MiniMonth.tsx +169 -0
- package/src/schedule/Calendar/MonthGrid.tsx +296 -0
- package/src/schedule/Calendar/TimeGrid.tsx +252 -0
- package/src/schedule/Calendar/Timeline.tsx +287 -0
- package/src/schedule/Calendar/ZoneChip.tsx +73 -0
- package/src/schedule/Calendar/calendar-shell.ts +160 -0
- package/src/schedule/Calendar/calendar-state.ts +30 -0
- package/src/schedule/Calendar/now-spans.tsx +33 -0
- package/src/schedule/Calendar/shared.ts +3 -0
- package/src/schedule/Calendar/state-actions.ts +189 -0
- package/src/schedule/Calendar/state-base.ts +143 -0
- package/src/schedule/Calendar/state-keys.ts +128 -0
- package/src/schedule/Calendar/time-fit.ts +84 -0
- package/src/schedule/Calendar.tsx +182 -0
- package/src/schedule/EventEditor.tsx +267 -0
- package/src/schedule/EventPopover.tsx +190 -0
- package/src/schedule/MeetingPlanner-model.ts +222 -0
- package/src/schedule/MeetingPlanner-view.tsx +310 -0
- package/src/schedule/MeetingPlanner.tsx +122 -0
- package/src/schedule/MoveSheet.tsx +168 -0
- package/src/schedule/YearView-model.ts +164 -0
- package/src/schedule/YearView-parts.tsx +147 -0
- package/src/schedule/YearView.tsx +100 -0
- package/src/schedule/bits.tsx +65 -0
- package/src/schedule/index.ts +25 -0
- package/src/skeleton-css.ts +4 -0
- package/src/styles.ts +13 -0
- package/src/type.css +15 -0
- package/src/ui.ts +41 -0
- package/src/use-ui.ts +10 -0
- package/tailwind.css +38 -0
- package/tokens/color.mjs +87 -0
package/src/Field.tsx
ADDED
|
@@ -0,0 +1,238 @@
|
|
|
1
|
+
import { SearchIcon } from '@kerf-js/veneer-icons'
|
|
2
|
+
import { field, formInvalid, formReset, labelChars, uid } from '@kerf-js/veneer-core'
|
|
3
|
+
import { derived, state, untrack, omit } from '@kerf-js/core'
|
|
4
|
+
import type { IconRef } from './Icon'
|
|
5
|
+
import { fieldView } from './Field/view'
|
|
6
|
+
import { use } from '@kerf-js/veneer-core/kerf'
|
|
7
|
+
import './fields'
|
|
8
|
+
import '@kerf-js/veneer-core/styles/inputs/text-field.css'
|
|
9
|
+
|
|
10
|
+
interface FieldBase {
|
|
11
|
+
/** The accessible name, drawn as the floating border label (or above, classic). Required. */
|
|
12
|
+
label: string
|
|
13
|
+
hint?: unknown
|
|
14
|
+
/** Shown instead of the hint; marks the field invalid. */
|
|
15
|
+
error?: unknown
|
|
16
|
+
/** Leading icon: it sweeps with the border. */
|
|
17
|
+
icon?: IconRef | undefined
|
|
18
|
+
/** Text or an element inside the field before the value, such as `$`. */
|
|
19
|
+
prefix?: unknown
|
|
20
|
+
/** Text or an element inside the field after the value, such as `kg`. */
|
|
21
|
+
suffix?: unknown
|
|
22
|
+
/** floating (default, or the app's `veneer({ fields })`) · classic: the label above. */
|
|
23
|
+
variant?: 'floating' | 'classic' | undefined
|
|
24
|
+
/** `line`: only a bottom line under the value, no box or fill. The boxed field otherwise. */
|
|
25
|
+
appearance?: 'line' | undefined
|
|
26
|
+
size?: 'sm' | 'md' | 'lg' | undefined
|
|
27
|
+
/** A clear button (shown once there is a value). */
|
|
28
|
+
clearable?: boolean | undefined
|
|
29
|
+
/** Maximum length, shown as a counter. */
|
|
30
|
+
counter?: number | undefined
|
|
31
|
+
id?: string | undefined
|
|
32
|
+
value?: string | undefined
|
|
33
|
+
disabled?: boolean | undefined
|
|
34
|
+
/** Shows the value and lets it be selected and copied, but not changed. */
|
|
35
|
+
readonly?: boolean | undefined
|
|
36
|
+
onInput?:
|
|
37
|
+
| ((e: InputEvent & { currentTarget: HTMLInputElement & HTMLTextAreaElement }) => void)
|
|
38
|
+
| undefined
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** Props for the text fields; anything else lands on the control. */
|
|
42
|
+
export interface FieldProps extends FieldBase {
|
|
43
|
+
[attr: string]: unknown
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** FieldProps for a component with its own value type: Omit on FieldProps would drop what the index signature hides. */
|
|
47
|
+
export type FieldShell = Omit<FieldBase, 'value' | 'defaultValue'> & { [attr: string]: unknown }
|
|
48
|
+
|
|
49
|
+
// `invalid` is a getter and `rest` the live props: the control is made once and its attributes update in place
|
|
50
|
+
export type Control = (p: {
|
|
51
|
+
id: string
|
|
52
|
+
readonly describedby?: string | undefined
|
|
53
|
+
readonly invalid: boolean
|
|
54
|
+
rest: Record<string, unknown>
|
|
55
|
+
}) => unknown
|
|
56
|
+
|
|
57
|
+
/** `labelChars`, which only needs `Intl.Segmenter` past ASCII: making one is most of the work of mounting a field. */
|
|
58
|
+
export const letters = (s: string) =>
|
|
59
|
+
/^[ -~]*$/.test(s) ? s.trim().replace(/ +/g, ' ').split('') : labelChars(s)
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* The field shell (03 §1 anatomy, verbatim order): control (sweep-base, sweep, icon, label, prefix, input, suffix,
|
|
63
|
+
* clear, end slot) then message (hint | error, counter). Everything is in the server HTML, label letters included;
|
|
64
|
+
* after mount veneer-core's field() measures the label's per-letter delays and notch and wires clear, counter and focus.
|
|
65
|
+
*/
|
|
66
|
+
export function Field(
|
|
67
|
+
props: FieldProps & {
|
|
68
|
+
control: Control
|
|
69
|
+
end?: unknown
|
|
70
|
+
filled?: boolean
|
|
71
|
+
attrs?: Record<string, unknown>
|
|
72
|
+
group?: boolean
|
|
73
|
+
/** The control takes its name from `aria-labelledby`, not `for`: a press on the label must not click a button. */ labelled?: boolean
|
|
74
|
+
hold?: boolean
|
|
75
|
+
wrap?: boolean
|
|
76
|
+
/** Puts held state back when the field's form resets. */ reset?: () => void
|
|
77
|
+
},
|
|
78
|
+
) {
|
|
79
|
+
return setupField(untrack(() => props))
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
const OWN = [
|
|
83
|
+
'label',
|
|
84
|
+
'hint',
|
|
85
|
+
'error',
|
|
86
|
+
'icon',
|
|
87
|
+
'prefix',
|
|
88
|
+
'suffix',
|
|
89
|
+
'variant',
|
|
90
|
+
'appearance',
|
|
91
|
+
'size',
|
|
92
|
+
'clearable',
|
|
93
|
+
'counter',
|
|
94
|
+
'id',
|
|
95
|
+
'control',
|
|
96
|
+
'end',
|
|
97
|
+
'filled',
|
|
98
|
+
'attrs',
|
|
99
|
+
'group',
|
|
100
|
+
'labelled',
|
|
101
|
+
'hold',
|
|
102
|
+
'wrap',
|
|
103
|
+
'reset',
|
|
104
|
+
]
|
|
105
|
+
|
|
106
|
+
function setupField(p: Parameters<typeof Field>[0]) {
|
|
107
|
+
const rest: Record<string, unknown> = omit(p, OWN)
|
|
108
|
+
const id = untrack(() => p.id) ?? uid('field')
|
|
109
|
+
// the browser's message for a constraint that failed (03 §1.3): `error` wins over it
|
|
110
|
+
const missing = state<string | undefined>(undefined)
|
|
111
|
+
const problem = derived(() => p.error ?? missing.value)
|
|
112
|
+
const glyph = derived(() => p.icon)
|
|
113
|
+
// an error can arrive long after mount, so the block and what points at it follow it
|
|
114
|
+
const message = derived(() => p.hint !== undefined || !!problem.value || p.counter !== undefined)
|
|
115
|
+
const msg = derived(() => (message.value ? `${id}-msg` : undefined))
|
|
116
|
+
// keyed by position: a label repeats letters
|
|
117
|
+
const chars = derived(() =>
|
|
118
|
+
letters(String((p.label as string | number | undefined) ?? '')).map((c, at) => ({ c, at })),
|
|
119
|
+
)
|
|
120
|
+
// The control keeps one rest view whose prop reads stay live.
|
|
121
|
+
const live = untrack(() => rest)
|
|
122
|
+
let root: HTMLDivElement | undefined
|
|
123
|
+
const shell = {
|
|
124
|
+
id,
|
|
125
|
+
get describedby() {
|
|
126
|
+
return msg.value
|
|
127
|
+
},
|
|
128
|
+
get invalid() {
|
|
129
|
+
return !!problem.value
|
|
130
|
+
},
|
|
131
|
+
rest: live,
|
|
132
|
+
}
|
|
133
|
+
use(
|
|
134
|
+
field,
|
|
135
|
+
() => root,
|
|
136
|
+
() => [live.value, p.label] as const,
|
|
137
|
+
)
|
|
138
|
+
use(
|
|
139
|
+
formInvalid,
|
|
140
|
+
() => root,
|
|
141
|
+
() => (m: string | undefined) => (missing.value = m),
|
|
142
|
+
)
|
|
143
|
+
if (untrack(() => p.reset))
|
|
144
|
+
use(
|
|
145
|
+
formReset,
|
|
146
|
+
() => root,
|
|
147
|
+
() => () => p.reset!(),
|
|
148
|
+
)
|
|
149
|
+
return fieldView({
|
|
150
|
+
p,
|
|
151
|
+
id,
|
|
152
|
+
problem,
|
|
153
|
+
glyph,
|
|
154
|
+
message,
|
|
155
|
+
msg,
|
|
156
|
+
chars,
|
|
157
|
+
live,
|
|
158
|
+
shell,
|
|
159
|
+
setRoot: (n: HTMLDivElement) => (root = n),
|
|
160
|
+
})
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/** A text field. Every native input attribute passes through (type, autocomplete, inputmode, onInput…). */
|
|
164
|
+
export function Input({
|
|
165
|
+
placeholder,
|
|
166
|
+
...props
|
|
167
|
+
}: FieldProps & { placeholder?: string; type?: string }) {
|
|
168
|
+
return (
|
|
169
|
+
<Field
|
|
170
|
+
{...props}
|
|
171
|
+
control={(p) => (
|
|
172
|
+
// a placeholder, " " at least: the label floats on :placeholder-shown (03 §1 decision 3)
|
|
173
|
+
<input
|
|
174
|
+
data-part="input"
|
|
175
|
+
id={p.id}
|
|
176
|
+
placeholder={placeholder ?? ' '}
|
|
177
|
+
aria-describedby={p.describedby}
|
|
178
|
+
aria-invalid={p.invalid ? 'true' : undefined}
|
|
179
|
+
{...p.rest}
|
|
180
|
+
/>
|
|
181
|
+
)}
|
|
182
|
+
/>
|
|
183
|
+
)
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/** A search field: the search icon, type=search, a clear button (03 §3 SearchInput; suggestions come with Combobox). */
|
|
187
|
+
export function Search({ placeholder, ...props }: FieldProps & { placeholder?: string }) {
|
|
188
|
+
return (
|
|
189
|
+
<Field
|
|
190
|
+
icon={SearchIcon}
|
|
191
|
+
clearable
|
|
192
|
+
{...props}
|
|
193
|
+
attrs={{ role: 'search' }}
|
|
194
|
+
control={(p) => (
|
|
195
|
+
<input
|
|
196
|
+
data-part="input"
|
|
197
|
+
id={p.id}
|
|
198
|
+
type="search"
|
|
199
|
+
enterkeyhint="search"
|
|
200
|
+
autocomplete="off"
|
|
201
|
+
placeholder={placeholder ?? ' '}
|
|
202
|
+
aria-describedby={p.describedby}
|
|
203
|
+
aria-invalid={p.invalid ? 'true' : undefined}
|
|
204
|
+
{...p.rest}
|
|
205
|
+
/>
|
|
206
|
+
)}
|
|
207
|
+
/>
|
|
208
|
+
)
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
/** A textarea: grows from `rows` lines (3) to 10 or `rows`, Cmd/Ctrl+Enter submits its form (03 §3). */
|
|
212
|
+
export function Textarea({
|
|
213
|
+
placeholder,
|
|
214
|
+
rows = 3,
|
|
215
|
+
...props
|
|
216
|
+
}: FieldProps & { placeholder?: string; rows?: number }) {
|
|
217
|
+
return (
|
|
218
|
+
<Field
|
|
219
|
+
{...props}
|
|
220
|
+
control={(p) => (
|
|
221
|
+
// field-sizing: content ignores the rows attribute, so the height reads it from --_rows
|
|
222
|
+
<textarea
|
|
223
|
+
data-part="input"
|
|
224
|
+
id={p.id}
|
|
225
|
+
rows={rows}
|
|
226
|
+
style={`--_rows:${rows}`}
|
|
227
|
+
enterkeyhint="enter"
|
|
228
|
+
placeholder={placeholder ?? ' '}
|
|
229
|
+
aria-describedby={p.describedby}
|
|
230
|
+
aria-invalid={p.invalid ? 'true' : undefined}
|
|
231
|
+
{...p.rest}
|
|
232
|
+
>
|
|
233
|
+
{untrack(() => p.rest.value)}
|
|
234
|
+
</textarea>
|
|
235
|
+
)}
|
|
236
|
+
/>
|
|
237
|
+
)
|
|
238
|
+
}
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
import { dialog, uid } from '@kerf-js/veneer-core'
|
|
2
|
+
import { derived, state, type State, untrack } from '@kerf-js/core'
|
|
3
|
+
import { use } from '@kerf-js/veneer-core/kerf'
|
|
4
|
+
import './styles'
|
|
5
|
+
import '@kerf-js/veneer-core/styles/components/button.css'
|
|
6
|
+
import '@kerf-js/veneer-core/styles/components/hamburger.css'
|
|
7
|
+
|
|
8
|
+
/** A top link in a HamburgerMenu. */
|
|
9
|
+
export interface HamburgerLink {
|
|
10
|
+
href: string
|
|
11
|
+
label: string
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/** Props for HamburgerMenu. */
|
|
15
|
+
export interface HamburgerMenuProps {
|
|
16
|
+
/** Names the button and the menu. */
|
|
17
|
+
label?: string
|
|
18
|
+
/** Pass a box to open or close it from outside. */
|
|
19
|
+
open?: State<boolean>
|
|
20
|
+
/** The site's main links, shown large. */
|
|
21
|
+
links?: HamburgerLink[]
|
|
22
|
+
/** The current page's href: its link is marked. */
|
|
23
|
+
current?: string
|
|
24
|
+
/** Shown at the top left of the open menu, usually the site's brand. */
|
|
25
|
+
brand?: unknown
|
|
26
|
+
/** More navigation under the links: sections, a filter, a footer. */
|
|
27
|
+
children?: unknown
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const Bars = () => (
|
|
31
|
+
<span data-part="bars" aria-hidden="true">
|
|
32
|
+
<i></i>
|
|
33
|
+
<i></i>
|
|
34
|
+
<i></i>
|
|
35
|
+
</span>
|
|
36
|
+
)
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* A site's main navigation on small screens: a button whose bars become an X, opening a full-screen menu over the
|
|
40
|
+
* page. The page behind stops scrolling, and pressing a link closes the menu while the route changes underneath.
|
|
41
|
+
* For a section's own pages beside the content, use SideNav.
|
|
42
|
+
*/
|
|
43
|
+
export function HamburgerMenu(props: HamburgerMenuProps) {
|
|
44
|
+
const id = uid('menu')
|
|
45
|
+
const open = untrack(() => props.open) ?? state(false)
|
|
46
|
+
let el: HTMLDialogElement | undefined
|
|
47
|
+
use(
|
|
48
|
+
dialog,
|
|
49
|
+
() => el,
|
|
50
|
+
() => ({ open: open.value, onClose: () => (open.value = false) }),
|
|
51
|
+
)
|
|
52
|
+
const show = (e: MouseEvent) => {
|
|
53
|
+
const r = (e.currentTarget as HTMLElement).getBoundingClientRect()
|
|
54
|
+
el?.style.setProperty('--_x', `${Math.round(r.left + r.width / 2)}px`)
|
|
55
|
+
el?.style.setProperty('--_y', `${Math.round(r.top + r.height / 2)}px`)
|
|
56
|
+
open.value = true
|
|
57
|
+
}
|
|
58
|
+
const follow = (e: MouseEvent) => {
|
|
59
|
+
if ((e.target as Element).closest?.('a[href]')) open.value = false
|
|
60
|
+
}
|
|
61
|
+
const label = derived(() => props.label ?? 'Menu')
|
|
62
|
+
return (
|
|
63
|
+
<>
|
|
64
|
+
<button
|
|
65
|
+
type="button"
|
|
66
|
+
data-scope="button"
|
|
67
|
+
data-emphasis="ghost"
|
|
68
|
+
data-tone="neutral"
|
|
69
|
+
data-icon-only=""
|
|
70
|
+
data-hamburger=""
|
|
71
|
+
aria-label={label.value}
|
|
72
|
+
aria-controls={id}
|
|
73
|
+
aria-expanded={open.value ? 'true' : 'false'}
|
|
74
|
+
onClick={show}
|
|
75
|
+
>
|
|
76
|
+
<Bars />
|
|
77
|
+
</button>
|
|
78
|
+
<dialog
|
|
79
|
+
ref={(n) => (el = n)}
|
|
80
|
+
id={id}
|
|
81
|
+
data-scope="menu-screen"
|
|
82
|
+
aria-label={label.value}
|
|
83
|
+
onClick={follow}
|
|
84
|
+
>
|
|
85
|
+
<div data-part="head">
|
|
86
|
+
<span data-part="brand">{props.brand}</span>
|
|
87
|
+
<button
|
|
88
|
+
type="button"
|
|
89
|
+
data-scope="button"
|
|
90
|
+
data-emphasis="ghost"
|
|
91
|
+
data-tone="neutral"
|
|
92
|
+
data-icon-only=""
|
|
93
|
+
data-hamburger=""
|
|
94
|
+
data-part="close"
|
|
95
|
+
data-close=""
|
|
96
|
+
aria-label="Close"
|
|
97
|
+
aria-expanded="true"
|
|
98
|
+
>
|
|
99
|
+
<Bars />
|
|
100
|
+
</button>
|
|
101
|
+
</div>
|
|
102
|
+
<nav data-part="body" aria-label={label.value}>
|
|
103
|
+
{props.links && (
|
|
104
|
+
<ul data-part="links">
|
|
105
|
+
{props.links.map((l) => (
|
|
106
|
+
<li key={l.href}>
|
|
107
|
+
<a href={l.href} aria-current={l.href === props.current ? 'page' : undefined}>
|
|
108
|
+
{l.label}
|
|
109
|
+
</a>
|
|
110
|
+
</li>
|
|
111
|
+
))}
|
|
112
|
+
</ul>
|
|
113
|
+
)}
|
|
114
|
+
{props.children}
|
|
115
|
+
</nav>
|
|
116
|
+
</dialog>
|
|
117
|
+
</>
|
|
118
|
+
)
|
|
119
|
+
}
|
package/src/Icon.tsx
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
// veneer's `icon` props: a name, or an icon from @kerf-js/veneer-icons. veneer's own fixed glyphs import their icon directly.
|
|
2
|
+
import './styles'
|
|
3
|
+
import { AlertIcon, AlertCircleIcon, CheckCircleIcon, InfoIcon } from '@kerf-js/veneer-icons'
|
|
4
|
+
|
|
5
|
+
/** `<svg data-icon aria-hidden focusable=false>`: decorative, never named (03 §7); `label` names it. */
|
|
6
|
+
export { Icon, type IconName, type IconRef } from '@kerf-js/veneer-icons/icon'
|
|
7
|
+
|
|
8
|
+
export const toneIcon = {
|
|
9
|
+
info: InfoIcon,
|
|
10
|
+
neutral: InfoIcon,
|
|
11
|
+
accent: InfoIcon,
|
|
12
|
+
positive: CheckCircleIcon,
|
|
13
|
+
caution: AlertIcon,
|
|
14
|
+
critical: AlertCircleIcon,
|
|
15
|
+
} as const
|
package/src/Kbd.tsx
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { flush, type State } from '@kerf-js/core'
|
|
2
|
+
import type { bindable } from '../bind'
|
|
3
|
+
import type { KeyValue } from '../KeyValueEditor'
|
|
4
|
+
|
|
5
|
+
type Refs = {
|
|
6
|
+
serial: number
|
|
7
|
+
body?: HTMLElement | undefined
|
|
8
|
+
timer?: ReturnType<typeof setTimeout> | undefined
|
|
9
|
+
}
|
|
10
|
+
export function keyValueActions({
|
|
11
|
+
held,
|
|
12
|
+
ids,
|
|
13
|
+
mode,
|
|
14
|
+
text,
|
|
15
|
+
refs,
|
|
16
|
+
toJson,
|
|
17
|
+
}: {
|
|
18
|
+
held: ReturnType<typeof bindable<KeyValue[]>>
|
|
19
|
+
ids: State<number[]>
|
|
20
|
+
mode: State<'form' | 'json'>
|
|
21
|
+
text: State<string>
|
|
22
|
+
refs: Refs
|
|
23
|
+
toJson: (rows: KeyValue[]) => string
|
|
24
|
+
}) {
|
|
25
|
+
const dup = (rows: KeyValue[], at: number) =>
|
|
26
|
+
!!rows[at]!.key && rows.findIndex((r) => r.key === rows[at]!.key) !== at
|
|
27
|
+
const hasDup = () => held.value.some((_, at) => dup(held.value, at))
|
|
28
|
+
const edit = (at: number, part: 'key' | 'value', v: string) =>
|
|
29
|
+
held.set(
|
|
30
|
+
held.value.map((r, n) => (n === at ? { ...r, [part]: v } : r)),
|
|
31
|
+
'keyboard',
|
|
32
|
+
)
|
|
33
|
+
const add = () => {
|
|
34
|
+
held.set([...held.value, { key: '', value: '' }], 'pointer')
|
|
35
|
+
ids.value = [...ids.value, ++refs.serial]
|
|
36
|
+
flush()
|
|
37
|
+
refs.body?.querySelector<HTMLElement>('tbody tr:last-child input')?.focus()
|
|
38
|
+
}
|
|
39
|
+
const remove = (at: number) => {
|
|
40
|
+
held.set(
|
|
41
|
+
held.value.filter((_, n) => n !== at),
|
|
42
|
+
'pointer',
|
|
43
|
+
)
|
|
44
|
+
ids.value = ids.value.filter((_, n) => n !== at)
|
|
45
|
+
flush()
|
|
46
|
+
const left = refs.body?.querySelectorAll<HTMLElement>('[data-part="remove"]')
|
|
47
|
+
;(
|
|
48
|
+
left?.[Math.min(at, (left?.length ?? 1) - 1)] ??
|
|
49
|
+
refs.body?.querySelector<HTMLElement>('[data-part="add"]')
|
|
50
|
+
)?.focus()
|
|
51
|
+
}
|
|
52
|
+
const flipMode = (m: 'form' | 'json') => {
|
|
53
|
+
if (m === 'json') text.value = toJson(held.value)
|
|
54
|
+
mode.value = m
|
|
55
|
+
}
|
|
56
|
+
const fromJson = (v: string) => {
|
|
57
|
+
text.value = v
|
|
58
|
+
clearTimeout(refs.timer)
|
|
59
|
+
refs.timer = setTimeout(() => {
|
|
60
|
+
try {
|
|
61
|
+
const o = JSON.parse(v)
|
|
62
|
+
if (!o || typeof o !== 'object' || Array.isArray(o)) return
|
|
63
|
+
const rows = Object.entries(o).map(([key, val]) => ({
|
|
64
|
+
key,
|
|
65
|
+
value: typeof val === 'string' ? val : JSON.stringify(val),
|
|
66
|
+
}))
|
|
67
|
+
held.set(rows, 'keyboard')
|
|
68
|
+
ids.value = rows.map(() => ++refs.serial)
|
|
69
|
+
} catch {
|
|
70
|
+
/* the editor shows the error; the last good value stands */
|
|
71
|
+
}
|
|
72
|
+
}, 250)
|
|
73
|
+
}
|
|
74
|
+
return { dup, hasDup, edit, add, remove, flipMode, fromJson }
|
|
75
|
+
}
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import { XIcon } from '@kerf-js/veneer-icons'
|
|
2
|
+
import { untrack, type State } from '@kerf-js/core'
|
|
3
|
+
import { Button } from '../Button'
|
|
4
|
+
import { Segmented } from '../Choice'
|
|
5
|
+
import type { bindable } from '../bind'
|
|
6
|
+
import type { KeyValue } from '../KeyValueEditor'
|
|
7
|
+
|
|
8
|
+
type Mode = 'form' | 'json'
|
|
9
|
+
export function formatSwitcher(
|
|
10
|
+
label: () => string,
|
|
11
|
+
mode: State<Mode>,
|
|
12
|
+
flipMode: (m: Mode) => void,
|
|
13
|
+
) {
|
|
14
|
+
return (
|
|
15
|
+
<Segmented
|
|
16
|
+
label={`${label()} format`}
|
|
17
|
+
size="sm"
|
|
18
|
+
value={mode.value}
|
|
19
|
+
options={[
|
|
20
|
+
{ value: 'form', label: 'Form' },
|
|
21
|
+
{ value: 'json', label: 'JSON' },
|
|
22
|
+
]}
|
|
23
|
+
onChange={(m) => flipMode(m)}
|
|
24
|
+
/>
|
|
25
|
+
)
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
type Rows = {
|
|
29
|
+
setBody: (n: HTMLDivElement) => void
|
|
30
|
+
keyLabel: string
|
|
31
|
+
valueLabel: string
|
|
32
|
+
ids: State<number[]>
|
|
33
|
+
held: ReturnType<typeof bindable<KeyValue[]>>
|
|
34
|
+
dup: (rows: KeyValue[], at: number) => boolean
|
|
35
|
+
edit: (at: number, part: 'key' | 'value', v: string) => void
|
|
36
|
+
add: () => void
|
|
37
|
+
remove: (at: number) => void
|
|
38
|
+
}
|
|
39
|
+
export function KeyValueRows({
|
|
40
|
+
setBody,
|
|
41
|
+
keyLabel,
|
|
42
|
+
valueLabel,
|
|
43
|
+
ids,
|
|
44
|
+
held,
|
|
45
|
+
dup,
|
|
46
|
+
edit,
|
|
47
|
+
add,
|
|
48
|
+
remove,
|
|
49
|
+
}: Rows) {
|
|
50
|
+
const rows = untrack(() => held)
|
|
51
|
+
return (
|
|
52
|
+
<div ref={setBody}>
|
|
53
|
+
<table>
|
|
54
|
+
<thead>
|
|
55
|
+
<tr>
|
|
56
|
+
<th scope="col">{keyLabel}</th>
|
|
57
|
+
<th scope="col">{valueLabel}</th>
|
|
58
|
+
<th scope="col">
|
|
59
|
+
<span data-part="sr-only">Remove</span>
|
|
60
|
+
</th>
|
|
61
|
+
</tr>
|
|
62
|
+
</thead>
|
|
63
|
+
<tbody>
|
|
64
|
+
{ids.value.map((rowId, at) => (
|
|
65
|
+
<tr key={rowId}>
|
|
66
|
+
<td>
|
|
67
|
+
<input
|
|
68
|
+
aria-label={`${keyLabel}, row ${at + 1}`}
|
|
69
|
+
value={rows.value[at]?.key ?? ''}
|
|
70
|
+
data-duplicate={rows.value[at] && dup(rows.value, at) ? '' : undefined}
|
|
71
|
+
onInput={(e: Event) =>
|
|
72
|
+
edit(at, 'key', (e.currentTarget as HTMLInputElement).value)
|
|
73
|
+
}
|
|
74
|
+
/>
|
|
75
|
+
</td>
|
|
76
|
+
<td>
|
|
77
|
+
<input
|
|
78
|
+
aria-label={`${valueLabel}, row ${at + 1}`}
|
|
79
|
+
value={rows.value[at]?.value ?? ''}
|
|
80
|
+
onInput={(e: Event) =>
|
|
81
|
+
edit(at, 'value', (e.currentTarget as HTMLInputElement).value)
|
|
82
|
+
}
|
|
83
|
+
/>
|
|
84
|
+
</td>
|
|
85
|
+
<td>
|
|
86
|
+
<button
|
|
87
|
+
type="button"
|
|
88
|
+
data-part="remove"
|
|
89
|
+
aria-label={`Remove row ${at + 1}`}
|
|
90
|
+
onClick={() => remove(at)}
|
|
91
|
+
>
|
|
92
|
+
<XIcon />
|
|
93
|
+
</button>
|
|
94
|
+
</td>
|
|
95
|
+
</tr>
|
|
96
|
+
))}
|
|
97
|
+
</tbody>
|
|
98
|
+
</table>
|
|
99
|
+
<Button emphasis="ghost" tone="neutral" size="sm" data-part="add" onClick={add}>
|
|
100
|
+
Add row
|
|
101
|
+
</Button>
|
|
102
|
+
</div>
|
|
103
|
+
)
|
|
104
|
+
}
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import { onCleanup, state, untrack } from '@kerf-js/core'
|
|
2
|
+
import { formReset } from '@kerf-js/veneer-core'
|
|
3
|
+
import { bindable, type Bindable } from './bind'
|
|
4
|
+
import { JsonEditor } from './CodeInput'
|
|
5
|
+
import { Field, type FieldShell } from './Field'
|
|
6
|
+
import { use } from '@kerf-js/veneer-core/kerf'
|
|
7
|
+
import { KeyValueRows, formatSwitcher } from './KeyValueEditor/rows'
|
|
8
|
+
import { keyValueActions } from './KeyValueEditor/actions'
|
|
9
|
+
import '@kerf-js/veneer-core/styles/rich/body.css'
|
|
10
|
+
import '@kerf-js/veneer-core/styles/rich/key-value.css'
|
|
11
|
+
|
|
12
|
+
/** One row of a KeyValueEditor. */
|
|
13
|
+
export type KeyValue = { key: string; value: string }
|
|
14
|
+
|
|
15
|
+
/** Props for KeyValueEditor. */
|
|
16
|
+
export type KeyValueEditorProps = FieldShell &
|
|
17
|
+
Bindable<KeyValue[]> & {
|
|
18
|
+
keyLabel?: string
|
|
19
|
+
valueLabel?: string
|
|
20
|
+
/** A Form ⇄ JSON switch; both keep the value. */
|
|
21
|
+
json?: boolean
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const toJson = (rows: KeyValue[]) =>
|
|
25
|
+
JSON.stringify(
|
|
26
|
+
Object.fromEntries(rows.filter((r) => r.key).map((r) => [r.key, r.value])),
|
|
27
|
+
null,
|
|
28
|
+
2,
|
|
29
|
+
)
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* A table of `key | value | remove` rows with Add row (04 KeyValueEditor). A duplicate key is a caution, not a block.
|
|
33
|
+
* With `json`, a Form ⇄ JSON switch shows the same value as a JsonEditor. Rows keep their identity, so typing never
|
|
34
|
+
* loses focus; adding focuses the new key, removing the row before.
|
|
35
|
+
*/
|
|
36
|
+
export function KeyValueEditor({
|
|
37
|
+
value,
|
|
38
|
+
defaultValue,
|
|
39
|
+
onValueChange,
|
|
40
|
+
keyLabel = 'Key',
|
|
41
|
+
valueLabel = 'Value',
|
|
42
|
+
json,
|
|
43
|
+
hint,
|
|
44
|
+
...rest
|
|
45
|
+
}: KeyValueEditorProps) {
|
|
46
|
+
const held = bindable<KeyValue[]>(
|
|
47
|
+
() => value,
|
|
48
|
+
() => defaultValue,
|
|
49
|
+
[],
|
|
50
|
+
() => onValueChange,
|
|
51
|
+
)
|
|
52
|
+
const refs: {
|
|
53
|
+
serial: number
|
|
54
|
+
body?: HTMLElement | undefined
|
|
55
|
+
timer?: ReturnType<typeof setTimeout> | undefined
|
|
56
|
+
} = { serial: 0 }
|
|
57
|
+
const ids = state(untrack(() => held.value.map(() => ++refs.serial)))
|
|
58
|
+
const mode = state<'form' | 'json'>('form')
|
|
59
|
+
const text = state('')
|
|
60
|
+
onCleanup(() => clearTimeout(refs.timer))
|
|
61
|
+
const { dup, hasDup, edit, add, remove, flipMode, fromJson } = keyValueActions({
|
|
62
|
+
held,
|
|
63
|
+
ids,
|
|
64
|
+
mode,
|
|
65
|
+
text,
|
|
66
|
+
refs,
|
|
67
|
+
toJson,
|
|
68
|
+
})
|
|
69
|
+
const switcher = untrack(() => json)
|
|
70
|
+
? formatSwitcher(() => rest.label, mode, flipMode)
|
|
71
|
+
: undefined
|
|
72
|
+
let root: HTMLDivElement | undefined
|
|
73
|
+
use(
|
|
74
|
+
formReset,
|
|
75
|
+
() => root,
|
|
76
|
+
() => () => {
|
|
77
|
+
clearTimeout(refs.timer)
|
|
78
|
+
held.reset()
|
|
79
|
+
ids.value = held.value.map(() => ++refs.serial)
|
|
80
|
+
text.value = toJson(held.value)
|
|
81
|
+
},
|
|
82
|
+
)
|
|
83
|
+
return (
|
|
84
|
+
<div ref={(n) => (root = n)} data-scope="kv">
|
|
85
|
+
{switcher}
|
|
86
|
+
{mode.value === 'json' ? (
|
|
87
|
+
<JsonEditor {...rest} value={text.value} onValueChange={fromJson} />
|
|
88
|
+
) : (
|
|
89
|
+
<Field
|
|
90
|
+
{...rest}
|
|
91
|
+
group
|
|
92
|
+
filled
|
|
93
|
+
hint={hasDup() ? 'That key is already here: the last one wins.' : hint}
|
|
94
|
+
attrs={{ 'data-multiline': '' }}
|
|
95
|
+
control={() => (
|
|
96
|
+
<KeyValueRows
|
|
97
|
+
setBody={(n) => (refs.body = n)}
|
|
98
|
+
keyLabel={keyLabel}
|
|
99
|
+
valueLabel={valueLabel}
|
|
100
|
+
ids={ids}
|
|
101
|
+
held={held}
|
|
102
|
+
dup={dup}
|
|
103
|
+
edit={edit}
|
|
104
|
+
add={add}
|
|
105
|
+
remove={remove}
|
|
106
|
+
/>
|
|
107
|
+
)}
|
|
108
|
+
/>
|
|
109
|
+
)}
|
|
110
|
+
</div>
|
|
111
|
+
)
|
|
112
|
+
}
|