1.19.9 • Published 11 days ago

@olenbetong/appframe-react v1.19.9

Weekly downloads
-
License
MIT
Repository
github
Last release
11 days ago

@olenbetong/appframe-react

Hooks to connect AppframeWeb data objects to React components.

Getting Started

Installation

Install using npm

npm install @olenbetong/appframe-react

or include the IIFE build in a script

<script src="https://unpkg.com/@olenbetong/appframe-react@latest/dist/iife/af.React.min.js"></script>

Data object context

This package includes a data object context that is useful for binding form elements and buttons to the current row of the data object.

The following hooks use the data object from context:

  • useDataObject - Returns the data object in context
  • useCancelButton - Returns a cancelEdit function used to revert the current row to saved values
  • useDeleteButton - Returns a deleteRow function to delete the current row (or row at an index given as the first parameter). Will prompt the user with window.confirm before deleting. Also returns an isDeleting boolean.
  • useField(fieldName) - Returns an object with everything needed to bind form controls to the current row of the data object:
    • dataObject: The data object to bind to
    • error: Any validation errors when trying to update the record
    • value: Current value of the input (if there is en error, the faulty value is still returned)
    • onKeyDown: Keydown event handler that cancels changes when Escape is pressed
    • setValue: Function to set the value of the field on the current row
    • onChange: For simple components, this property can be passed directly to the component
    • record: The current record
    • reset: Cancels changes, and removes any errors
  • useRefreshButton - Returns a method to refresh the data object, and a loading boolean
  • useRefreshRowButton - Returns a method to refresh the current row of the data object, and a loading indicator
  • useSaveButton - Returns a method to save changes on the current row of the data object, and dirty and isSaving booleans

Other hooks

  • useCurrentIndex(dataObject) - Returns only the current index
  • useCurrentRow(dataObject) - Returns the current record
  • useData(dataObject, options) - Returns an array with all records currently in the data object
  • useDataLength(dataObject) - Returns the current number of records in the data object
  • useDataWithFilter(dataObject, filter, type) - Like useData, but loads data with the given filter.
  • useDirty(dataObject) - Returns a boolean indicating if the current row is dirty or not
  • useError(dataObject) - Returns any loading error message
  • useFilter(dataObject, filter, type) - Refreshes data object whenever the filter changes
  • useLoading(dataObject) - Returns a boolean indicating if the data object is loading or not
  • useStatus(dataObject) - Returns booleans indicating if the data object is saving or deleting records
  • usePaging(dataObject) - Returns page, page count and a method to change the page
  • useParameter(dataObject, parameter) - Returns the current value of parameter
  • usePermissions(dataObject) - Returns booleans indicating if the user can delete, insert or update records
  • useDragAndDropUpload({ dataObject }) - Returns drag and drop event handlers needed to automatically upload files to the data object when dropped on the element

The above hooks uses the data objects internal state to pass data to the components. If you do not want to depend on the data objects current row or data storage, you can use the following hooks. They return data from the data object's data handler directly, and will not affect the data objects internal state.

  • useFetchData(dataObject, filter) - Returns data matching the filter. If the filter is set to false, data will not be loaded.
  • useFetchRecord(dataObject, filter) - Use if the filter is expected to only return a single row. If multiple rows are returned from the server, only the first record will be returned to the component.

One hook is also available for procedures.

  • useProcedure(procedure, parameters, options) - Returns an object containing data, execute, isExecuting and error properties. Executes whenever the procedure or parameters arguments change. execute can be used to manually execute the procedure again

useData options

useData accepts a second options argument. Available options are:

  • includeDirty (default true) - Includes currently dirty data in the dataset. Disable this to optimize if the data is used many places.

Examples

Getting all state from the data object

This will list all reacords in the data object, and create an editor for the current row.

import {
  useCurrentIndex,
  useCurrentRow,
  useData,
  useDataLength,
  useDirty,
  useError,
  useLoading,
  useStatus,
  usePermissions,
  useParameter,
} from "@olenbetong/appframe-react";

