|
1 | 1 | # MobX RESTful Shadcn |
2 | 2 |
|
3 | | -A **Pagination Table** & **Scroll List** component suite for [CRUD operation][1], which is based on [MobX RESTful][2] & [React][3]. |
| 3 | +A **Pagination Table** & **Scroll List** component suite for [CRUD operation][1], which is based on [MobX RESTful][2], [React][3] & [Shadcn UI][4]. |
4 | 4 |
|
5 | | -You can use the `shadcn` CLI to run your own component registry. Running your own |
6 | | -component registry allows you to distribute your custom components, hooks, pages, and |
7 | | -other files to any React project. |
| 5 | +[][5] |
| 6 | +[][6] |
| 7 | +[][7] |
8 | 8 |
|
9 | | -> [!IMPORTANT] |
10 | | -> This template uses Tailwind v4. For Tailwind v3, see [registry-template-v3](https://github.com/shadcn-ui/registry-template-v3). |
| 9 | +## Components |
11 | 10 |
|
12 | | -## Getting Started |
| 11 | +1. [Badge Bar](https://mobx-restful-shadcn.idea2.app/) |
| 12 | +2. [Badge Input](https://mobx-restful-shadcn.idea2.app/) |
| 13 | +3. [Image Preview](https://mobx-restful-shadcn.idea2.app/) |
| 14 | +4. [File Preview](https://mobx-restful-shadcn.idea2.app/) |
| 15 | +5. [File Picker](https://mobx-restful-shadcn.idea2.app/) |
| 16 | +6. [File Uploader](https://mobx-restful-shadcn.idea2.app/) |
| 17 | +7. [Form Field](https://mobx-restful-shadcn.idea2.app/) |
| 18 | +8. [Range Input](https://mobx-restful-shadcn.idea2.app/) |
| 19 | +9. [Array Field](https://mobx-restful-shadcn.idea2.app/) |
| 20 | +10. [REST Form](https://mobx-restful-shadcn.idea2.app/) |
| 21 | +11. [REST Form Modal](https://mobx-restful-shadcn.idea2.app/) |
| 22 | +12. [Pager](https://mobx-restful-shadcn.idea2.app/) |
| 23 | +13. [REST Table](https://mobx-restful-shadcn.idea2.app/) |
| 24 | +14. [Scroll Boundary](https://mobx-restful-shadcn.idea2.app/) |
| 25 | +15. [Scroll List](https://mobx-restful-shadcn.idea2.app/) |
| 26 | +16. [Searchable Input](https://mobx-restful-shadcn.idea2.app/) |
13 | 27 |
|
14 | | -This is a template for creating a custom registry using Next.js. |
| 28 | +## Installation |
15 | 29 |
|
16 | | -- The template uses a `registry.json` file to define components and their files. |
17 | | -- The `shadcn build` command is used to build the registry. |
18 | | -- The registry items are served as static files under `public/r/[name].json`. |
19 | | -- The template also includes a route handler for serving registry items. |
20 | | -- Every registry item are compatible with the `shadcn` CLI. |
21 | | -- We have also added v0 integration using the `Open in v0` api. |
| 30 | +```shell |
| 31 | +npx shadcn-helper add https://mobx-restful-shadcn.idea2.app/r/rest-table.json |
| 32 | +``` |
| 33 | + |
| 34 | +Replace `rest-table` with any component name from the list above. |
| 35 | + |
| 36 | +## Configuration |
| 37 | + |
| 38 | +### Internationalization |
| 39 | + |
| 40 | +Set up i18n translation model for UI text: |
| 41 | + |
| 42 | +```typescript |
| 43 | +import { TranslationModel } from "mobx-i18n"; |
| 44 | +import { IDType } from "mobx-restful"; |
| 45 | + |
| 46 | +export const i18n = new TranslationModel({ |
| 47 | + en_US: { |
| 48 | + load_more: "Load more", |
| 49 | + no_more: "No more", |
| 50 | + create: "Create", |
| 51 | + view: "View", |
| 52 | + submit: "Submit", |
| 53 | + cancel: "Cancel", |
| 54 | + edit: "Edit", |
| 55 | + delete: "Delete", |
| 56 | + total_x_rows: ({ totalCount }: { totalCount: number }) => |
| 57 | + `Total ${totalCount} rows`, |
| 58 | + sure_to_delete_x: ({ keys }: { keys: IDType[] }) => |
| 59 | + `Are you sure to delete ${keys.join(", ")}?`, |
| 60 | + }, |
| 61 | +}); |
| 62 | +``` |
| 63 | + |
| 64 | +### Data Source |
| 65 | + |
| 66 | +Set up HTTP client and implement Model class: |
| 67 | + |
| 68 | +```typescript |
| 69 | +import { githubClient, RepositoryModel } from "mobx-github"; |
| 70 | + |
| 71 | +const GITHUB_TOKEN = process.env.GITHUB_TOKEN; |
| 72 | + |
| 73 | +githubClient.use(({ request }, next) => { |
| 74 | + if (GITHUB_TOKEN) |
| 75 | + request.headers = { |
| 76 | + ...request.headers, |
| 77 | + Authorization: `Bearer ${GITHUB_TOKEN}`, |
| 78 | + }; |
| 79 | + return next(); |
| 80 | +}); |
| 81 | + |
| 82 | +export const repositoryStore = new RepositoryModel("idea2app"); |
| 83 | +``` |
| 84 | + |
| 85 | +## Usage |
| 86 | + |
| 87 | +### Pagination Table |
| 88 | + |
| 89 | +```tsx |
| 90 | +import { computed } from "mobx"; |
| 91 | +import { observer } from "mobx-react"; |
| 92 | +import { Component } from "react"; |
| 93 | + |
| 94 | +import { BadgeBar } from "@/components/ui/badge-bar"; |
| 95 | +import { RestTable, Column } from "@/components/ui/rest-table"; |
| 96 | +import repositoryStore, { Repository } from "@/models/Repository"; |
| 97 | +import { i18n } from "@/models/Translation"; |
| 98 | + |
| 99 | +@observer |
| 100 | +export class RepositoryTable extends Component { |
| 101 | + @computed |
| 102 | + get columns() { |
| 103 | + return [ |
| 104 | + { |
| 105 | + key: "full_name", |
| 106 | + renderHead: "Repository Name", |
| 107 | + renderBody: ({ html_url, full_name }) => ( |
| 108 | + <a target="_blank" href={html_url}> |
| 109 | + {full_name} |
| 110 | + </a> |
| 111 | + ), |
| 112 | + required: true, |
| 113 | + minLength: 3, |
| 114 | + invalidMessage: "Input 3 characters at least", |
| 115 | + }, |
| 116 | + { key: "homepage", type: "url", renderHead: "Home Page" }, |
| 117 | + { key: "language", renderHead: "Programming Language" }, |
| 118 | + { |
| 119 | + key: "topics", |
| 120 | + renderHead: "Topic", |
| 121 | + renderBody: ({ topics }) => ( |
| 122 | + <BadgeBar |
| 123 | + list={(topics || []).map((text) => ({ |
| 124 | + text, |
| 125 | + link: `https://github.com/topics/${text}`, |
| 126 | + }))} |
| 127 | + /> |
| 128 | + ), |
| 129 | + }, |
| 130 | + { key: "stargazers_count", type: "number", renderHead: "Star Count" }, |
| 131 | + { key: "description", renderHead: "Description", rows: 3 }, |
| 132 | + ]; |
| 133 | + } |
| 134 | + |
| 135 | + render() { |
| 136 | + return ( |
| 137 | + <RestTable |
| 138 | + editable |
| 139 | + deletable |
| 140 | + columns={this.columns} |
| 141 | + store={repositoryStore} |
| 142 | + translator={i18n} |
| 143 | + onCheck={console.log} |
| 144 | + /> |
| 145 | + ); |
| 146 | + } |
| 147 | +} |
| 148 | +``` |
| 149 | + |
| 150 | +### Scroll List |
| 151 | + |
| 152 | +```tsx |
| 153 | +import { observer } from "mobx-react"; |
| 154 | + |
| 155 | +import { ScrollList } from "@/components/ui/scroll-list"; |
| 156 | +import repositoryStore from "@/models/Repository"; |
| 157 | +import { i18n } from "@/models/Translation"; |
| 158 | + |
| 159 | +export const ScrollListExample = () => ( |
| 160 | + <ScrollList |
| 161 | + translator={i18n} |
| 162 | + store={repositoryStore} |
| 163 | + renderList={(allItems) => ( |
| 164 | + <ul className="grid grid-cols-1 md:grid-cols-2 gap-4"> |
| 165 | + {allItems.map(({ id, name, description }) => ( |
| 166 | + <li key={id} className="p-4 border rounded"> |
| 167 | + <h3>{name}</h3> |
| 168 | + <p>{description}</p> |
| 169 | + </li> |
| 170 | + ))} |
| 171 | + </ul> |
| 172 | + )} |
| 173 | + /> |
| 174 | +); |
| 175 | +``` |
| 176 | + |
| 177 | +### File Uploader |
| 178 | + |
| 179 | +```tsx |
| 180 | +import { FileModel, FileUploader } from "@/components/ui/file-uploader"; |
| 181 | + |
| 182 | +class MyFileModel extends FileModel {} |
| 183 | + |
| 184 | +const store = new MyFileModel(); |
| 185 | + |
| 186 | +export const EditorPage = () => ( |
| 187 | + <FileUploader |
| 188 | + store={store} |
| 189 | + accept="image/*" |
| 190 | + name="images" |
| 191 | + multiple |
| 192 | + required |
| 193 | + onChange={console.log} |
| 194 | + /> |
| 195 | +); |
| 196 | +``` |
| 197 | + |
| 198 | +## Development |
| 199 | + |
| 200 | +This is a custom component registry built with Next.js and compatible with the `shadcn` CLI. |
| 201 | + |
| 202 | +### Getting Started |
| 203 | + |
| 204 | +1. Clone the repository |
| 205 | +2. Install dependencies: `pnpm install` |
| 206 | +3. Run development server: `pnpm dev` |
| 207 | +4. Build registry: `pnpm registry:build` |
| 208 | +5. Build project: `pnpm build` |
| 209 | + |
| 210 | +### Registry Structure |
| 211 | + |
| 212 | +- The `registry.json` file defines all components and their files |
| 213 | +- Components are located in `registry/new-york/blocks/` |
| 214 | +- Each component has its implementation and example files |
| 215 | +- The `shadcn build` command generates registry items in `public/r/` |
22 | 216 |
|
23 | 217 | ## Documentation |
24 | 218 |
|
25 | | -Visit the [shadcn documentation](https://ui.shadcn.com/docs/registry) to view the full documentation. |
| 219 | +- [Shadcn UI Documentation](https://ui.shadcn.com/docs) |
| 220 | +- [Component Registry Documentation](https://ui.shadcn.com/docs/registry) |
| 221 | +- [MobX RESTful Documentation][2] |
26 | 222 |
|
27 | 223 | [1]: https://en.wikipedia.org/wiki/Create,_read,_update_and_delete |
28 | 224 | [2]: https://github.com/idea2app/MobX-RESTful |
29 | 225 | [3]: https://reactjs.org/ |
| 226 | +[4]: https://ui.shadcn.com/ |
| 227 | +[5]: https://mobx.js.org/ |
| 228 | +[6]: https://libraries.io/npm/mobx-restful-shadcn |
| 229 | +[7]: https://github.com/idea2app/MobX-RESTful-Shadcn/actions/workflows/main.yml |
0 commit comments