5.2.1 • Published 18 days ago

@dxc-technology/halstack-react-hal v5.2.1

Weekly downloads
1
License
-
Repository
github
Last release
18 days ago

Halstack React HAL

Halstack React HAL is an npm library of reusable React components. It brings together two different responsibilities:

We have other libraries that will help you handling these responsibilities individually (Halstack Client / Halstack React). Halstack React HAL uses them under the hood, but it is a higher level abstraction that puts both responsibilities together using the most common association patterns.

For example, collection resources are often associated with tables, and there are a lot of semantics in the standards described by the DXC API guidelines for collections (sorting, paginating...) that could be associated with UI interactions (clicking a table header for sorting, clicking pages for paginating)

Usage

Halstack React HAL is distributed as an npm library. In order to use it in an existing project, you must install it first. You also need to install styled-components and Halstack React CDK, which are peer dependencies.

npm install @dxc-technology/halstack-react-hal styled-components @dxc-technology/halstack-react

The library provides the following components and hooks to be used in your React application:

Components

Hooks

HalTable Component

HalTable Props

NameDefaultDescription
collectionUrl: stringThe URL of the collection resource to be used for the table. Required
headers: objectContains the HTTP headers to be sent along with the HTTP requests to the collectionUrl. Optional
itemsPerPage: number5The amount of items to be displayed per page. Will be used to calculate the _start and _num query parameters that will be sent to the collection for pagination. Optional
asyncHeadersHandler: () => Promise<object>Async function that will be executed right before every HTTP request in order to retrieve dynamic headers. It must return a promise that resolves into an object with the keys and values of the headers. These headers will be merged with the ones indicated in the headers prop. Optional
columns: Column[][]Array of objects specifying the columns to be displayed in the table. Each Column object has: - header: Column label to be place at the table header. - displayProperty: The name of the property in the items summary to be rendered for this column in the table. - sortProperty: The name of the property in the items summary to be used for sorting the table. - onClickItemFunction: Callback function that will be executed when the user clicks an item in that column. The collection item will be passed to this function when executed. - mapFunction: Callback function that must return the value to be rendered in that column for a specific item. The item will be passed to this function as a parameter.

HalTable Example

import React from "react";
import { HalTable } from "@dxc-technology/halstack-react-hal";

export default () => (
  <HalTable
    collectionUrl={"https://..."}
    columns={[
      {
        header: "Username",
        displayProperty: "username",
        sortProperty: "username"
      },
      {
        header: "Status",
        displayProperty: "status",
      },
      {
        header: "Enabled",
        displayProperty: "enabled",
      },
    ]}
  />
);

HalAutocomplete Component

HalAutocomplete Props

NameDefaultDescription
collectionUrl: stringThe URL of the collection resource to be used for the suggestions. Required
headers: objectContains the HTTP headers to be sent along with the HTTP requests to the collectionUrl. Optional
asyncHeadersHandler: () => Promise<object>Async function that will be executed right before every HTTP request in order to retrieve dynamic headers. It must return a promise that resolves into an object with the keys and values of the headers. These headers will be merged with the ones indicated in the headers prop. Optional
propertyName: stringName of the property to be used for filtering the data.

In addition to these component-specific properties you will also have all the properties of the Text Input component that can be found on its site.

HalAutocomplete Example

import React, { useState } from "react";
import { HalAutocomplete } from "@dxc-technology/halstack-react-hal";

export default () => {
  const [autocompleteValue, changeAutocompleteValue] = useState("");
  const onChange = ({ newValue }) => {
    changeAutocompleteValue(newValue);
  };

  return (
    <HalAutocomplete
      collectionUrl="https://..."
      propertyName="full-name"
      label="Full Name"
      onChange={onChange}
      value={autocompleteValue}
    />
  );
};

useHalResource Hook

useHalResource Parameters

NameDefaultDescription
url: stringThe URL of the resource. Required
headers: objectContains the HTTP headers to be sent along with the HTTP requests to the URL indicated in the url prop. Optional
asyncHeadersHandler: () => Promise<object>Async function that will be executed right before every HTTP request in order to retrieve dynamic headers. It must return a promise that resolves into an object with the keys and values of the headers. These headers will be merged with the ones indicated in the headers prop. Optional

useHalResource Return Array

The return value of this hook is an array with the following stateful variables. The property names in this table are just a reference, and you will need to identify them by item position. The table is sorted by item position within the array.