function MyFunctionComponent(props) {
  const currentIndex = useCurrentIndex(dsMyDataObject);
  const myRecord = useCurrentRow(dsMyDataObject);
  const myRecords = useData(dsMyDataObject);
  const count = useDataLength(dsMyDataObject);
  const isDirty = useDirty(dsMyDataObject);
  const error = useError(dsMyDataObject);
  const isLoading = useLoading(dsMyDataObject);
  const { isDeleting, isSaving } = useStatus(dsMyDataObject);
  const { allowDelete, allowInsert, allowUpdate } = usePermissions(dsMyDataObject);
  const filter = useParameter(dsMyDataObject, "filterString");

  return (
    <div>
      {isLoading && <i className="fa fa-spin fa-spinner" />}
      {error && <Error message={error} />}
      <MyEditor {...myRecord} isDirty={isDirty} />
      {myRecords.map((record) => (
        <ListItem {...item} />
      ))}
      There are {count} records matching {filter}
    </div>
  );
}

Automatically getting data with a given filter.

If you want to conditionally load data, you may set the filter to false.

import { useDataWithFilter, useLoading } from "@olenbetong/appframe-react";

function MyComponent({ someId }) {
  const isLoading = useLoading(dsMyDataObject);
  const data = useDataWithFilter(dsMyDataObject, `[SomeID] = ${someId}`);

  return (
    <div>
      {isLoading && <i className="fa fa-spin fa-spinner" />}
      {data.map((record) => (
        <ListItem {...record} />
      ))}
    </div>
  );
}

Getting data from the data source without affecting the data object

If you want to conditionally load data, you may set the filter to false.

The refreshRows method can be used to update only a subset of the current data. The first parameter is the filter that will be used to fetch data. The second parameter is the field that will be used to compare fetched data with current data (defaults to PrimKey). If the refreshRows fetches records that are not in the current set, they will not be added.

import { useFetchData, useFetchRecord } from "@olenbetong/appframe-react";

function MyFunctionComponent(props) {
  const { isLoading, data, refresh, refreshRows } = useFetchData(dsMyDataObject, `[EntityCategory] = 1`);

  return (
    <div>
      {isLoading && <i className="fa fa-spin fa-spinner" />}
      {data.map((data) => (
        <ListItem {...item} onRefresh={refreshRows(`[PrimKey] = '${item.PrimKey}'`, "PrimKey")} />
      ))}
    </div>
  );
}

function MyRecordComponent(props) {
  const { isLoading, record, refresh } = useFetchRecord(dsMyDataObject, `[EntityID] = ${props.id}`);

  return (
    <div>
      {isLoading && <Spinner />}
      <button onClick={refresh}>
        <i className="fa fa-refresh" /> Refresh
      </button>
      <MyEditor {...record} />
    </div>
  );
}

Executing a stored procedure

If 'Parameter', 'OtherParam', 'ThirdParam' are not valid parameters for 'procMyProcedure', they will be removed before executing the procedure. This way the procedure can be executed even if the actual parameters haven't changed. If 'removeInvalidParameters' isn't set to true, an error will occur instead.

import { useProcedure } from "@olenbetong/appframe-react";

function MyComponent() {
  const { data, execute, error, isExecuting } = useProcedure(
    procMyProcedure,
    {
      Parameter: "value",
      OtherParam: 52,
      ThirdParam: "Oh, hai!",
    },
    { removeInvalidParameters: true }
  );

  return (
    <div>
      <button onClick={execute}>Refresh data</button>
      {error && <div className="alert alert-danger">{error}</div>}
      {isExecuting && <Spinner />}
      {data && data.length > 0 && data[0].map((record) => <RecordComponent key={record.IdentityField} {...record} />)}
    </div>
  );
}

Paging component

import { usePaging } from "@olenbetong/appframe-react";

function PagingComponent() {
  const { changePage, page, pagecount } = usePaging(dsMyDataObject);

  return (
    <div>
      <button onClick={() => changePage(page - 1)} disabled={page <= 0}>
        Previous
      </button>
      Page {page + 1} of {pageCount}
      <button onClick={() => changePage(page + 1)} disabled={page + 1 >= pageCount}>
        pagecount
      </button>
    </div>
  );
}

Changelog

See the GitHub releases page

1.19.9

11 days ago

1.19.8

28 days ago

1.19.7

29 days ago

