Giter VIP home page Giter VIP logo

kieran-mcintyre / react-native-section-alphabet-list Goto Github PK

View Code? Open in Web Editor NEW
185.0 1.0 54.0 1.45 MB

A simple React Native component that takes an array of data and renders a SectionList with alphabetically sorted data

License: MIT License

JavaScript 2.06% Starlark 0.64% Java 16.43% Ruby 1.70% Objective-C 2.52% TypeScript 63.48% CMake 0.29% C++ 7.69% Shell 0.51% Objective-C++ 4.69%
react-native alphabet sectionlist addressbook

react-native-section-alphabet-list's Introduction

react-native-section-alphabet-list

A simple React Native component that takes an array of data and renders a SectionList with alphabetically (or custom) sorted data.

AlphabetList

Motivation

Unlike alternatives, this package takes an array of data rather than an object with letters as keys.

For example,

Other packages

const data = {
  A: [{...}, ...],
  B: [{...}, ...],
  C: [{...}, ...],
  ...
}

This package

const data = [{...}, {...}, {...}, ...]

For react-native-section-alphabet-list, the data does not need to be pre-formatted and sorted before use. The component handles all this logic internally. You can also provide an array of characters to sort your sections in a custom order.

Installation

Using npm:

npm install react-native-section-alphabet-list

or with yarn:

yarn add react-native-section-alphabet-list

Basic Usage

import { AlphabetList } from "react-native-section-alphabet-list";

const data = [
  { value: 'Lillie-Mai Allen', key: 'lCUTs2' },
  { value: 'Emmanuel Goldstein', key: 'TXdL0c' },
  { value: 'Winston Smith', key: 'zqsiEw' },
  { value: 'William Blazkowicz', key: 'psg2PM' },
  { value: 'Gordon Comstock', key: '1K6I18' },
  { value: 'Philip Ravelston', key: 'NVHSkA' },
  { value: 'Rosemary Waterlow', key: 'SaHqyG' },
  { value: 'Julia Comstock', key: 'iaT1Ex' },
  { value: 'Mihai Maldonado', key: 'OvMd5e' },
  { value: 'Murtaza Molina', key: '25zqAO' },
  { value: 'Peter Petigrew', key: '8cWuu3' },
]

render() {
  return (
    <AlphabetList
      data={data}
      indexLetterStyle={{ 
        color: 'blue', 
        fontSize: 15,
      }}
      renderCustomItem={(item) => (
        <View style={styles.listItemContainer}>
          <Text style={styles.listItemLabel}>{item.value}</Text>
        </View>
      )}
      renderCustomSectionHeader={(section) => (
        <View style={styles.sectionHeaderContainer}>
          <Text style={styles.sectionHeaderLabel}>{section.title}</Text>
        </View>
      )}
    />
  )
}

API

Props

