An open API service indexing awesome lists of open source software.

https://github.com/repetitioestmaterstudiorum/dynamic-list-react

React component to render a list of input items that can be added to, updated and removed from without any buttons.
https://github.com/repetitioestmaterstudiorum/dynamic-list-react

dynamiclist nomodes react

Last synced: about 1 month ago
JSON representation

React component to render a list of input items that can be added to, updated and removed from without any buttons.

Awesome Lists containing this project

README

          

# dynamic-list-react

Inspired by Larry Tesler's work, this package contains a react component to render a list of input items that can be added to, updated and removed from without switching between edit and view modes and without any buttons (optionally, a delete button can be shown for each item).

## Installation

`npm install dynamic-list-react`

## Usage

### Basic Example

![Basic Example Illustration](https://github.com/repetitioestmaterstudiorum/dynamic-list-react/raw/main/assets/basic-example.gif?raw=true)

```javascript
import React from 'react';
import { DynamicList, type HandleInputChange } from 'dynamic-list-react';

export function ExampleDynamicList() {
const data = getCurrentData();
const defaultItem = { name: '' };

const updateKeyValue = (_id: string, key: string, value: any) => {
const currentData = getCurrentData();
const itemToUpdate = currentData.find(item => item._id === _id);
if (itemToUpdate) {
itemToUpdate[key] = value;
setData(currentData);
}
}

const removeItem = (_id: string) => {
const currentData = getCurrentData();
setData(currentData.filter(item => item._id !== _id));
}

const addItem = (item: ItemType) => {
const currentData = getCurrentData();
setData(currentData.concat(item));
}

const InputComponent = (item: ItemType, handleInputChange: HandleInputChange) => (


handleInputChange(item._id, 'name', e.target.value)}
/>

);

return (

renderComponent={InputComponent}
dataProp={data}
addItem={addItem}
updateKeyValue={updateKeyValue}
removeItem={removeItem}
defaultItem={defaultItem}
/>
);
}

function getCurrentData() {
return JSON.parse(localStorage.getItem('storageKey') || '[]') as ItemType[];
}

function setData(data: ItemType[]) {
localStorage.setItem('storageKey', JSON.stringify(data));
}

type ItemType = Record;
```

### Advanced Example

![Advanced Example Illustration](https://github.com/repetitioestmaterstudiorum/dynamic-list-react/raw/main/assets/advanced-example.gif?raw=true)

```javascript
import React from 'react'
import { DynamicList, type HandleDelete, type HandleInputChange } from 'dynamic-list-react'

export function ExampleDynamicList() {
const data = getCurrentData()
const defaultItem = {
firstName: '',
lastName: '',
score: '',
}

const updateKeyValue = (_id: string, key: string, value: any) => {
const currentData = getCurrentData()
const itemToUpdate = currentData.find((item) => item._id === _id)
if (itemToUpdate) {
itemToUpdate[key] = value
setData(currentData)
}
}

const removeItem = (_id: string) => {
const currentData = getCurrentData()
setData(currentData.filter((item) => item._id !== _id))
}

const addItem = (item: ItemType) => {
const currentData = getCurrentData()
setData(currentData.concat(item))
}

const InputComponent = (
item: ItemType,
handleInputChange: HandleInputChange,
handleDelete: HandleDelete,
isLastItem: boolean,
idx: number
) => (


Item {idx + 1}


handleInputChange(item._id, 'firstName', e.target.value)}
/>
handleInputChange(item._id, 'lastName', e.target.value)}
/>
handleInputChange(item._id, 'score', e.target.value)}
/>
{isLastItem ? null : handleDelete(item._id)}>Delete}

)

return (

renderComponent={InputComponent}
dataProp={data}
addItem={addItem}
updateKeyValue={updateKeyValue}
removeItem={removeItem}
defaultItem={defaultItem}
getRandomId={getRandomId}
/>
)
}

function getCurrentData() {
return JSON.parse(localStorage.getItem('storageKey') || '[]') as ItemType[]
}

function setData(data: ItemType[]) {
localStorage.setItem('storageKey', JSON.stringify(data))
}

function getRandomId() {
// e.g. crypto.randomUUID() or uuidv4()
return Date.now().toString()
}

type ItemType = Record
```

## Larry Tesler: No Modes

> One of Mr Tesler's firmest beliefs was that computer systems should stop using "modes", which were common in software design at the time.
> Modes allow users to switch between functions on software and apps but make computers both time-consuming and complicated.
> So strong was this belief that Mr Tesler's website was called "nomodes.com", his Twitter handle was "@nomodes", and even his car's registration plate was "No Modes".

Source: https://www.bbc.com/news/world-us-canada-51567695