Skip to content

Commit

Permalink
Merge pull request #131 from yildirims/master
Browse files Browse the repository at this point in the history
This closes #123
  • Loading branch information
mbrn authored Dec 24, 2018
2 parents afad0a0 + 818e035 commit c9ec79c
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 4 deletions.
9 changes: 6 additions & 3 deletions docz/examples/02-example-column-types.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,14 @@ import MaterialTable from '../../src/material-table'
columns={[
{title: 'Name', field: 'name'},
{title: 'Surname', field: 'surname'},
{title: 'Expenses', field: 'expenses', type: 'currency', currencySetting:{locale:'tr-TR',currencyCode:'TRY',minimumFractionDigits:2,maximumFractionDigits:3}},
{title: 'Expenses', field: 'expenses', type: 'currency', currencySetting: { locale: 'tr-TR', currencyCode: 'TRY', minimumFractionDigits: 2, maximumFractionDigits: 3 }, emptyValue: 'N/A'},
{title: 'EmptyValue', field: 'emptyVal', emptyValue : () => 'Empty Value Function' }
]}
data={[
{name: 'Mehmet', surname: 'Baran', expenses:3500.555 },
{name: 'Selahattin', surname: 'Yıldırım', expenses:2702.5575 },
{name: 'Mehmet', surname: 'Baran', expenses: 3500.555, emptyVal: 'Empty' },
{name: 'Selahattin', surname: 'Yıldırım', expenses: 2702.5575, emptyVal: '' },
{name: 'Jake', surname: 'Leventhal', expenses: null, emptyVal: null },
{name: 'Selahattin', surname: 'Yıldırım', expenses: null, emptyVal: null }
]}
title="Currency"
options={{
Expand Down
1 change: 1 addition & 0 deletions docz/props.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ import MaterialTable from '../src/material-table'
| | | | 'locale' represents language tag, currencyCode represents ISO 4217 currency codes, |
| | | | minimumFractionDigits represents the min number of fraction digits to use Possible values are from 0 to 20 |
| | | | maximumFractionDigits represents the max number of fraction digits to use. Possible values are from 0 to 20 |
|emptyValue |string or func | | When data is empty or undefined, string value or function result can be set as default value |

### components

Expand Down
1 change: 1 addition & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export interface Column {
type?: 'boolean' | 'numeric' | 'date' | 'datetime' | 'time' | 'currency';
searchable?: boolean;
currencySetting?:{ locale?: string,currencyCode?: string,minimumFractionDigits?:number,maximumFractionDigits?:number};
emptyValue?: any | ((data: any) => any);
}

export interface Components {
Expand Down
11 changes: 11 additions & 0 deletions src/m-table-cell.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import PropTypes from 'prop-types';

export default class MTableCell extends React.Component {
getRenderValue() {
if (this.props.columnDef.emptyValue !== undefined && (this.props.value === undefined || this.props.value === null)) {
return this.getEmptyValue(this.props.columnDef.emptyValue);
}
if (this.props.columnDef.render) {
return this.props.columnDef.render(this.props.rowData);
} else if (this.props.columnDef.type === 'boolean') {
Expand Down Expand Up @@ -40,6 +43,14 @@ export default class MTableCell extends React.Component {
return this.props.value;
}

getEmptyValue(emptyValue) {
if (typeof emptyValue === 'function') {
return this.props.columnDef.emptyValue(this.props.rowData);
} else {
return emptyValue;
}
}

getCurrencyValue(currencySetting, value) {
if (currencySetting !== undefined) {
return new Intl.NumberFormat((currencySetting.locale !== undefined) ? currencySetting.locale : 'en-US',
Expand Down
3 changes: 2 additions & 1 deletion src/material-table.js
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,8 @@ MaterialTable.propTypes = {
currencyCode: PropTypes.string,
minimumFractionDigits: PropTypes.number,
maximumFractionDigits: PropTypes.number
})
}),
emptyValue:PropTypes.oneOfType([PropTypes.string,PropTypes.func])
})).isRequired,
components: PropTypes.shape({
Actions: PropTypes.oneOfType([PropTypes.element, PropTypes.func]),
Expand Down

0 comments on commit c9ec79c

Please sign in to comment.