Skip to content

Commit deba245

Browse files
committed
docs: add things to consider when using custom cell components (#22)
1 parent 2bd11a4 commit deba245

1 file changed

Lines changed: 57 additions & 6 deletions

File tree

README.md

Lines changed: 57 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -174,11 +174,13 @@ children | - |
174174
#### Wrap `Cell`
175175

176176
Sometimes custom `Cell` components are needed.
177-
By creating new component, which is based on `Cell`, its only necessary to set the props once.
177+
By creating a new component, which is based on `Cell`, its only necessary to set the props once.
178+
However, this comes with certain downsides. In order to keep the API as easy to use as possible, I implemented some automations for the `Sections` component.
179+
For example, the `Cell.backgroundColor` prop will also decide on the `backgroundColor` of the `Separator` component.
178180

179-
```javascript
180-
...
181+
Given the following pattern:
181182

183+
```javascript
182184
import {
183185
Cell,
184186
Section,
@@ -204,7 +206,7 @@ const CellVariant = (props) => (
204206
/>
205207
);
206208

207-
...
209+
// ...
208210

209211
<TableView>
210212
<Section>
@@ -214,9 +216,58 @@ const CellVariant = (props) => (
214216
<CellVariant title="Element 4" />
215217
</Section>
216218
</TableView>
219+
````
217220

218-
...
219-
```
221+
This pattern introduces an additional layer between `Section` and `Cell`: `Section` -> `CellVariant` -> `Cell`.
222+
The `Section` component is expecting a `Cell` component as a child and therefor tries to [access the props as defined for the `Cell` component](https://github.com/Purii/react-native-tableview-simple/blob/5e81f61993eea32784cd9b20fa6e73d1240d77e5/src/components/Section.tsx#L131).
223+
If following the mentioned pattern, this would fail, because `CellVariant.props` only contains the prop `title`.
224+
Instead, I recommend to insert your new default props as description in this section: [Override defaults of `Cell`-Component](##override-defaults-of-cell-component).
225+
226+
If this is not enough for you, and you still need to have a custom cell component, consider merging both approaches:
227+
228+
```javascript
229+
import {
230+
Cell,
231+
Section,
232+
TableView,
233+
} from 'react-native-tableview-simple';
234+
235+
236+
const cellPropsVariant = {
237+
hideSeparator: true,
238+
backgroundColor: 'black',
239+
};
240+
241+
const CellVariant = (props) => (
242+
<Cell
243+
{...props}
244+
cellContentView={
245+
<View
246+
style={{ alignItems: 'center', flexDirection: 'row', flex: 1, paddingVertical: 10 }}
247+
>
248+
<Text
249+
allowFontScaling
250+
numberOfLines={1}
251+
style={{ flex: 1, fontSize: 20 }}
252+
>
253+
{props.title}
254+
</Text>
255+
</View>
256+
}
257+
/>
258+
);
259+
260+
// ...
261+
262+
<TableView>
263+
<Section>
264+
<CellVariant title="Element 1" {...cellPropsVariant} />
265+
<CellVariant title="Element 2" {...cellPropsVariant} />
266+
<CellVariant title="Element 3" {...cellPropsVariant} />
267+
<CellVariant title="Element 4" {...cellPropsVariant} />
268+
</Section>
269+
</TableView>
270+
````
220271
221272
### `Separator`
222273

0 commit comments

Comments
 (0)