hyochan / react-native-masonry-list

The Masonry List implementation which has similar implementation as the `FlatList` in React Native
MIT License
393 stars 55 forks source link

Add support for horizontal layout #17

Closed samzmann closed 2 years ago

samzmann commented 2 years ago

Description

This PR adds support for horizontal list layout, which is a common use of FlatList and ScrollView. This is achieved by modifying the flexDirection of the column and row views:

if horizontal is falsy (default behavior):

if horizontal is true:

Layout direction Before After
default vertical_layout_before vertical_layout_after
horizontal horizontal_layout_before horizontal_layout_after

Related Issues

There are no related issues. I was going to create one but then thought coding the feature myself was probably not so complicated so I decided to submit this PR instead :)

Tests

I did not add any tests. The current tests are passing. I only had to update snapshots. I tested manually on both iOS and Android devices, by using the following dummy data. Works as expected.

// create some dummy data
const [data, setData] = useState<{letter: string, width: number, height: number}[]>([])
useEffect(() => {
  const letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
  const d = []

  for (let i = 0; i < 20; i += 1) {
    d.push({
      letter: letters[i],

      width: Math.floor((Math.random() * 100) + 20),
      height: 30,

      // uncomment to generate data that works for vertical layout
      // width: 100,
      // height: Math.floor((Math.random() * 100) + 20),
    })
  }
  setData(d)
}, [])

// render
<MasonryList
  contentContainerStyle={{ padding: 20 }}
  data={data}
  numColumns={3}
  horizontal
  showsHorizontalScrollIndicator={false}
  renderItem={({ item }) => (
    <View
      style={{
        backgroundColor: 'red', width: item.width, height: item.height, margin: 5,
      }}
    >
      <Text>{item.letter}</Text>
    </View>
  )}
/>

Checklist

Before you create this PR confirms that it meets all requirements listed below by checking the relevant checkboxes ([x]). This will ensure a smooth and quick review process.

codecov[bot] commented 2 years ago

Codecov Report

Merging #17 (795c980) into master (3f26841) will not change coverage. The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff            @@
##            master       #17   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files            1         1           
  Lines           34        35    +1     
  Branches        12        13    +1     
=========================================
+ Hits            34        35    +1     
Flag Coverage Δ
unittests 100.00% <100.00%> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
index.tsx 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more Δ = absolute <relative> (impact), ø = not affected, ? = missing data Powered by Codecov. Last update 3f26841...795c980. Read the comment docs.

samzmann commented 2 years ago

Happy to help :) Thanks for merging!

jduhking commented 7 months ago

For some reason, my content container does cover all my items. Why?

samzmann commented 7 months ago

@jduhking I'm not working on this anymore. Feel free to experiment and submit a new PR. Happy hacking :)