diff --git a/packages/playground/admin/app/components/navigation.tsx b/packages/playground/admin/app/components/navigation.tsx index 39080ce63..ad3658645 100644 --- a/packages/playground/admin/app/components/navigation.tsx +++ b/packages/playground/admin/app/components/navigation.tsx @@ -61,6 +61,9 @@ export const Navigation = () => { + } label={'Custom components'}> + + ) diff --git a/packages/playground/admin/app/pages/custom.tsx b/packages/playground/admin/app/pages/custom.tsx new file mode 100644 index 000000000..90b342239 --- /dev/null +++ b/packages/playground/admin/app/pages/custom.tsx @@ -0,0 +1,41 @@ +import { Component, EntitySubTree, Field, SugaredRelativeSingleField, useField } from '@contember/interface' +import { Binding, PersistButton } from '../../lib/components/binding' +import * as React from 'react' +import { Slots } from '../../lib/components/slots' +import { Button } from '../../lib/components/ui/button' + +export const input = () => ( + + + + + + +) + +type MyComponentProps = { + field: SugaredRelativeSingleField['field'] +} + +const UseFieldComponent = Component( + ({ field }) => { + // The `useField` hook is used to get and update the field value from data-binding. + const { value, updateValue } = useField(field) + const increment = () => updateValue((value ?? 0) + 1) + const decrement = () => updateValue((value ?? 0) - 1) + + return ( +
+ +
+
{value}
+
+ +
+ ) + }, + // When using hooks in the render function above, specify `staticRender` as the second argument to `Component` to ensure all data is properly registered. + ({ field }) => ( + + ), +) diff --git a/packages/playground/admin/app/pages/grid.tsx b/packages/playground/admin/app/pages/grid.tsx index 297b487f9..985986973 100644 --- a/packages/playground/admin/app/pages/grid.tsx +++ b/packages/playground/admin/app/pages/grid.tsx @@ -279,7 +279,7 @@ const CustomGridRow = Component(() => (
- accessor.length} /> +
diff --git a/packages/playground/api/migrations/2024-06-10-144526-grid-article-view.json b/packages/playground/api/migrations/2024-06-10-144526-grid-article-view.json new file mode 100644 index 000000000..77766071f --- /dev/null +++ b/packages/playground/api/migrations/2024-06-10-144526-grid-article-view.json @@ -0,0 +1,106 @@ +{ + "formatVersion": 5, + "modifications": [ + { + "modification": "createView", + "entity": { + "eventLog": { + "enabled": true + }, + "name": "GridArticleDetail", + "primary": "id", + "primaryColumn": "id", + "tableName": "grid_article_detail", + "fields": { + "id": { + "name": "id", + "columnName": "id", + "columnType": "uuid", + "nullable": false, + "type": "Uuid" + }, + "commentsCount": { + "name": "commentsCount", + "columnName": "comments_count", + "columnType": "integer", + "nullable": false, + "type": "Integer" + }, + "article": { + "type": "OneHasOne", + "name": "article", + "target": "GridArticle", + "joiningColumn": { + "columnName": "article_id", + "onDelete": "restrict" + }, + "nullable": false, + "inversedBy": "details" + } + }, + "unique": [], + "indexes": [], + "view": { + "sql": "\n\tSELECT\n\t\tarticle.id AS id,\n\t\tarticle.id AS article_id,\n\t\t(SELECT COUNT(*)\n\t\t FROM \"grid_article_comment\" comment\n\t\t WHERE comment.article_id = article.id) AS comments_count\n\tFROM \"grid_article\" article;\n" + } + } + }, + { + "modification": "createRelationInverseSide", + "entityName": "GridArticle", + "relation": { + "type": "OneHasOne", + "name": "details", + "target": "GridArticleDetail", + "ownedBy": "article", + "nullable": true + } + }, + { + "modification": "patchAclSchema", + "patch": [ + { + "op": "add", + "path": "/roles/admin/entities/GridArticleDetail", + "value": { + "predicates": {}, + "operations": { + "read": { + "id": true, + "article": true, + "commentsCount": true + }, + "create": { + "id": true, + "article": true, + "commentsCount": true + }, + "update": { + "id": true, + "article": true, + "commentsCount": true + }, + "delete": true, + "customPrimary": true + } + } + }, + { + "op": "add", + "path": "/roles/admin/entities/GridArticle/operations/read/details", + "value": true + }, + { + "op": "add", + "path": "/roles/admin/entities/GridArticle/operations/create/details", + "value": true + }, + { + "op": "add", + "path": "/roles/admin/entities/GridArticle/operations/update/details", + "value": true + } + ] + } + ] +} diff --git a/packages/playground/api/model/Grid.ts b/packages/playground/api/model/Grid.ts index 9b8670245..02e3c4ea8 100644 --- a/packages/playground/api/model/Grid.ts +++ b/packages/playground/api/model/Grid.ts @@ -14,6 +14,7 @@ export class GridArticle { tags = c.manyHasMany(GridTag) views = c.intColumn() comments = c.oneHasMany(GridArticleComment, 'article') + details = c.oneHasOneInverse(GridArticleDetail, 'article') } export class GridTag { @@ -37,3 +38,17 @@ export class GridArticleComment { content = c.stringColumn() createdAt = c.dateTimeColumn() } + +@c.View(` + SELECT + article.id AS id, + article.id AS article_id, + (SELECT COUNT(*) + FROM "grid_article_comment" comment + WHERE comment.article_id = article.id) AS comments_count + FROM "grid_article" article; +`) +export class GridArticleDetail { + article = c.oneHasOne(GridArticle, 'details').notNull() + commentsCount = c.intColumn().notNull() +}