Skip to content

Commit

Permalink
Update core components (#27)
Browse files Browse the repository at this point in the history
* Update core components
  • Loading branch information
cvetty authored Jul 1, 2024
1 parent eaef087 commit f11eebb
Show file tree
Hide file tree
Showing 6 changed files with 491 additions and 138 deletions.
136 changes: 0 additions & 136 deletions docs/core-components.md

This file was deleted.

54 changes: 54 additions & 0 deletions docs/core-components/dynamic-table-component.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
## Description
The `krait-ui` DynamicTable component represents the table data on front-end.

## Example

```html
<script setup>
defineProps({
'filtersForm': {
type: String,
required: false,
default: undefined,
}
});
</script>

<template>
<DynamicTable
apiEndpoint="users-table"
tableName="users-table"
:filtersForm="filtersForm"
>
<template #row="{ record, column }">
<div class="cell" v-if="column.name === 'my_first_column'">
Krait is awesome!
</div>
<div class="cell" v-else>
{{ record[column.name] ?? 'N/A' }}
</div>
</template>
</DynamicTable>
</template>

<style scoped lang="scss"></style>
```

## Highlights

### Slots Usage
The `DynamicTable` component uses slots to provide an easy way to manipulate the front-end data.

```html
<template #row="{ record, column }">
<div class="cell" v-if="column.name === 'my_first_column'">
Krait is awesome!
</div>
<div class="cell" v-else>
{{ record[column.name] ?? 'N/A' }}
</div>
</template>
```

- The `record` object contains the record values for all columns
- The `column` object contains all column properties (`name`, `fixed`, `label`, etc.)
127 changes: 127 additions & 0 deletions docs/core-components/table-columns.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
## Description

All columns are internally represented by the [`TableColumnDTO`](https://github.com/mtrdesign/krait/blob/main/krait/src/DTO/TableColumnDTO.php) class. We define the columns
inside our Table Definition Classes using the `column` method. Here is the list of all parameters that can be assigned to specific column.



```php
<?php
protected column(
string $name,
string $label,
bool $hideLabel = false,
bool $datetime = false,
bool $sortable = true,
bool $fixed = false,
string|null $classes = null,
callable|null $process = null,
callable|null $sort = null
): void
```

**Parameters:**

| Parameter | Type | Description |
|-------------|------------------------|-----------------------------------------------------|
| `name` | **string** | The columns name |
| `label` | **string** | The columns label |
| `hideLabel` | **bool** | Flags if the label should be visible in the header. |
| `datetime` | **bool** | Flags if the column contains datetime object. |
| `sortable` | **bool** | Flags if the column is sortable. |
| `fixed` | **bool** | Flags if the column is resizable. |
| `classes` | **string&#124;null** | Additional classes that will be added on FE. |
| `process` | **callable&#124;null** | The column result generation callback. |
| `sort` | **callable&#124;null** | The column sorting callback. |


## TableColumnDTO Description

DTO Object for handling consistent column generation.

* Full name: `\MtrDesign\Krait\DTO\TableColumnDTO`

## TableColumnDTO Methods

### __construct

```php
public __construct(
string $name,
string $label,
bool $hideLabel = false,
bool $datetime = false,
bool $sortable = true,
bool $fixed = false,
string|null $classes = null,
callable|null $process = null,
callable|null $sort = null
): mixed
```

**Parameters:**

| Parameter | Type | Description |
|--------------|------------------------|------------------------------------------------------|
| `$name` | **string** | - The column name. |
| `$label` | **string** | - The column label. |
| `$hideLabel` | **bool** | - Flags if the label should be visible. |
| `$datetime` | **bool** | - Flags if the column contains datetime information. |
| `$sortable` | **bool** | - Flags if the column is sortable. |
| `$fixed` | **bool** | - Flags if the column is resizable. |
| `$classes` | **string&#124;null** | - Sets FE style classes. |
| `$process` | **callable&#124;null** | - Sets the result processing callback. |
| `$sort` | **callable&#124;null** | - Sets the records sorting callback. |

***

### process

Processes one record.

```php
public process(mixed $model): mixed
```

**Parameters:**

| Parameter | Type | Description |
|------------|-------------|--------------------------|
| `$model` | **mixed** | the target resource data |

***

### sort

Sorts records.

```php
public sort(mixed $records, string $direction): mixed
```

**Parameters:**

| Parameter | Type | Description |
|--------------|-------------|--------------------------|
| `$records` | **mixed** | - The records. |
| `$direction` | **string** | - The sorting direction. |

***

### hasProcessingCallback

Flags if the column has processing callback assigned.

```php
public hasProcessingCallback(): bool
```

***

### toArray

Returns the column represented as array.

```php
public toArray(): array
```
40 changes: 40 additions & 0 deletions docs/core-components/table-controller.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
## Description

All table-related controllers are created in the `app/Http/Controllers/Tables` directory.
These controllers are invokable and have only one purpose - to fetch the table data and pass it to the .
`Table Definition Class`.

```php
<?php

namespace App\Http\Controllers\Tables;

use App\Tables\MyAwesomeTable;
use MtrDesign\Krait\Http\Resources\TableCollection;

class MyAwesomeTableController extends Controller
{
public function __invoke(): TableCollection
{
$records = [['my_first_column' => 'foo']];

/**
* Here you might fetch/manipulate the data.
*/

return MyAwesomeTable::from($records);
}
}
```

## Methods

***

### __invoke

Processes the incoming front-end request.

```php
public invoke(): MtrDesign\Krait\Http\Resources\TableCollection
```
Loading

0 comments on commit f11eebb

Please sign in to comment.