Prop Description Type Signature (func) Default
data List of objects to be sorted and rendered in the SectionList. Each item must have both a value property and unique a key property. array
index (optional) The characters used to sort each item into sections. These characters are rendered on the right-hand side and clicking on each item scrolls the user to its respective section. The default is the letters of the alphabet. array ['a', 'b', 'c', 'd', 'e', 'f', ...] (see DEFAULT_CHAR_INDEX here)
renderCustomItem (optional) Render a row in the SectionList. Should return a valid React Element. func { item: { value: string, key: string } } : ReactElement
renderCustomSectionHeader (optional) Render a section header in the SectionList. Should return a valid React Element. func { section: { title: string, index: number, data: array } } : ReactElement
renderCustomListHeader (optional) Render a list header in the SectionList. Should return a valid React Element. func
renderCustomIndexLetter (optional) Render a custom index letter element. Should return a valid React Element. func { item: { title: string, index: number, data: array }, index: number, onPress: func } : ReactElement
getItemHeight (optional) For sectionListGetItemLayout. This may be necessary if each item has a dynamic height. This allows for smooth scrolling and accurate positioning when scrolling to a section. func { sectionIndex: number, rowIndex: number } : number
sectionHeaderHeight (optional) The height of the section header. number 40
listHeaderHeight (optional) The height of the list header. number 0
letterListContainerStyle (optional) Override the style of the letter list container. object undefined
indexContainerStyle (optional) Override the style of the list index container. object undefined
indexLetterStyle (optional) Override the style of the list letter index text. object undefined
indexLetterContainerStyle (optional) Override the style of the list letter index container. object undefined
uncategorizedAtTop (optional) If true, the uncategorised items (the # section) is moved to the top of the list. boolean false

You can also provide any valid SectionList props. A list of available props can be found here.

Custom index

You can provide a custom array of characters to the component using the index prop. For example, if we wanted to sort alphabetically in-reverse, we could do:

const customIndex = [
  'z',
  'y',
  'x',
  'w',
  ...
]

<AlphabetList
  ...
  index={customIndex}
/>

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update the tests as appropriate.

License

MIT

react-native-section-alphabet-list's People

Contributors

aytajj avatar dependabot[bot] avatar ex-git avatar kieran-mcintyre avatar martinslapinskis avatar sabymike avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

react-native-section-alphabet-list's Issues

Feature Request: Disable scrollTo animation

It would be nice to disable the scroll animation like with SectionList.scrollToLocation({animated:false}) or FlatList.scrollToIndex({animated:false}).

In my usage case, the list has over 1k entries so the animation doesn't work well. Btw, good work on this project and sorry if this feature already exists and I just missed it.

Add style override for index list container

The letter index container itself cannot be styled and its default values are not a perfect match to iOS's default implementation. Adding a style override for the list wrapper would allow for these values to be overridden if desired.

Android tapping Z brings me to F section of the list

I have these props on the ,:
contentInsetAdjustmentBehavior="automatic"
initialNumToRender={100}
maxToRenderPerBatch={500}
updateCellsBatchingPeriod={10}
windowSize={400}
removeClippedSubviews
without these props, as referenced in another issue, the list jerks around a lot and breaks.
when I tap Z on the index, it'll bring me to the F section of the list. The other letters also only bring me to a section of the list from A->F instead of A->Z. I have ~8000 items in the list. works fine on iOS.

Date Not Updating in runtime

Expected Behavior
If we change data source at run time Section list should dynamically updated

Actual Behavior
Currentlyif we change data source section list is not getting current data

Jerky / Jumps around when using..

Hi

Anyone else finding that this component can be quite jerky, and it jumps around when you're using. I've got a video from the simulator here - 249 countries. I just click on the "U" to go there, and you can see it's jumping and struggling with this. Then I move around a little bit and you can see the header bouncing a bit and basically not coping very well.

It's worse on the simulator, on the actual device it's faster but still has the same jumping / stuttering / jerky issues...

Simulator.Screen.Recording.-.iPhone.13.-.2022-06-22.at.11.05.36.mp4

how does getItemHeight work?

Can we have an example of how getItemHeight works?
the doc feels unclear to me;

if my section header is 44px
and each of my list items are 40px

what should I put in getItemHeight ?

Custom component for indexLetterList

Hey!

I am enjoying your package thus far, the only problem/limitation I am currently facing is the ability to customize the indexLetterList with a custom component or custom styling. Do you have any solution for this?

Allow array of letters for section header

Some alphabets include variations of letters which are typically grouped together in an alphabet list. This would be a great feature to add to this plugin for international support.
For example, maybe we could do something like

const myCustomIndex = [
    "a",
    "b",
    "c",
    "ç",
    "d",
    "e",
    "f",
    ["g", "ğ"],
    "h",
    "i",
    ...
]

Thanks!

Originally posted by @Kieran-McIntyre in #6 (comment)

Unable control the index list height

I want to center the alphabet list to middle of the page, is it able to move the index list out of the section to follow the page height? I only can adjust the position within the section list using the indexLetterStyle

Screenshot_1636620047

IData.value is explicitly of type string resulting in TypeScript incompatibility if an object is preferred

Expected

interface IData {
  key: string;
  value: any;
} 

Actual

interface IData {
  key: string;
  value: string;
}

To reproduce with template linting rules

  1. Create an example project.
$ npx create-expo-app --template
Need to install the following packages:
  create-expo-app
Ok to proceed? (y) y
✔ Choose a template: › Blank (TypeScript)
✔ What is your app named? … example-app
...
$ cd example-app
$ npm install react-native-section-alphabet-list
  1. Define a list of objects e.g.
const data: Array<IData> = [
  { key: "0", value: {name: "Boston"}},
  { key: "1", value: {name:"Yellowknife"}}
]
  1. Note linting error:
(property) IData.value: string
Type '{ name: string; }' is not assignable to type 'string'.ts(2322)
types.d.ts(4, 5): The expected type comes from property 'value' which is declared here on type 'IData'

Proposal

Convert IData.value to a generic with a type of any used within the library to remain backwards compatible.
Overall this allows the module to be far more flexible by passing IData.value transparently to an overridden renderCustomItem prop.

Ability to hide scroll bar

Hello, great repo there,
I was going through docs but was unable to find ability to hide scroll bar as it interferes with the alphabets. Is there any way to do so?

image

ref and scrollToLocation

Hello. I'm having issues while trying to use the (SectionList) scrollToLocation method. Is passing ref supposed to work with this component?

I'm using AlphabetList as a functional component and I am using useRef. I can also scroll a regular SectionList fine, its just this component I'm having difficulties with.

Thanks in advance.

RN: 0.64.0
section-alphabet-list: 2.0.5

Sort Number first

Hi, my business want to sort number on the top, how can I do that

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.