mirror of
https://github.com/gristlabs/grist-core.git
synced 2026-03-02 04:09:24 +00:00
(core) Simple localization support and currency selector.
Summary: - Grist document has a associated "locale" setting that affects how currency is formatted. - Currency selector for number format. Test Plan: not done Reviewers: dsagal Reviewed By: dsagal Subscribers: paulfitz Differential Revision: https://phab.getgrist.com/D2977
This commit is contained in:
49
app/client/widgets/CurrencyPicker.ts
Normal file
49
app/client/widgets/CurrencyPicker.ts
Normal file
@@ -0,0 +1,49 @@
|
||||
import {ACSelectItem, buildACSelect} from "app/client/lib/ACSelect";
|
||||
import {Computed, IDisposableOwner, Observable} from "grainjs";
|
||||
import {ACIndexImpl} from "app/client/lib/ACIndex";
|
||||
import {testId} from 'app/client/ui2018/cssVars';
|
||||
import {currencies} from 'app/common/Locales';
|
||||
|
||||
interface CurrencyPickerOptions {
|
||||
// The label to use in the select menu for the default option.
|
||||
defaultCurrencyLabel: string;
|
||||
}
|
||||
|
||||
export function buildCurrencyPicker(
|
||||
owner: IDisposableOwner,
|
||||
currency: Observable<string|undefined>,
|
||||
onSave: (value: string|undefined) => void,
|
||||
{defaultCurrencyLabel}: CurrencyPickerOptions
|
||||
) {
|
||||
const currencyItems: ACSelectItem[] = currencies
|
||||
.map(item => ({
|
||||
value: item.code,
|
||||
label: `${item.code} ${item.name}`,
|
||||
cleanText: `${item.code} ${item.name}`.trim().toLowerCase(),
|
||||
}));
|
||||
|
||||
// Add default currency label option to the very front.
|
||||
currencyItems.unshift({
|
||||
label : defaultCurrencyLabel,
|
||||
value : defaultCurrencyLabel,
|
||||
cleanText : defaultCurrencyLabel.toLowerCase(),
|
||||
});
|
||||
// Create a computed that will display 'Local currency' as a value and label
|
||||
// when `currency` is undefined.
|
||||
const valueObs = Computed.create(owner, (use) => use(currency) || defaultCurrencyLabel);
|
||||
const acIndex = new ACIndexImpl<ACSelectItem>(currencyItems, 200, true);
|
||||
return buildACSelect(owner,
|
||||
{
|
||||
acIndex, valueObs,
|
||||
save(_, item: ACSelectItem | undefined) {
|
||||
// Save only if we have found a match
|
||||
if (!item) {
|
||||
throw new Error("Invalid currency");
|
||||
}
|
||||
// For default value, return undefined to use default currency for document.
|
||||
onSave(item.value === defaultCurrencyLabel ? undefined : item.value);
|
||||
}
|
||||
},
|
||||
testId("currency-autocomplete")
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user