Skip to content

Commit 8b2ea04

Browse files
authored
Allow primary columns without getLink (#68)
* Allow primary columns without getLink * Simplify cleanUpNullables
1 parent bce13bb commit 8b2ea04

File tree

4 files changed

+32
-23
lines changed

4 files changed

+32
-23
lines changed

docs/Examples/Layouts.example.purs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,13 @@ import Data.Array (index)
66
import Data.Maybe (Maybe(..), fromMaybe)
77
import Data.Newtype (un)
88
import Data.NonEmpty ((:|))
9-
import Data.Nullable (toNullable)
9+
import Data.Nullable (notNull, toNullable)
1010
import Data.String (Pattern(..), split)
1111
import Effect.Console (log)
1212
import Effect.Uncurried (mkEffectFn1, mkEffectFn2, runEffectFn1)
1313
import Lumi.Components.Button (button, defaults)
1414
import Lumi.Components.Column (column_)
15+
import Lumi.Components.Example (example)
1516
import Lumi.Components.Images (avatar_, productThumb_)
1617
import Lumi.Components.Layouts.Centered as Centered
1718
import Lumi.Components.Layouts.OneColumnWithHeader as OneColumnWithHeader
@@ -24,7 +25,6 @@ import Lumi.Components.Tab (TabId(..), TabKey(..), urlParts)
2425
import Lumi.Components.Table (ColumnName(..), table)
2526
import Lumi.Components.Text (body_, h2_, p_, sectionHeader_)
2627
import Lumi.Components.Utility.ReactRouter (RouterProps, withRouter)
27-
import Lumi.Components.Example (example)
2828
import React.Basic (Component, JSX, createComponent, element, toReactComponent)
2929
import React.Basic.DOM as R
3030
import Web.HTML.History (URL(..))
@@ -241,7 +241,7 @@ docs = (\c -> element c {}) $ withRouter $ toReactComponent identity component {
241241
, filterLabel: toNullable $ Just "Product title"
242242
, sortBy: toNullable Nothing
243243
, style: R.css {}
244-
, getLink: _.link
244+
, getLink: notNull _.link
245245
, sticky: false
246246
, renderCell: \rowData ->
247247
lockup

docs/Examples/Modal.example.purs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import Data.Maybe (Maybe(..))
66
import Data.Monoid (guard)
77
import Data.Nullable (notNull, null)
88
import Lumi.Components.Button as Button
9-
import Lumi.Components.Button (ButtonState(..), button, secondary)
9+
import Lumi.Components.Button (button, secondary)
1010
import Lumi.Components.Column (column_)
1111
import Lumi.Components.Modal (dialog, errorModal, modal)
1212
import Lumi.Components.Size (Size(..))

docs/Examples/Table.example.purs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import Effect.Uncurried (mkEffectFn1, mkEffectFn2)
1313
import Foreign (readString, unsafeToForeign)
1414
import Foreign.Index (readProp)
1515
import Lumi.Components.Column (column, column_)
16+
import Lumi.Components.Example (example)
1617
import Lumi.Components.Images (productThumb_)
1718
import Lumi.Components.Input (input, text_)
1819
import Lumi.Components.Link as Link
@@ -21,7 +22,6 @@ import Lumi.Components.NativeSelect (defaults, nativeSelect)
2122
import Lumi.Components.Size (Size(..))
2223
import Lumi.Components.Table (ColumnName(..), SortString(..), table)
2324
import Lumi.Components.Text (p_)
24-
import Lumi.Components.Example (example)
2525
import React.Basic (Component, JSX, createComponent, make)
2626
import React.Basic.DOM (css)
2727
import React.Basic.DOM as R
@@ -117,7 +117,7 @@ docs = unit # make component { initialState, render }
117117
, filterLabel: notNull "Product lockup"
118118
, sortBy: null
119119
, style: css {}
120-
, getLink: _.link
120+
, getLink: notNull _.link
121121
, renderCell: \rowData ->
122122
lockup
123123
{ image: Just $ productThumb_ { image: R.img { src: rowData.imgSrc }, size: Small }
@@ -192,7 +192,7 @@ docs = unit # make component { initialState, render }
192192
, filterLabel: notNull "Product title"
193193
, sortBy: null
194194
, style: css {}
195-
, getLink: _.link
195+
, getLink: null -- notNull _.link
196196
, renderCell: R.text <<< _.title
197197
, sticky: false
198198
}
@@ -237,7 +237,7 @@ docs = unit # make component { initialState, render }
237237
, filterLabel: notNull "Product title"
238238
, sortBy: null
239239
, style: css {}
240-
, getLink: _.link
240+
, getLink: notNull _.link
241241
, renderCell: R.text <<< _.title
242242
, sticky: false
243243
}

src/Lumi/Components/Table.purs

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ type TableProps row =
7777
, filterLabel :: Nullable String
7878
, sortBy :: Nullable ColumnName
7979
, style :: R.CSS
80-
, getLink :: row -> URL
80+
, getLink :: Nullable (row -> URL)
8181
, renderCell :: row -> JSX
8282
, sticky :: Boolean
8383
}
@@ -310,7 +310,9 @@ table = make component
310310
where
311311
selected = fromMaybe self.state.selected (toMaybe self.props.selected)
312312

