forked from tsai/budibase
7 changed files with 254 additions and 0 deletions
@ -0,0 +1,99 @@ |
|||||
|
<script> |
||||
|
import { getContext } from "svelte" |
||||
|
import { BlockBuilder } from "./block-builder" |
||||
|
import Component from "components/Component.svelte" |
||||
|
|
||||
|
// Common props |
||||
|
export let searchColumns |
||||
|
export let dataSource |
||||
|
|
||||
|
// Data provider props |
||||
|
export let filter |
||||
|
export let sortColumn |
||||
|
export let sortOrder |
||||
|
export let paginate |
||||
|
|
||||
|
// Table props |
||||
|
export let tableColumns |
||||
|
export let showAutoColumns |
||||
|
export let rowCount |
||||
|
export let quiet |
||||
|
export let size |
||||
|
|
||||
|
const { styleable } = getContext("sdk") |
||||
|
const component = getContext("component") |
||||
|
|
||||
|
let instance |
||||
|
$: { |
||||
|
const builder = new BlockBuilder($component) |
||||
|
|
||||
|
const form = builder.createComponent("form", { |
||||
|
dataSource, |
||||
|
}) |
||||
|
|
||||
|
let newFilter = [...(filter || [])] |
||||
|
|
||||
|
// Add search bar if required |
||||
|
if (searchColumns?.length) { |
||||
|
const searchContainer = builder |
||||
|
.createComponent("container", { |
||||
|
direction: "row", |
||||
|
hAlign: "right", |
||||
|
vAlign: "middle", |
||||
|
gap: "M", |
||||
|
wrap: true, |
||||
|
}) |
||||
|
.setStyles({ |
||||
|
"margin-bottom": "20px", |
||||
|
}) |
||||
|
|
||||
|
searchColumns?.forEach(column => { |
||||
|
const field = builder.createComponent("stringfield", { |
||||
|
field: column, |
||||
|
placeholder: column, |
||||
|
label: column, |
||||
|
}) |
||||
|
|
||||
|
searchContainer.addChild(field) |
||||
|
|
||||
|
newFilter.push({ |
||||
|
field: column, |
||||
|
operator: "equal", |
||||
|
type: "string", |
||||
|
valueType: "Binding", |
||||
|
value: `{{ [${form.id}].[${column}] }}`, |
||||
|
}) |
||||
|
}) |
||||
|
|
||||
|
form.addChild(searchContainer) |
||||
|
} |
||||
|
|
||||
|
const dataProvider = builder.createComponent("dataprovider", { |
||||
|
dataSource, |
||||
|
filter: newFilter, |
||||
|
sortColumn, |
||||
|
sortOrder, |
||||
|
paginate, |
||||
|
limit: rowCount, |
||||
|
}) |
||||
|
|
||||
|
const table = builder.createComponent("table", { |
||||
|
dataProvider: `{{ literal [${dataProvider.id}] }}`, |
||||
|
columns: tableColumns, |
||||
|
showAutoColumns, |
||||
|
rowCount, |
||||
|
quiet, |
||||
|
size, |
||||
|
}) |
||||
|
|
||||
|
dataProvider.addChild(table) |
||||
|
|
||||
|
form.addChild(dataProvider) |
||||
|
instance = form.build() |
||||
|
console.log("new instance") |
||||
|
} |
||||
|
</script> |
||||
|
|
||||
|
<div use:styleable={$component.styles}> |
||||
|
<Component {instance} /> |
||||
|
</div> |
||||
@ -0,0 +1,14 @@ |
|||||
|
import { ComponentBuilder } from "./component-builder" |
||||
|
|
||||
|
export class BlockBuilder { |
||||
|
context |
||||
|
componentCount = 0 |
||||
|
|
||||
|
constructor(context) { |
||||
|
this.context = context |
||||
|
} |
||||
|
|
||||
|
createComponent(type, props) { |
||||
|
return new ComponentBuilder(this, type, props) |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,49 @@ |
|||||
|
export class ComponentBuilder { |
||||
|
context |
||||
|
|
||||
|
constructor(blockBuilder, type, props) { |
||||
|
this.blockBuilder = blockBuilder |
||||
|
this.type = type |
||||
|
this.id = `${blockBuilder.context.id}-${blockBuilder.componentCount++}` |
||||
|
this.props = props |
||||
|
this.children = [] |
||||
|
this.styles = null |
||||
|
} |
||||
|
|
||||
|
setProp(key, value) { |
||||
|
this.props[key] = value |
||||
|
return this |
||||
|
} |
||||
|
|
||||
|
setProps(props) { |
||||
|
this.props = { |
||||
|
...this.props, |
||||
|
...props, |
||||
|
} |
||||
|
return this |
||||
|
} |
||||
|
|
||||
|
setStyles(styles) { |
||||
|
this.styles = styles |
||||
|
return this |
||||
|
} |
||||
|
|
||||
|
addChild(component) { |
||||
|
this.children.push(component) |
||||
|
return this |
||||
|
} |
||||
|
|
||||
|
build() { |
||||
|
return { |
||||
|
_component: `@budibase/standard-components/${this.type}`, |
||||
|
_id: this.id, |
||||
|
_children: this.children?.map(child => child.build()), |
||||
|
_styles: { |
||||
|
normal: { |
||||
|
...this.styles, |
||||
|
}, |
||||
|
}, |
||||
|
...this.props, |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1 @@ |
|||||
|
export { default as tablewithsearch } from "./TableWithSearch.svelte" |
||||
Loading…
Reference in new issue