1.19.6

1 month ago

1.19.5

2 months ago

1.19.4

2 months ago

1.19.3

2 months ago

1.19.2

2 months ago

1.19.1

2 months ago

1.19.0

2 months ago

1.18.2

3 months ago

1.18.1

3 months ago

1.18.0

3 months ago

1.17.1

4 months ago

1.17.0

4 months ago

1.15.1

4 months ago

1.16.1

4 months ago

1.16.0

4 months ago

1.15.0

5 months ago

1.14.15

5 months ago

1.14.13

5 months ago

1.14.14

5 months ago

1.14.1

8 months ago

1.14.0

9 months ago

1.14.5

7 months ago

1.14.4

7 months ago

1.14.3

8 months ago

1.14.2

8 months ago

1.14.9

6 months ago

1.14.8

6 months ago

1.14.7

6 months ago

1.14.6

6 months ago

1.14.10

6 months ago

1.14.11

6 months ago

1.14.12

5 months ago

1.13.2

1 year ago

1.13.6

1 year ago

1.13.5

1 year ago

1.13.4

1 year ago

1.13.3

1 year ago

1.13.9

12 months ago

1.13.11

11 months ago

1.13.8

1 year ago

1.13.10

11 months ago

1.13.7

1 year ago

1.12.0

1 year ago

1.13.1

1 year ago

1.13.0

1 year ago

1.11.5

1 year ago

1.11.0

1 year ago

1.11.4

1 year ago

1.11.3

1 year ago

1.11.2

1 year ago

1.11.1

1 year ago

1.9.12

1 year ago

1.9.11

1 year ago

1.9.10

1 year ago

1.9.9

1 year ago

1.9.8

1 year ago

1.9.7

1 year ago

1.9.6

1 year ago

1.9.5

1 year ago

1.9.4

1 year ago

1.9.3

1 year ago

1.9.2

1 year ago

1.9.1

1 year ago

1.9.0

1 year ago

1.8.13

1 year ago

1.8.14

1 year ago

1.8.15

1 year ago

1.8.16

1 year ago

1.10.1

1 year ago

1.10.0

1 year ago

1.8.2

1 year ago

1.8.1

2 years ago

1.8.0

2 years ago

1.8.9

1 year ago

1.8.10

1 year ago

1.8.8

1 year ago

1.8.11

1 year ago

1.8.7

1 year ago

1.8.12

1 year ago

1.8.6

1 year ago

1.8.5

1 year ago

1.8.4

1 year ago

1.8.3

1 year ago

1.6.4

2 years ago

1.6.3

2 years ago

1.6.2

2 years ago

1.6.1

2 years ago

1.6.0

2 years ago

1.7.7

2 years ago

1.5.9

2 years ago

1.7.6

2 years ago

1.5.8

2 years ago

1.7.5

2 years ago

1.5.7

2 years ago

1.7.4

2 years ago

1.5.6

2 years ago

1.7.3

2 years ago

1.5.5

2 years ago

1.7.2

2 years ago

1.5.4

2 years ago

1.7.1

2 years ago

1.7.0

2 years ago

1.5.10

2 years ago

1.5.11

2 years ago

1.5.3

2 years ago

1.5.2

2 years ago

1.2.5

2 years ago

1.5.1

2 years ago

1.2.4

2 years ago

1.5.0

2 years ago

1.4.0

2 years ago

1.3.0

2 years ago

1.2.0

2 years ago

1.1.1

2 years ago

1.1.0

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago

1.2.3

2 years ago

1.2.2

2 years ago

1.2.1

2 years ago

1.1.2

2 years ago

1.0.0-rc.9

2 years ago

1.0.0-rc.7

2 years ago

1.0.0-rc.8

2 years ago

1.0.0-rc.5

2 years ago

1.0.0-rc.6

2 years ago

1.0.0-rc.13

2 years ago

1.0.0-rc.12

2 years ago

1.0.0-rc.11

2 years ago

1.0.0-rc.10

2 years ago

1.0.0-rc.4

2 years ago

1.0.0-rc.14

2 years ago

1.0.0-rc.3

2 years ago

1.0.0-rc.2

2 years ago

1.0.0-rc.0

2 years ago