|
| 1 | + |
| 2 | +# terminusdb-documents-ui |
| 3 | +SDK to build UI from terminusdb documents. This package includes a ``<FrameViewer/>`` component which displays a TerminusDB schema driven Form in three modes (Create/ Edit or View) |
| 4 | + |
| 5 | +## Installation |
| 6 | +Install the dependancy from npm |
| 7 | +```npm install @terminusdb/terminusdb-documents-ui``` |
| 8 | + |
| 9 | +## Usage |
| 10 | +Then import dependancy as shown below |
| 11 | +```import {FrameViewer} from '@terminusdb/terminusdb-documents-ui'``` |
| 12 | + |
| 13 | +## Run sandbox |
| 14 | + |
| 15 | +Run sandbox to get a demo on how to use ``<FrameViewer/>`` component. |
| 16 | +```npm run sandbox``` |
| 17 | +In this project, for the purpose of the demo Frames & Data are provided in the form of JSONs in ``src/lego.constants.js`` |
| 18 | + |
| 19 | +Otherwise frames can be retrieved by using TerminusDB Client. Refer [getSchemaFrame()](https://terminusdb.com/docs/guides/reference-guides/javascript-client-reference/woqlclient#getschemaframe) |
| 20 | + |
| 21 | + |
| 22 | +### Create |
| 23 | + |
| 24 | +``` |
| 25 | +import { FrameViewer } from '@terminusdb/terminusdb-documents-ui' |
| 26 | +import { LEGO_FRAMES } from "./lego.constants" |
| 27 | +
|
| 28 | +let frame = LEGO_FRAMES |
| 29 | +
|
| 30 | +// Search component to be displayed in UI when user wants to link a document. This is just a |
| 31 | +// dummy example where the user can choose 3 Ids from the below table. Users can load GraphQL |
| 32 | +// search component as well to search for documents. |
| 33 | +const Search = ({ setSelected }) => { |
| 34 | + function handleClick(e){ |
| 35 | + if(setSelected) setSelected({ id: e.target.id, label: e.target.name }) |
| 36 | + } |
| 37 | +
|
| 38 | + return <React.Fragment> |
| 39 | + Search this dummy result .... |
| 40 | + <Row id={"ID 1"} name="first id" onClick={handleClick}> |
| 41 | + {"ID 1"} |
| 42 | + </Row> |
| 43 | + <Row id={"ID 2"} name="second id" onClick={handleClick}> |
| 44 | + {"ID 2"} |
| 45 | + </Row> |
| 46 | + <Row id={"ID 3"} name="third id" onClick={handleClick}> |
| 47 | + {"ID 3"} |
| 48 | + </Row> |
| 49 | + </React.Fragment> |
| 50 | +} |
| 51 | +
|
| 52 | +/** |
| 53 | +* @param {*} data extracted from Form on click of Submit button |
| 54 | +* function to handle on Submit of form |
| 55 | +*/ |
| 56 | +function handleSubmit(data) { |
| 57 | + console.log(`Submitted data - ${data}`) |
| 58 | +} |
| 59 | +
|
| 60 | +return <FrameViewer |
| 61 | + frame={frame} // frames |
| 62 | + mode={"Create"} // mode in which to display the form |
| 63 | + onSelect={<Search/>} // displays a Search component |
| 64 | + formData={{}} // instance data - empty in create mode |
| 65 | + onSubmit={handleSubmit} // Callback submit function |
| 66 | + type={"Theme"}/> // type of document to display in form |
| 67 | +``` |
| 68 | + |
| 69 | + |
| 70 | +### Edit |
| 71 | + |
| 72 | +``` |
| 73 | +import { FrameViewer } from '@terminusdb/terminusdb-documents-ui' |
| 74 | +import { LEGO_FRAMES, THEME_DATA } from "./lego.constants" |
| 75 | +
|
| 76 | +let frame = LEGO_FRAMES, data = THEME_DATA // filled data to display in Edit mode |
| 77 | +
|
| 78 | +// Search component to be displayed in UI when user wants to link a document. This is just a |
| 79 | +// dummy example where the user can choose 3 Ids from the below table. Users can load GraphQL |
| 80 | +// search component as well to search for documents. |
| 81 | +const Search = ({ setSelected }) => { |
| 82 | + function handleClick(e){ |
| 83 | + if(setSelected) setSelected({ id: e.target.id, label: e.target.name }) |
| 84 | + } |
| 85 | +
|
| 86 | + return <React.Fragment> |
| 87 | + Search this dummy result .... |
| 88 | + <Row id={"ID 1"} name="first id" onClick={handleClick}> |
| 89 | + {"ID 1"} |
| 90 | + </Row> |
| 91 | + <Row id={"ID 2"} name="second id" onClick={handleClick}> |
| 92 | + {"ID 2"} |
| 93 | + </Row> |
| 94 | + <Row id={"ID 3"} name="third id" onClick={handleClick}> |
| 95 | + {"ID 3"} |
| 96 | + </Row> |
| 97 | + </React.Fragment> |
| 98 | +} |
| 99 | +
|
| 100 | +/** |
| 101 | +* @param {*} data extracted from Form on click of Submit button |
| 102 | +* function to handle on Submit of form |
| 103 | +*/ |
| 104 | +function handleSubmit(data) { |
| 105 | + console.log(`Submitted data - ${data}`) |
| 106 | +} |
| 107 | +
|
| 108 | +/** |
| 109 | +* |
| 110 | +* @param {*} clicked callback function which returns back the ID of element clicked |
| 111 | +* function which handles click on a document link. On click the function will |
| 112 | +* return back document ID clicked. |
| 113 | +*/ |
| 114 | +function handleTraverse (clicked) { |
| 115 | + alert(`You have clicked on document ID ${clicked}`) |
| 116 | +} |
| 117 | +
|
| 118 | +return <FrameViewer |
| 119 | + frame={frame} // frames |
| 120 | + mode={"Edit"} // mode in which to display the form |
| 121 | + onTraverse={handleTraverse} // Callback traverse links function |
| 122 | + onSelect={<Search/>} // displays a Search component |
| 123 | + formData={data} // instance data |
| 124 | + onSubmit={handleSubmit} // Callback submit function |
| 125 | + type={"Theme"}/> // type of document to display in form |
| 126 | +``` |
| 127 | + |
| 128 | + |
| 129 | +### View |
| 130 | + |
| 131 | +``` |
| 132 | +import { FrameViewer } from '@terminusdb/terminusdb-documents-ui' |
| 133 | +import { LEGO_FRAMES, THEME_DATA } from "./lego.constants" |
| 134 | +
|
| 135 | +let frame = LEGO_FRAMES, data = THEME_DATA // filled data to display in Edit mode |
| 136 | +
|
| 137 | +/** |
| 138 | +* |
| 139 | +* @param {*} clicked callback function which returns back the ID of element clicked |
| 140 | +* function which handles click on a document link. On click the function will |
| 141 | +* return back document ID clicked. |
| 142 | +*/ |
| 143 | +function handleTraverse (clicked) { |
| 144 | + alert(`You have clicked on document ID ${clicked}`) |
| 145 | +} |
| 146 | +
|
| 147 | +return <FrameViewer |
| 148 | + frame={frame} // frames |
| 149 | + mode={"View"} // mode in which to display the form |
| 150 | + onTraverse={handleTraverse} // Callback traverse links function |
| 151 | + formData={data} // instance data |
| 152 | + type={"Theme"}/> // type of document to display in form |
| 153 | +``` |
| 154 | +### Theme Selector |
| 155 | +FrameViewer is based on [Bootswatch](https://bootswatch.com/cosmo/) Themes. Use props ``theme`` in ``<FrameViewer/>`` component to change themes at an application level. |
| 156 | +``` |
| 157 | +import { FrameViewer } from '@terminusdb/terminusdb-documents-ui' |
| 158 | +return <FrameViewer |
| 159 | + frame={frame} // frames |
| 160 | + mode={"View"} // mode in which to display the form |
| 161 | + onTraverse={handleTraverse} // Callback traverse links function |
| 162 | + formData={data} // instance data |
| 163 | + theme="darkly" // pass a bootswatch theme - like darkly/ pulse |
| 164 | + type={"Theme"}/> // type of document to display in form |
| 165 | +``` |
| 166 | + |
| 167 | +A Theme selector can also be enabled by passing ``showThemeSelector={true}`` |
| 168 | +``` |
| 169 | +import { FrameViewer } from '@terminusdb/terminusdb-documents-ui' |
| 170 | +return <FrameViewer |
| 171 | + frame={frame} // frames |
| 172 | + mode={"View"} // mode in which to display the form |
| 173 | + onTraverse={handleTraverse} // Callback traverse links function |
| 174 | + formData={data} // instance data |
| 175 | + showThemeSelector={true} // use the selector to swap themes |
| 176 | + type={"Theme"}/> // type of document to display in form |
| 177 | +``` |
| 178 | + |
| 179 | +### CSS |
| 180 | +If you dont want to use themes at an application level then import CSS to make the ``<FrameViewer/>`` component appear in dark or light mode. |
| 181 | + |
| 182 | +#### Light mode |
| 183 | +``` |
| 184 | +import { FrameViewer } from '@terminusdb/terminusdb-documents-ui' |
| 185 | +import "terminusdb__light.css" |
| 186 | +return <FrameViewer |
| 187 | + frame={frame} // frames |
| 188 | + mode={"View"} // mode in which to display the form |
| 189 | + onTraverse={handleTraverse} // Callback traverse links function |
| 190 | + formData={data} // instance data |
| 191 | + type={"Theme"}/> // type of document to display in form |
| 192 | +``` |
| 193 | + |
| 194 | +#### Dark mode |
| 195 | +``` |
| 196 | +import { FrameViewer } from '@terminusdb/terminusdb-documents-ui' |
| 197 | +import "terminusdb__darkly.css" |
| 198 | +return <FrameViewer |
| 199 | + frame={frame} // frames |
| 200 | + mode={"View"} // mode in which to display the form |
| 201 | + onTraverse={handleTraverse} // Callback traverse links function |
| 202 | + formData={data} // instance data |
| 203 | + type={"Theme"}/> // type of document to display in form |
| 204 | +``` |
0 commit comments