Skip to content

Commit

Permalink
feat(Table): add locale props
Browse files Browse the repository at this point in the history
To format number and dates according to the locale.
Default locale comes from the navigator language.
  • Loading branch information
KevinFabre-ods committed Feb 23, 2024
1 parent c6323ba commit d36cdd0
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ const options: TableOptions = {
source: {
href: '',
},
locale: 'fr',
};

const Template: ComponentStory<typeof Table> = (args) => <Table {...args} />;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
<script lang="ts">
import store from '../../store';
import type { DateColumn } from '../../types';
import { warn } from './utils';
export let rawValue: unknown;
export let options: DateColumn['options'];
function getDisplayValue(v: unknown) {
function getDisplayValue(v: unknown, locale: string) {
if (
(typeof v === 'string' || typeof v === 'number') &&
!Number.isNaN(new Date(v).getTime())
) {
return new Intl.DateTimeFormat(navigator.language, options).format(new Date(v));
return new Intl.DateTimeFormat(locale, options).format(new Date(v));
}
warn(v, 'date');
return v;
}
$: value = getDisplayValue(rawValue);
$: value = getDisplayValue(rawValue, $store.locale);
</script>

{value}
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
<script lang="ts">
import store from '../../store';
import type { NumberColumn } from '../../types';
import { warn } from './utils';
export let rawValue: unknown;
export let options: NumberColumn['options'];
function getDisplayValue(v: unknown) {
function getDisplayValue(v: unknown, locale: string) {
if (!Number.isFinite(v)) {
warn(v, 'number');
return v;
}
return new Intl.NumberFormat(navigator.language, options).format(v as number);
return new Intl.NumberFormat(locale, options).format(v as number);
}
$: value = getDisplayValue(rawValue);
$: value = getDisplayValue(rawValue, $store.locale);
</script>

{value}
4 changes: 3 additions & 1 deletion packages/visualizations/src/components/Table/Table.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import SourceLink from '../utils/SourceLink.svelte';
import Header from './Header.svelte';
import Body from './Body.svelte';
import { updateLocale } from './store';
export let data: Async<TableData>;
export let options: TableOptions;
Expand All @@ -16,8 +17,9 @@
$: ({ value: records } = data);
// FIXME: Eslint is in conflict with prettier
// eslint-disable-next-line object-curly-newline
$: ({ columns, title, subtitle, description, source, unstyled } = options);
$: ({ columns, title, subtitle, description, source, unstyled, locale } = options);
$: defaultStyle = !unstyled;
$: updateLocale(locale);
</script>

<div class="ods-dataviz-sdk-table" class:ods-dataviz-sdk-table--default={defaultStyle}>
Expand Down
17 changes: 17 additions & 0 deletions packages/visualizations/src/components/Table/store.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// eslint-disable-next-line import/no-extraneous-dependencies
import { writable } from 'svelte/store';

type TableStore = {
locale: string;
};

const defaultLocale = navigator.language;

const store = writable<TableStore>({ locale: defaultLocale });

const updateLocale = (locale: string = defaultLocale) => {
store.update((previousValue) => ({ ...previousValue, locale }));
};

export { updateLocale };
export default store;
2 changes: 2 additions & 0 deletions packages/visualizations/src/components/Table/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ export type TableOptions = {
subtitle?: string;
description?: string;
source?: Source;
/** To format date and number with the right locale. Default is from browser language */
locale?: string;
/**
* Removes all the presentational styles.
* Default is `false`.
Expand Down

0 comments on commit d36cdd0

Please sign in to comment.