NameDescription
resource: HalResourceA Halstack Client's HalResource instance of the resource behind the url parameter. It will be null until the resource is fetched. It will be automatically refreshed if the execution of an interaction handler responds with an instance of the same resource.
requestStatus: 'idle' | 'fetching' | 'resolved' | 'rejected' | 'interaction'The status of the HTTP request to the url parameter. 'idle' before the request is triggered 'fetching' after the request is triggered and the before we get a response. 'resolved' after getting a successful response. Only if it contains a HAL resource. 'rejected' after getting an error response. Or if response doesn't contain a HAL resource. 'interaction' during the execution of an interaction handler.
requestError: ErrorResponseThe error object in case the request gets rejected. It will be null before getting the response or if the response is successful and contains a HAL resource.
resourceInteractions: objectThis is an object containing as many entries as interactions (_options.links) are available in the HAL resource. Each entry has the rel value of the interaction as a key, and a function that you can execute passing a payload as a parameter. Executing one of these functions will: Make the HTTP request associated to the given interaction.Change the requestStatus to 'interaction', and then back to 'resolved' (even when the request fails).Update the resource if the request responds with a new representation of the same resource.Return a promise, so that you can handle the resolution or rejection manually.

useHalResource Example

import React from "react";
import { useHalResource } from "@dxc-technology/halstack-react-hal";

export default () => {
  const [
    resource,
    requestStatus,
    requestError,
    resourceInteractions,
  ] = useHalResource({
    url: "https://...",
  });

  return <div>{requestStatus}</div>;
};

Contributing

Before opening new issues or pull requests, please refer to CONTRIBUTING.MD.

Development Setup

These instructions will get you a copy of the project up and running on your local machine for development and testing purposes.

The project is divided in two main folders. One is for the actual library, and the other one is a React application using the library.

Project

Install the dependencies for the library and the example project.

npm install

Library

Contained in the lib folder.

Run the build process updating the bundled files inside the dist folder.

nx build halstack-react-hal #`npx nx build halstack-react-hal` if nx is not recognized as a command.

Example Application

Contained in the app folder.

Start the application.

nx serve app #`npx nx serve app` if nx is not recognized as a command.

Now, anytime you make a change to the library or the app, nx will live-reload your local dev server so you can iterate on your component in real-time.

0.0.0-6e2a5ef

18 days ago

5.2.1

18 days ago

0.0.0-9a0711d

1 month ago

5.2.0

1 month ago

5.1.0

1 month ago

0.0.0-2031665

1 month ago

5.0.0

1 month ago

0.0.0-5f681d5

1 month ago

0.0.0-f6fb71a

2 months ago

0.0.0-b5815ce

4 months ago

0.0.0-959e63b

4 months ago

0.0.0-0b1d369

4 months ago

0.0.0-7f6ca7c

5 months ago

4.0.0

5 months ago

0.0.0-2b39bbd

5 months ago

0.0.0-1f7ded7

6 months ago

0.0.0-d8219fc

1 year ago

3.0.0

1 year ago

0.0.0-03eb9ae

1 year ago

0.0.0-10ce6b8

1 year ago

0.0.0-6d2820f

1 year ago

0.0.0-90b3739

1 year ago

0.0.0-609d867

1 year ago

0.0.0-3a23b22

1 year ago

0.0.0-777ef70

2 years ago

0.0.0-6b2c44c

2 years ago

0.0.0-e5f85e3

2 years ago

0.0.0-2e8bed7

2 years ago

0.0.0-408a64a

2 years ago

0.0.0-880df26

2 years ago

2.0.0

2 years ago

0.0.0-fef431e

2 years ago

0.0.0-f9cf5bc

2 years ago

0.0.0-611eb58

2 years ago

0.0.0-e9d8c69

2 years ago

0.0.0-f6bd61d

2 years ago

0.0.0-fb4e7de

2 years ago

0.0.0-4ff71e1

2 years ago

0.0.0-7df9297

2 years ago

0.0.0-33fcce2

2 years ago

0.0.0-9945855

2 years ago

1.1.4

2 years ago

0.0.0-d16f8ca

2 years ago

0.0.0-7485b1d

2 years ago

1.1.3

2 years ago

0.0.0-53d771a

2 years ago

0.0.0-a1f85e8

3 years ago

0.0.0-7102767

3 years ago

1.1.2

3 years ago

0.0.0-6902cd4

3 years ago

1.1.1

3 years ago

0.0.0-8d5042d

3 years ago

0.0.0-e3519d9

3 years ago

0.0.0-948174a

3 years ago

1.1.0

4 years ago

0.0.0-20f739d

4 years ago

0.0.0-6f8f4ca

4 years ago

0.0.0-8222279

4 years ago

1.0.2

4 years ago

0.0.0-33219bf

4 years ago

1.0.1

4 years ago

0.0.0-8186c10

4 years ago

1.0.0

4 years ago

0.0.0-efdd784

4 years ago

0.0.0-c372eb1

4 years ago

0.0.0-b2baeaa

4 years ago

0.0.4

4 years ago

0.0.0-30124aa

4 years ago

0.0.3

4 years ago

0.0.2

4 years ago

0.0.0-81fe961

4 years ago

0.0.1

4 years ago

0.0.0-6cb69af

4 years ago

0.0.0-b3abca3

4 years ago

0.0.0-58d598a

4 years ago

0.0.0-f340e67

4 years ago