0.1.9 • Published 6 years ago

tyble v0.1.9

Weekly downloads
4
License
MIT
Repository
github
Last release
6 years ago

tyble is a typesafe React table written in TypeScript.

Master Status Develop Status

Table of Contents

Features

  • Typed property selectors for cell data
  • Customizable and controllable (React class, JSX, callbacks)
  • Themeable (styled-components, sass)
  • Column sorting and custom sorting functionality provided

Installation

npm version

Yarn

yarn add tyble

NPM

npm i tyble

or for the in development branch:

npm i tyble@next

Example

import * as React from 'react';
import * as ReactDOM from 'react-dom';

import Tyble, { SortOrder, TableColumn } from 'tyble';

interface Person {
    name: string;
    lastname: string;
    skills: Skill[];
    company: Company;
}

interface Skill {
    name: string;
    level: number;
}

interface Company {
    name: string;
}

const data: Person[] = [
    {
        name: 'Jason',
        lastname: 'Watson',
        skills: [{  name: 'TypeScript',  level: 100 }],
        company: {  name: 'Caspian' },
    },
    {
        name: 'Charles',
        lastname: 'Xavier',
        skills: [{ name: 'Telepathy', level: 90}],
        company: {  name: 'X-Men' },
    },
];

const columns: Array<TableColumn<Person>> = [
    {
        heading: {
            content: 'First',
            sortOrder: SortOrder.DESC,
        },
        sort: sortFunc,
        cells: (props: Person) => <span>{props.name}</span>
    },
    {
        heading: { content: 'Last' },
        cells: (props: Person) => <span>{props.lastname}</span>
    },
    {
        heading: { content: 'Company' },
        cells: (props: Person) => <span>{props.company.name}</span>
    },
];

ReactDOM.render(
    <Table columns={columns} data={data} />,
    document.getElementById('root'),
);

There are some more examples in the example directory.

Props

Tyble

These are all of the props for <Tyble /> component.

NameTypeDescription
dataT[]T is the type your data uses.
columnsTableColumn<T>[]Provide columns to shape and connect data to tyble.
themeThemePropsOverride or replace default theme. By default it uses an internal theme.
defaultSortoptional SortFuncIf no sorting function is provided, no sorting will be available.
onHeadingClickoptional MouseClickFuncHeading click callback.
onRowClickoptional MouseClickFuncRow click callback.
classNameoptional stringTop level className for styling.
captionoptional string<caption> text

Theme Props

Here are all the props and defaults for the ThemeProps object. Creating your own allows you to customize the default theme. You can also, if you prefer, create only a partial theme to only override certain parts.

NameDefault
headingFontColor#4a4a4a
headingBgColor#f7f7f7
headingFontFamilyNews Cycle
headingBorder1px solid #e6e6e6
headingFontSize14px
headingFontWeightnormal
headingCursorpointer
headingTextTransformuppercase
headingPadding15px
rowSeparatorColor1px solid #e6e6e6
rowBgColornone
rowFontFamilyLato
rowAltBgColorundefined
rowHoverColor#f5f8fc
rowPadding15px
rowTextAligncenter
rowTransitionall 0.5s ease
cellFontSize12px
cellFontColor#4a4a4a
cellBgColornone
captionBgColorundefined
captionFontColorundefined
captionPadding.8em .8em

Types

You can define our interfaces and types to be used by tyble.

import * as React from 'react';
import * as ReactDOM from 'react-dom';

import { Tyble, SortOrder, TableColumn } from 'tyble';

interface Person {
    name: string;
    lastname: string;
    skills: Skill[];
    company: Company;
}

interface Skill {
    name: string;
    level: number;
}

interface Company {
    name: string;
}

Data

Pass any data into tyble in an array. See below an example of what data might look like. This could be from an external web api etc.

const data: Person[] = [
    {
        name: 'Jason',
        lastname: 'Watson',
        skills: [{  name: 'TypeScript',  level: 100 }],
        company: {  name: 'Caspian' },
    },
    {
        name: 'Charles',
        lastname: 'Xavier',
        skills: [{ name: 'Telepathy', level: 90}],
        company: {  name: 'X-Men' },
    },
];

If you want sorting you can write a custom sorting method to enable it on a column.

const sortFunc = (props: Person[], sortOrder: SortOrder) => {
   return props.sort((a: Person, b: Person) => {

        if (sortOrder === SortOrder.DESC) {
            return a.name > b.name ? 1 : -1;
        }
        return a.name < b.name ? 1 : -1;

    });
};

Columns

Define your columns and use them to populate your cells. No accessor id needed because we have type safety!

const columns: Array<TableColumn<Person>> = [
    {
        heading: {
            content: 'First',
            sortOrder: SortOrder.DESC,
        },
        sort: sortFunc,
        cells: (props: Person) => <span>{props.name}</span>
    },
    {
        heading: { content: 'Last' },
        cells: (props: Person) => <span>{props.lastname}</span>
    },
    {
        heading: { content: 'Company' },
        cells: (props: Person) => <span>{props.company.name}</span>
    },
];

Rendering

Standard

ReactDOM.render(
    <Table columns={columns} data={data} />,
    document.getElementById('root'),
);

JSX Style

Alternatively you can write a tyble using JSX manually.

<Table className={'tyble'}>
    <HeadingSection>
        <Heading content='Heading 1' />
    </HeadingSection>
    <RowSection>
        <Row>
            <Cell content='Cell 1' />
            <Cell content='Cell 2' />
            <Cell content='Cell 3' />
        </Row>
    </RowSection>
</Table>;

Styling

There are a number of ways to style tyble we try to be unopinionated about styling and offer a few ways to do it.

  • Sass
  • Theme (styled-components)
  • Overriding default theme

Sass

 .tyble {
    display: flex;
    flex-flow: column nowrap;
    flex: 1 1 auto;
    .heading-section {
        display: flex;
        cursor: pointer;
        border-top: 1px solid lightgray;
        .heading {
            display: flex;
            width: 100%;
            background: #ff8080;
            padding: 15px;
        }
    }
    .row {
        display: flex;
        padding: 15px;
        width: 100%;
        border-top: 1px solid lightgray;
        .cell {
            display: flex;
            flex-grow: 1;
            flex-basis: 0;
        }
    }
}

Classes

  • tyble, heading-section, heading, row, cell

Theme

You can pass a theme object to the theme prop to use your own.

Override default theme

tyble ships with a clean and minimal theme but you can override this style with your own.

Tests and Linting

Running the tests and linters

 yarn run test
 yarn run lint:ts
 yarn run lint:css

Contributing

Please read CONTRIBUTING for details on our code of conduct, and the process for submitting pull requests to us.

License

This project is licensed under the MIT License - see the LICENSE file for details.

FAQ

  • Is this production ready?

Until version 1.0.0 this component will not be production ready. It is being developed and changed at pace.

0.1.9

6 years ago

0.2.0

6 years ago

0.1.68

6 years ago

0.1.67

6 years ago

0.1.6

7 years ago

0.1.5

7 years ago

0.1.4

7 years ago

0.1.3

7 years ago

0.1.2

7 years ago

0.1.11

7 years ago

0.1.1

7 years ago

0.1.0-np

7 years ago

0.1.0

7 years ago