313-
primaryColumn = toMaybe self.props.primaryColumn
313+
primaryColumn = cleanUpNullables <$> toMaybe self.props.primaryColumn
314+
where
315+
cleanUpNullables x = x { getLink = toMaybe x.getLink }
314316

315317
renderTableHead columns tableRef =
316318
element (R.unsafeCreateDOMComponent "thead")
@@ -472,7 +474,7 @@ type TableRowProps row col_ pcol_ =
472474
{ name :: ColumnName
473475
, renderCell :: row -> JSX
474476
, style :: R.CSS
475-
, getLink :: row -> URL
477+
, getLink :: Maybe (row -> URL)
476478
, sticky :: Boolean
477479
| pcol_
478480
}
@@ -502,14 +504,16 @@ tableRow = make tableRowComponent { initialState: unit, shouldUpdate, render }
502504
R.tr
503505
{ className: joinWith " "
504506
$ guard (tableProps.selectable && isSelected) [ "active" ]
505-
<> guard (isJust tableProps.primaryColumn) [ "active-row" ]
507+
<> guard (isJust $ _.getLink =<< tableProps.primaryColumn) [ "active-row" ]
506508
, onClick:
507509
case tableProps.primaryColumn of
508-
Nothing -> Events.handler_ (pure unit)
509-
Just col -> Events.handler syntheticEvent \event -> do
510-
s <- hasWindowSelection
511-
when (not s) $
512-
runEffectFn1 tableProps.onNavigate (col.getLink row)
510+
Just { getLink: Just getLink } ->
511+
Events.handler syntheticEvent \event -> do
512+
s <- hasWindowSelection
513+
when (not s) $
514+
runEffectFn1 tableProps.onNavigate (getLink row)
515+
_ ->
516+
Events.handler_ (pure unit)
513517
, children:
514518
[ if not tableProps.selectable
515519
then empty
@@ -554,11 +558,16 @@ tableRow = make tableRowComponent { initialState: unit, shouldUpdate, render }
554558
, style: col.style
555559
, "data-required": true
556560
, children:
557-
Link.link Link.defaults
558-
{ href = col.getLink row
559-
, navigate = Just (runEffectFn1 onNavigate (col.getLink row))
560-
, text = col.renderCell row
561-
}
561+
case col.getLink of
562+
Nothing ->
563+
col.renderCell row
564+
Just getLink ->
565+
Link.link Link.defaults
566+
{ href = getLink row
567+
, navigate = Just (runEffectFn1 onNavigate (getLink row))
568+
, text = col.renderCell row
569+
, className = Just "primary-cell-link"
570+
}
562571
}
563572

564573
renderBodyRowCell row col =
@@ -707,7 +716,7 @@ styles = jss
707716
, textOverflow: "ellipsis"
708717
, backgroundColor: cssStringHSLA colors.white
709718

710-
, "&.primary-cell a":
719+
, "&.primary-cell a.lumi.primary-cell-link":
711720
{ color: cssStringHSLA colors.primary
712721
}
713722
}

0 commit comments

Comments
 (0)