@ttoss/components

2.0.10 • Public • Published

@ttoss/components

@ttoss/components is a set of React components that you can use to build your apps using ttoss ecosystem.

ESM Only

This package is ESM only because react-markdown.

Getting Started

Install @ttoss/components

pnpm add @ttoss/components @ttoss/ui @emotion/react @ttoss/react-hooks

Components

You can check all the components of the library @ttoss/ui on the Storybook.

List

The List component is a React component that renders an unordered list (<ul>) and accepts ListItem as its children. Each ListItem can contain any React content, including other components.

import React from 'react';
import { List, ListItem } from '@ttoss/components/list';

const MyComponent = () => (
  <List>
    <ListItem>Item 1</ListItem>
    <ListItem>Item 2</ListItem>
    <ListItem>Item 3</ListItem>
    <ListItem>
      <CustomComponent />
    </ListItem>
  </List>
);

In this example, List is used to render an <ul> list with four items. The last item contains a custom React component (CustomComponent), demonstrating that ListItem can receive any React content as its children.

This is a basic example of how to use the List component with ListItem. You can customize the content and styles as needed to fit your project requirements.

Markdown

Markdown uses react-markdown under the hood, so the props are the same. You can update the elements as you want. Ex:

const MARKDOWN_CONTENT = `
# Heading 1
## Heading 2
### Heading 3
#### Heading 4
##### Heading 5

Lorem ipsum dolor sit, amet consectetur adipisicing elit. Odit quasi dolorum aperiam fugiat earum expedita non eligendi similique id minus explicabo, eum facere nihil aspernatur libero! Sapiente aliquid tenetur dolor.

- Item 1
- Item 2
- Item 3

![Alt Text](https://fastly.picsum.photos/id/436/200/300.jpg?hmac=OuJRsPTZRaNZhIyVFbzDkMYMyORVpV86q5M8igEfM3Y "Alt Text")

[Google](https://google.com)
`;

const Component = () => {
  return (
    <Markdown
      components={{
        a: ({ children, ...props }) => (
          <Link {...props} quiet>
            {children}
          </Link>
        ),
      }}
    >
      {MARKDOWN_CONTENT}
    </Markdown>
  );
};

Modal

Modal uses react-modal under the hood, so the props are the same. The only difference is that the styles are theme-aware. You can style the modal using theme tokens, except that array as value don't work.

import { Modal } from '@ttoss/components';
import { Box, Button, Flex, Text } from '@ttoss/ui';

/**
 * See https://reactcommunity.org/react-modal/accessibility/#app-element
 */
// Modal.setAppElement('#root'); Prefer using this static method over setting it on the component.

Modal.setAppElement('#modal-root');

const Component = () => {
  const [isOpen, setIsOpen] = React.useState(false);

  return (
    <Box id="modal-root">
      <Modal
        isOpen={isOpen}
        onAfterOpen={action('onAfterOpen')}
        onAfterClose={action('onAfterClose')}
        onRequestClose={() => {
          action('onRequestClose')();
          setIsOpen(false);
        }}
        style={{
          overlay: {
            backgroundColor: 'primary',
          },
          content: {
            backgroundColor: 'secondary',
            padding: ['lg', 'xl'], // Array as value don't work.
          },
        }}
      >
        <Flex>
          <Text>This is a modal.</Text>
          <Text>Here is the content.</Text>
          <Button
            onClick={() => {
              setIsOpen(false);
            }}
          >
            Close Modal
          </Button>
        </Flex>
      </Modal>
      <Button
        onClick={() => {
          setIsOpen(true);
        }}
      >
        Open Modal
      </Button>
    </Box>
  );
};

Search

Search is a component that integrates an input field with debouncing functionality, making it ideal for search bars where you want to limit the rate of search queries based on user input.

It uses the useDebounce hook from @ttoss/react-hooks to delay the search action until the user has stopped typing for a specified duration, which helps to prevent unnecessary or excessive queries.

import React, { useState } from 'react';
import { Search } from '@ttoss/components';
import { Box } from '@ttoss/ui';

