@supatype/ui 0.9.0 → 0.9.2

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/CHANGELOG.md CHANGED
@@ -12,6 +12,16 @@
12
12
  `stringToRichTextDocument`, `normalizeRichTextDefault`) and the `SerializedEditorState` types stay
13
13
  in `@supatype/ui`.
14
14
 
15
+ ### Features
16
+
17
+ * **ui:** `Select`, `Textarea` and `Checkbox` form controls. Each wraps the native element, takes
18
+ `label` and `error` the way `Input` does, forwards its ref, and accepts the element's own props.
19
+ The label points at the control through an id from `React.useId()` unless you pass `id`, and an
20
+ `error` sets `aria-invalid` and adds the error text to `aria-describedby`.
21
+ * **ui:** `Table`, `THead`, `TBody`, `Tr`, `Th` and `Td`, styled parts that render the native table
22
+ elements, so screen readers get the table, row, column header and cell roles. They hold no data
23
+ logic: map your rows into them.
24
+
15
25
  ### Bug Fixes
16
26
 
17
27
  * **styles:** `@supatype/ui/styles.css` exists. The package exported it, but no build produced it,
package/README.md CHANGED
@@ -1,8 +1,8 @@
1
1
  # `@supatype/ui`
2
2
 
3
- React components for Supatype apps: `Button`, `Input`, `Card`, `Badge`, `Skeleton`, a
4
- `ThemeProvider`, and a Lexical rich text editor that reads and writes the `RichText` JSON a
5
- Supatype schema stores.
3
+ React components for Supatype apps: `Button`, the form controls `Input`, `Select`, `Textarea` and
4
+ `Checkbox`, `Card`, `Badge`, `Table`, `Skeleton`, a `ThemeProvider`, and a Lexical rich text editor
5
+ that reads and writes the `RichText` JSON a Supatype schema stores.
6
6
 
7
7
  ## Install
8
8
 
