futil-js / contexture-react

React components for building contexture interfaces
https://smartprocure.github.io/contexture-react
MIT License
6 stars 4 forks source link

Add `displayDefault` to `ResultTable` #279

Open daedalus28 opened 5 years ago

daedalus28 commented 5 years ago

This line: https://github.com/smartprocure/contexture-react/blob/master/src/exampleTypes/ResultTable.js#L288

Replace the x => x with a parameterized default display function. Also pass field as the third param here: https://github.com/smartprocure/contexture-react/blob/master/src/exampleTypes/ResultTable.js#L290

The motivation is to allow end users to customize the default - e.g., to look at field types on schemas to automatically format by type. We could maybe also pass fields or even a "schemaField" equivalent (the destructed props there) as param 4

daedalus28 commented 5 years ago

for more context, these were recently written which partially duplicate functionality in ResultTable, but also provide way more intelligent defaults which should drastically reduce the number of times we need a display function:

let fieldFormatters = {
  date: toFullDate,
  number: toNumber,
  mongoId: formatMongoId,
}
let renderSchemaField = _.curry((schemas, schema, record, field) => {
  let schemaField = getSchemaField(schemas, schema, field)
  return schemaField.display
    ? x => schemaField.display(x, record)
    : _.getOr(safeRender, schemaField.typeDefault, fieldFormatters)
})

let renderSchemaLabel = _.curry(
  (schemas, schema, record, field) =>
    getSchemaField(schemas, schema, field).label || _.startCase(field)
)