const SearchComponent = () => {
  const [searchText, setSearchText] = useState('');

  const handleSearchChange = (newValue) => {
    setSearchText(newValue);
    // Perform search or update logic here
  };

  return (
    <Box>
      <Search
        value={searchText}
        onChange={handleSearchChange}
        loading={/* loading state here */}
        debounce={500} // Adjust the debounce time as needed
      />
    </Box>
  );
};

In this example, the Search component receives the current search text and a handler function to update this text. The loading prop can be used to display a loading indicator, and the debounce prop controls the debounce delay.

Table

The Table component is a flexible and customizable table that supports sorting, filtering, and pagination. It is designed to be easy to use and integrate with your data sources. It exports all TanStack Table hooks and methods.

Basic Usage

import * as React from 'react';
import {
  Table,
  TableBody,
  TableCell,
  TableFooter,
  TableHead,
  TableHeader,
  TableRow,
  createColumnHelper,
  flexRender,
  getCoreRowModel,
  useReactTable,
} from '@ttoss/components/table';

type Person = {
  firstName: string;
  lastName: string;
  age: number;
  visits: number;
  status: string;
  progress: number;
};

const defaultData: Person[] = [
  {
    firstName: 'tanner',
    lastName: 'linsley',
    age: 24,
    visits: 100,
    status: 'In Relationship',
    progress: 50,
  },
  {
    firstName: 'tandy',
    lastName: 'miller',
    age: 40,
    visits: 40,
    status: 'Single',
    progress: 80,
  },
  {
    firstName: 'joe',
    lastName: 'dirte',
    age: 45,
    visits: 20,
    status: 'Complicated',
    progress: 10,
  },
];

const columnHelper = createColumnHelper<Person>();

const columns = [
  columnHelper.accessor('firstName', {
    cell: (info) => {
      return info.getValue();
    },
    footer: (info) => {
      return info.column.id;
    },
  }),
  columnHelper.accessor(
    (row) => {
      return row.lastName;
    },
    {
      id: 'lastName',
      cell: (info) => {
        return <i>{info.getValue()}</i>;
      },
      header: () => {
        return <span>Last Name</span>;
      },
      footer: (info) => {
        return info.column.id;
      },
    }
  ),
  columnHelper.accessor('age', {
    header: () => {
      return 'Age';
    },
    cell: (info) => {
      return info.renderValue();
    },
    footer: (info) => {
      return info.column.id;
    },
  }),
  columnHelper.accessor('visits', {
    header: () => {
      return <span>Visits</span>;
    },
    footer: (info) => {
      return info.column.id;
    },
  }),
  columnHelper.accessor('status', {
    header: 'Status',
    footer: (info) => {
      return info.column.id;
    },
  }),
  columnHelper.accessor('progress', {
    header: 'Profile Progress',
    footer: (info) => {
      return info.column.id;
    },
  }),
];

const RenderTable = () => {
  const [data] = React.useState(() => {
    return [...defaultData];
  });

  const table = useReactTable({
    data,
    columns,
    getCoreRowModel: getCoreRowModel(),
  });

  return (
    <Table>
      <TableHead>
        {table.getHeaderGroups().map((headerGroup) => {
          return (
            <TableRow key={headerGroup.id}>
              {headerGroup.headers.map((header) => {
                return (
                  <TableHeader key={header.id}>
                    {header.isPlaceholder
                      ? null
                      : flexRender(
                          header.column.columnDef.header,
                          header.getContext()
                        )}
                  </TableHeader>
                );
              })}
            </TableRow>
          );
        })}
      </TableHead>
      <TableBody>
        {table.getRowModel().rows.map((row) => {
          return (
            <TableRow key={row.id}>
              {row.getVisibleCells().map((cell) => {
                return (
                  <TableCell key={cell.id}>
                    {flexRender(cell.column.columnDef.cell, cell.getContext())}
                  </TableCell>
                );
              })}
            </TableRow>
          );
        })}
      </TableBody>
      <TableFooter>
        {table.getFooterGroups().map((footerGroup) => {
          return (
            <TableRow key={footerGroup.id}>
              {footerGroup.headers.map((header) => {
                return (
                  <TableHeader key={header.id}>
                    {header.isPlaceholder
                      ? null
                      : flexRender(
                          header.column.columnDef.footer,
                          header.getContext()
                        )}
                  </TableHeader>
                );
              })}
            </TableRow>
          );
        })}
      </TableFooter>
    </Table>
  );
};