@@ -15,7 +15,7 @@ you use the editor (see [Rich text editor](#rich-text-editor)).
15
15
 
16
16
  ## Styling: Tailwind is required
17
17
 
18
- `Button`, `Input`, `Card`, `Badge` and `Skeleton` are styled with Tailwind utility classes from
18
+ Every component except the rich text editor is styled with Tailwind utility classes from
19
19
  the default palette (`bg-purple-600`, `text-neutral-700`, `dark:bg-neutral-900` and so on). The
20
20
  package ships those class names in its JavaScript, not as compiled CSS, so your app's Tailwind
21
21
  build has to see them. Without that step the components render unstyled.
@@ -96,8 +96,12 @@ From `@supatype/ui`:
96
96
  |---|---|
97
97
  | `Button`, `ButtonProps` | button with `variant` (`primary`, `secondary`, `ghost`, `danger`) and `size` (`sm`, `md`, `lg`) |
98
98
  | `Input`, `InputProps` | text input with optional `label` and `error` |
99
+ | `Select`, `SelectProps` | native `<select>` with optional `label` and `error`; pass `<option>` children |
100
+ | `Textarea`, `TextareaProps` | multi-line text input with optional `label` and `error` |
101
+ | `Checkbox`, `CheckboxProps` | native checkbox with its `label` beside it and optional `error` |
99
102
  | `Card`, `CardProps` | bordered container with `padding` (`sm`, `md`, `lg`) |
100
103
  | `Badge`, `BadgeProps` | pill label with `variant` (`default`, `success`, `warning`, `danger`, `purple`) |
104
+ | `Table`, `THead`, `TBody`, `Tr`, `Th`, `Td` | styled native table parts; props `TableProps`, `TableSectionProps`, `TrProps`, `ThProps`, `TdProps` |
101
105
  | `Skeleton` | pulsing loading placeholder |
102
106
  | `ThemeProvider`, `useTheme` | light, dark or system theme, saved in `localStorage` |
103
107
  | `emptyRichTextDocument` | an empty rich text document (one empty paragraph) |
@@ -108,6 +112,62 @@ From `@supatype/ui`:
108
112
 
109
113
  None of these import Lexical.
110
114
 
115
+ ## Form controls
116
+
117
+ `Input`, `Select`, `Textarea` and `Checkbox` wrap the native element, forward their ref to it and
118
+ accept all of its props, so they work with `value`/`onChange`, `defaultValue`, `name` and form
119
+ libraries that register through a ref. Each takes an optional `label` and `error`:
120
+
121
+ - With no `id`, the control gets a unique one from `React.useId()`, and the label points at it.
122
+ Pass `id` to choose your own.
123
+ - With an `error`, the control sets `aria-invalid` and adds the error text's id
124
+ (`<control id>-error`) to `aria-describedby`, keeping any `aria-describedby` you pass.
125
+
126
+ ```tsx
127
+ import { Checkbox, Input, Select, Textarea } from "@supatype/ui"
128
+
129
+ <Input label="Email" type="email" name="email" error={errors.email} />
130
+
131
+ <Select label="Role" name="role" defaultValue="member">
132
+ <option value="member">Member</option>
133
+ <option value="admin">Admin</option>
134
+ </Select>
135
+
136
+ <Textarea label="Bio" name="bio" rows={4} />
137
+
138
+ <Checkbox label="Send me product updates" name="updates" defaultChecked />
139
+ ```
140
+
141
+ `Select` is the browser's own `<select>`, so it keeps native keyboard handling, mobile pickers and
142
+ form submission. `Checkbox` puts its label to the right of the box; clicking either toggles it.
143
+
144
+ ## Table
145
+
146
+ `Table`, `THead`, `TBody`, `Tr`, `Th` and `Td` render `<table>`, `<thead>`, `<tbody>`, `<tr>`,
147
+ `<th>` and `<td>` with the package's borders and type scale, and accept each element's own props.
148
+ They fetch, sort and page nothing: map your rows into them.
149
+
150
+ ```tsx
151
+ import { Table, TBody, Td, Th, THead, Tr } from "@supatype/ui"
152
+
153
+ <Table aria-label="Users">
154
+ <THead>
155
+ <Tr>
156
+ <Th scope="col">Name</Th>
157
+ <Th scope="col">Email</Th>
158
+ </Tr>
159
+ </THead>
160
+ <TBody>
161
+ {users.map((user) => (
162
+ <Tr key={user.id}>
163
+ <Td>{user.name}</Td>
164
+ <Td>{user.email}</Td>
165
+ </Tr>
166
+ ))}
167
+ </TBody>
168
+ </Table>
169
+ ```
170
+
111
171
  ## Rich text editor
112
172
 
113
173
  `RichTextEditor` has its own entry point so that apps which never use it do not need Lexical:
@@ -0,0 +1,7 @@
1
+ import React from "react";
2
+ /** A native checkbox, so `checked`, `defaultChecked` and `onChange` work as they do on `<input>`. */
3
+ export interface CheckboxProps extends Omit<React.InputHTMLAttributes<HTMLInputElement>, "type"> {
4
+ label?: string;
5
+ error?: string;
6
+ }
7
+ export declare const Checkbox: React.ForwardRefExoticComponent<CheckboxProps & React.RefAttributes<HTMLInputElement>>;
@@ -0,0 +1,9 @@
1
+ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
+ import React from "react";
3
+ import { clsx } from "clsx";
4
+ import { FieldError, FieldLabel, useField } from "./field.js";
5
+ export const Checkbox = React.forwardRef(({ label, error, className, ...props }, ref) => {
6
+ const field = useField(props, { label, error });
7
+ return (_jsxs("div", { className: "flex flex-col gap-1.5", children: [_jsxs("div", { className: "flex items-center gap-2", children: [_jsx("input", { ref: ref, type: "checkbox", ...props, ...field.control, className: clsx("h-4 w-4 shrink-0 rounded accent-purple-600 disabled:opacity-50", "focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-purple-500 focus-visible:ring-offset-2", error && "outline outline-1 outline-offset-1 outline-red-500", className) }), _jsx(FieldLabel, { field: field })] }), _jsx(FieldError, { field: field })] }));
8
+ });
9
+ Checkbox.displayName = "Checkbox";
package/dist/Input.js CHANGED
@@ -1,16 +1,9 @@
1
- import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
1
+ import { jsx as _jsx } from "react/jsx-runtime";
2
2
  import React from "react";
3
3
  import { clsx } from "clsx";
4
- /** The caller's aria-describedby, with the error text's id added when there is an error. */
5
- function describedBy(callerValue, errorId) {
6
- const ids = [callerValue, errorId].filter((value) => value !== undefined && value !== "");
7
- return ids.length > 0 ? ids.join(" ") : undefined;
8
- }
9
- export const Input = React.forwardRef(({ label, error, className, id, "aria-describedby": ariaDescribedBy, "aria-invalid": ariaInvalid, ...props }, ref) => {
10
- // useId, not a slug of the label: two inputs labelled the same would share an id.
11
- const generatedId = React.useId();
12
- const inputId = id ?? generatedId;
13
- const errorId = error ? `${inputId}-error` : undefined;
14
- return (_jsxs("div", { className: "flex flex-col gap-1.5", children: [label && (_jsx("label", { htmlFor: inputId, className: "text-sm font-medium text-neutral-700 dark:text-neutral-300", children: label })), _jsx("input", { ref: ref, id: inputId, "aria-describedby": describedBy(ariaDescribedBy, errorId), "aria-invalid": ariaInvalid ?? (error ? true : undefined), className: clsx("h-10 rounded-lg border px-3 text-sm transition-colors", "border-neutral-300 bg-white text-neutral-900 placeholder:text-neutral-500", "dark:border-neutral-700 dark:bg-neutral-900 dark:text-neutral-100 dark:placeholder:text-neutral-500", "focus:border-purple-500 focus:outline-none focus:ring-1 focus:ring-purple-500", error && "border-red-500 focus:border-red-500 focus:ring-red-500", className), ...props }), error && _jsx("p", { id: errorId, className: "text-sm text-red-500", children: error })] }));
4
+ import { FIELD_ERROR, FIELD_FRAME, FieldLayout, useField } from "./field.js";
5
+ export const Input = React.forwardRef(({ label, error, className, ...props }, ref) => {
6
+ const field = useField(props, { label, error });
7
+ return (_jsx(FieldLayout, { field: field, children: _jsx("input", { ref: ref, ...props, ...field.control, className: clsx("h-10 px-3", FIELD_FRAME, error && FIELD_ERROR, className) }) }));
15
8
  });
16
9
  Input.displayName = "Input";
@@ -0,0 +1,7 @@
1
+ import React from "react";
2
+ /** A native `<select>`: pass `<option>` (and `<optgroup>`) elements as children. */
3
+ export interface SelectProps extends React.SelectHTMLAttributes<HTMLSelectElement> {
4
+ label?: string;
5
+ error?: string;
6
+ }
7
+ export declare const Select: React.ForwardRefExoticComponent<SelectProps & React.RefAttributes<HTMLSelectElement>>;
package/dist/Select.js ADDED
@@ -0,0 +1,9 @@
1
+ import { jsx as _jsx } from "react/jsx-runtime";
2
+ import React from "react";
3
+ import { clsx } from "clsx";
4
+ import { FIELD_ERROR, FIELD_FRAME, FieldLayout, useField } from "./field.js";
5
+ export const Select = React.forwardRef(({ label, error, className, children, ...props }, ref) => {
6
+ const field = useField(props, { label, error });
7
+ return (_jsx(FieldLayout, { field: field, children: _jsx("select", { ref: ref, ...props, ...field.control, className: clsx("h-10 px-3", FIELD_FRAME, error && FIELD_ERROR, className), children: children }) }));
8
+ });
9
+ Select.displayName = "Select";
@@ -0,0 +1,12 @@
1
+ import React from "react";
2
+ export type TableProps = React.TableHTMLAttributes<HTMLTableElement>;
3
+ export type TableSectionProps = React.HTMLAttributes<HTMLTableSectionElement>;
4
+ export type TrProps = React.HTMLAttributes<HTMLTableRowElement>;
5
+ export type ThProps = React.ThHTMLAttributes<HTMLTableCellElement>;
6
+ export type TdProps = React.TdHTMLAttributes<HTMLTableCellElement>;
7
+ export declare const Table: React.ForwardRefExoticComponent<TableProps & React.RefAttributes<HTMLTableElement>>;
8
+ export declare const THead: React.ForwardRefExoticComponent<TableSectionProps & React.RefAttributes<HTMLTableSectionElement>>;
9
+ export declare const TBody: React.ForwardRefExoticComponent<TableSectionProps & React.RefAttributes<HTMLTableSectionElement>>;
10
+ export declare const Tr: React.ForwardRefExoticComponent<TrProps & React.RefAttributes<HTMLTableRowElement>>;
11
+ export declare const Th: React.ForwardRefExoticComponent<ThProps & React.RefAttributes<HTMLTableCellElement>>;
12
+ export declare const Td: React.ForwardRefExoticComponent<TdProps & React.RefAttributes<HTMLTableCellElement>>;
package/dist/Table.js ADDED
@@ -0,0 +1,16 @@
1
+ // Table parts that render the native table elements, so each keeps its implicit ARIA role
2
+ // (table, rowgroup, row, columnheader, cell). No data fetching, sorting or paging: compose them.
3
+ import React from "react";
4
+ import { clsx } from "clsx";
5
+ /** One table part: the native `tag` with the base classes, the caller's className and its ref. */
6
+ function tablePart(tag, base, displayName) {
7
+ const Part = React.forwardRef(({ className, ...props }, ref) => React.createElement(tag, { ...props, ref, className: clsx(base, className) }));
8
+ Part.displayName = displayName;
9
+ return Part;
10
+ }
11
+ export const Table = tablePart("table", "w-full text-sm", "Table");
12
+ export const THead = tablePart("thead", "border-b border-neutral-200 bg-neutral-50 dark:border-neutral-800 dark:bg-neutral-800/40", "THead");
13
+ export const TBody = tablePart("tbody", "", "TBody");
14
+ export const Tr = tablePart("tr", "border-b border-neutral-200 last:border-0 dark:border-neutral-800", "Tr");
15
+ export const Th = tablePart("th", "whitespace-nowrap px-3 py-2 text-left text-xs font-medium uppercase tracking-wider text-neutral-500 dark:text-neutral-400", "Th");
16
+ export const Td = tablePart("td", "px-3 py-2 text-neutral-900 dark:text-neutral-100", "Td");
@@ -0,0 +1,6 @@
1
+ import React from "react";
2
+ export interface TextareaProps extends React.TextareaHTMLAttributes<HTMLTextAreaElement> {
3
+ label?: string;
4
+ error?: string;
5
+ }
6
+ export declare const Textarea: React.ForwardRefExoticComponent<TextareaProps & React.RefAttributes<HTMLTextAreaElement>>;
@@ -0,0 +1,9 @@
1
+ import { jsx as _jsx } from "react/jsx-runtime";
2
+ import React from "react";
3
+ import { clsx } from "clsx";
4
+ import { FIELD_ERROR, FIELD_FRAME, FieldLayout, useField } from "./field.js";
5
+ export const Textarea = React.forwardRef(({ label, error, className, ...props }, ref) => {
6
+ const field = useField(props, { label, error });
7
+ return (_jsx(FieldLayout, { field: field, children: _jsx("textarea", { ref: ref, ...props, ...field.control, className: clsx("min-h-20 px-3 py-2", FIELD_FRAME, error && FIELD_ERROR, className) }) }));
8
+ });
9
+ Textarea.displayName = "Textarea";
@@ -0,0 +1,45 @@
1
+ import React from "react";
2
+ type AriaInvalid = React.AriaAttributes["aria-invalid"];
3
+ /** The caller's props that the field wiring reads and then overrides on the control. */
4
+ export interface FieldAttrs {
5
+ id?: string | undefined;
6
+ "aria-describedby"?: string | undefined;
7
+ "aria-invalid"?: AriaInvalid | undefined;
8
+ }
9
+ /** The words around a control: its label and its error text. */
10
+ export interface FieldText {
11
+ label: string | undefined;
12
+ error: string | undefined;
13
+ }
14
+ /** A control's ids, and the attributes to spread on the native control after the caller's props. */
15
+ export interface Field extends FieldText {
16
+ errorId: string | undefined;
17
+ control: {
18
+ id: string;
19
+ "aria-describedby": string | undefined;
20
+ "aria-invalid": AriaInvalid | undefined;
21
+ };
22
+ }
23
+ /** The border, fill, text and focus ring every boxed control (Input, Select, Textarea) wears. */
24
+ export declare const FIELD_FRAME: string[];
25
+ /** Added to {@link FIELD_FRAME} while the control has an error. */
26
+ export declare const FIELD_ERROR = "border-red-500 focus:border-red-500 focus:ring-red-500";
27
+ /**
28
+ * Ids and ARIA for one control. useId, not a slug of the label: two controls labelled the same
29
+ * would share an id. A caller's `id` wins, and its aria-describedby is kept beside the error's.
30
+ */
31
+ export declare function useField(attrs: FieldAttrs, text: FieldText): Field;
32
+ /** The field's label, pointing at its control, or nothing when it has none. */
33
+ export declare function FieldLabel({ field }: {
34
+ field: Field;
35
+ }): React.ReactElement | null;
36
+ /** The field's error text, which the control names in aria-describedby, or nothing. */
37
+ export declare function FieldError({ field }: {
38
+ field: Field;
39
+ }): React.ReactElement | null;
40
+ /** A label over its control over the error text: the layout of Input, Select and Textarea. */
41
+ export declare function FieldLayout({ field, children }: {
42
+ field: Field;
43
+ children: React.ReactNode;
44
+ }): React.ReactElement;
45
+ export {};
package/dist/field.js ADDED
@@ -0,0 +1,44 @@
1
+ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
+ // Label, id and error wiring shared by the form controls (Input, Select, Textarea, Checkbox).
3
+ import React from "react";
4
+ /** The border, fill, text and focus ring every boxed control (Input, Select, Textarea) wears. */
5
+ export const FIELD_FRAME = [
6
+ "rounded-lg border text-sm transition-colors",
7
+ "border-neutral-300 bg-white text-neutral-900 placeholder:text-neutral-500",
8
+ "dark:border-neutral-700 dark:bg-neutral-900 dark:text-neutral-100 dark:placeholder:text-neutral-500",
9
+ "focus:border-purple-500 focus:outline-none focus:ring-1 focus:ring-purple-500",
10
+ ];
11
+ /** Added to {@link FIELD_FRAME} while the control has an error. */
12
+ export const FIELD_ERROR = "border-red-500 focus:border-red-500 focus:ring-red-500";
13
+ /** The caller's aria-describedby, with the error text's id added when there is an error. */
14
+ function describedBy(callerValue, errorId) {
15
+ const ids = [callerValue, errorId].filter((value) => value !== undefined && value !== "");
16
+ return ids.length > 0 ? ids.join(" ") : undefined;
17
+ }
18
+ /**
19
+ * Ids and ARIA for one control. useId, not a slug of the label: two controls labelled the same
20
+ * would share an id. A caller's `id` wins, and its aria-describedby is kept beside the error's.
21
+ */
22
+ export function useField(attrs, text) {
23
+ const generatedId = React.useId();
24
+ const id = attrs.id ?? generatedId;
25
+ const errorId = text.error ? `${id}-error` : undefined;
26
+ const invalid = attrs["aria-invalid"] ?? (text.error ? true : undefined);
27
+ return { ...text, errorId, control: { id, "aria-describedby": describedBy(attrs["aria-describedby"], errorId), "aria-invalid": invalid } };
28
+ }
29
+ /** The field's label, pointing at its control, or nothing when it has none. */
30
+ export function FieldLabel({ field }) {
31
+ if (!field.label)
32
+ return null;
33
+ return (_jsx("label", { htmlFor: field.control.id, className: "text-sm font-medium text-neutral-700 dark:text-neutral-300", children: field.label }));
34
+ }
35
+ /** The field's error text, which the control names in aria-describedby, or nothing. */
36
+ export function FieldError({ field }) {
37
+ if (!field.error)
38
+ return null;
39
+ return _jsx("p", { id: field.errorId, className: "text-sm text-red-500", children: field.error });
40
+ }
41
+ /** A label over its control over the error text: the layout of Input, Select and Textarea. */
42
+ export function FieldLayout({ field, children }) {
43
+ return (_jsxs("div", { className: "flex flex-col gap-1.5", children: [_jsx(FieldLabel, { field: field }), children, _jsx(FieldError, { field: field })] }));
44
+ }
package/dist/index.d.ts CHANGED
@@ -2,6 +2,14 @@ export { Button } from "./Button.js";
2
2
  export type { ButtonProps } from "./Button.js";
3
3
  export { Input } from "./Input.js";
4
4
  export type { InputProps } from "./Input.js";
5
+ export { Select } from "./Select.js";
6
+ export type { SelectProps } from "./Select.js";
7
+ export { Textarea } from "./Textarea.js";
8
+ export type { TextareaProps } from "./Textarea.js";
9
+ export { Checkbox } from "./Checkbox.js";
10
+ export type { CheckboxProps } from "./Checkbox.js";
11
+ export { Table, THead, TBody, Tr, Th, Td } from "./Table.js";
12
+ export type { TableProps, TableSectionProps, TrProps, ThProps, TdProps } from "./Table.js";
5
13
  export { Card } from "./Card.js";
6
14
  export type { CardProps } from "./Card.js";
7
15
  export { Badge } from "./Badge.js";
package/dist/index.js CHANGED
@@ -1,6 +1,10 @@
1
1
  // @supatype/ui: Shared component library
2
2
  export { Button } from "./Button.js";
3
3
  export { Input } from "./Input.js";
4
+ export { Select } from "./Select.js";
5
+ export { Textarea } from "./Textarea.js";
6
+ export { Checkbox } from "./Checkbox.js";
7
+ export { Table, THead, TBody, Tr, Th, Td } from "./Table.js";
4
8
  export { Card } from "./Card.js";
5
9
  export { Badge } from "./Badge.js";
6
10
  export { Skeleton } from "./Skeleton.js";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@supatype/ui",
3
- "version": "0.9.0",
3
+ "version": "0.9.2",
4
4
  "license": "MIT",
5
5
  "description": "Shared React components for Supatype apps, styled with Tailwind, plus a Lexical rich text editor",
6
6
  "type": "module",
@@ -58,7 +58,7 @@
58
58
  },
59
59
  "dependencies": {
60
60
  "clsx": "^2.1.0",
61
- "@supatype/types": "0.9.0"
61
+ "@supatype/types": "0.9.2"
62
62
  },
63
63
  "devDependencies": {
64
64
  "@lexical/link": "^0.43.0",
@@ -77,7 +77,7 @@
77
77
  "scripts": {
78
78
  "build": "tsc --project tsconfig.json && node scripts/copy-css.mjs",
79
79
  "typecheck": "tsc --project tsconfig.json --noEmit",
80
- "test": "node --test tests/package.test.mjs tests/input.test.mjs tests/rich-text-editor.test.mjs",
80
+ "test": "node --test tests/package.test.mjs tests/input.test.mjs tests/form-controls.test.mjs tests/table.test.mjs tests/rich-text-editor.test.mjs",
81
81
  "clean": "rm -rf dist *.tsbuildinfo"
82
82
  }
83
83
  }