Versions

Current Tags

VersionDownloads (Last 7 Days)Tag
2.0.10
4latest

Version History

VersionDownloads (Last 7 Days)Published
2.0.10
4
2.0.9
1
2.0.8
1
2.0.7
3
2.0.6
1
2.0.5
1
2.0.4
1
2.0.3
1
2.0.2
1
2.0.1
1
2.0.0
1
1.31.16
1
1.31.15
1
1.31.14
1
1.31.13
1
1.31.12
2
1.31.11
1
1.31.10
1
1.31.9
1
1.31.8
1
1.31.7
1
1.31.6
1
1.31.5
1
1.31.4
1
1.31.3
1
1.31.2
1
1.31.1
1
1.31.0
1
1.30.6
1
1.30.5
1
1.30.4
2
1.30.3
1
1.30.2
1
1.30.1
1
1.30.0
1
1.29.32
1
1.29.31
1
1.29.30
1
1.29.29
2
1.29.28
1
1.29.27
1
1.29.26
1
1.29.25
2
1.29.24
1
1.29.23
1
1.29.22
1
1.29.21
1
1.29.20
1
1.29.19
1
1.29.18
1
1.29.17
1
1.29.16
1
1.29.15
1
1.29.14
1
1.29.13
1
1.29.12
1
1.29.11
1
1.29.10
1
1.29.9
1
1.29.8
1
1.29.7
1
1.29.6
1
1.29.5
1
1.29.4
1
1.29.3
1
1.29.2
1
1.29.01
1.28.131
1.28.121
1.28.111
1.28.101
1.28.91
1.28.81
1.28.71
1.28.61
1.28.51
1.28.41
1.28.32
1.28.21
1.28.11
1.28.01
1.27.31
1.27.21
1.27.11
1.27.01
1.26.442
1.26.431
1.26.421
1.26.411
1.26.402
1.26.391
1.26.381
1.26.371
1.26.361
1.26.351
1.26.341
1.26.331
1.26.321
1.26.311
1.26.301
1.26.291
1.26.281
1.26.271
1.26.261
1.26.251
1.26.241
1.26.231
1.26.221
1.26.211
1.26.201
1.26.191
1.26.181
1.26.171
1.26.161
1.26.151
1.26.141
1.26.131
1.26.121
1.26.111
1.26.101
1.26.91
1.26.71
1.26.61
1.26.51
1.26.41
1.26.31
1.26.21
1.26.11
1.26.01
1.25.01
1.24.81
1.24.71
1.24.61
1.24.51
1.24.41
1.24.31
1.24.21
1.24.11
1.24.01
1.23.11
1.23.01
1.21.161
1.21.151
1.21.141
1.21.131
1.21.121
1.21.111
1.21.102
1.21.91
1.21.81
1.21.71
1.21.61
1.21.51
1.21.42
1.21.31
1.21.21
1.21.12
1.21.01
1.20.01
1.19.01
1.18.01
1.16.11
1.16.01
1.15.21
1.15.11
1.15.01
1.14.41
1.14.31
1.14.21
1.14.11
1.14.01
1.13.21
1.13.11
1.13.01
1.12.21
1.12.11
1.12.01
1.11.71
1.11.61
1.11.51
1.11.41
1.11.31
1.11.21
1.11.11
1.11.01
1.10.01
1.9.61
1.9.51
1.9.41
1.9.31
1.9.21
1.9.11
1.9.01
1.8.21
1.8.11
1.8.01

Package Sidebar

Install

npm i @ttoss/components

Weekly Downloads

211

Version

2.0.10

License

MIT

Unpacked Size

93.4 kB

Total Files

24

Last publish

Collaborators

  • pedro-arantes
  • techtriangulostecnologia