Skip to content

Add currency to DisplayMode #345

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/npm-fastui/src/components/display.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,8 @@ export const DisplayPrimitive: FC<DisplayPrimitiveProps> = (props) => {
case 'json':
case 'inline_code':
return <DisplayInlineCode value={value} />
case 'currency':
return <DisplayCurrency value={value} />
default:
unreachable('Unexpected display type', mode, props)
}
Expand Down Expand Up @@ -219,6 +221,18 @@ const DisplayInlineCode: FC<{ value: JSONPrimitive }> = ({ value }) => {
}
}

const DisplayCurrency: FC<{ value: JSONPrimitive }> = ({ value }) => {
if (typeof value === 'boolean') {
return value.toString()
} else if (value === null) {
return <DisplayNull />
} else {
return Intl.NumberFormat('en-US', { style: 'currency', currency: 'USD' }).format(
typeof value === 'string' ? parseFloat(value) : value,
)
}
}

export type DataModel = Record<string, JsonData>

export interface DisplayLookupProps extends Omit<Display, 'type' | 'value'> {
Expand Down
1 change: 1 addition & 0 deletions src/npm-fastui/src/models.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ export type DisplayMode =
| 'markdown'
| 'json'
| 'inline_code'
| 'currency'
export type SelectOptions = SelectOption[] | SelectGroup[]

/**
Expand Down
1 change: 1 addition & 0 deletions src/python-fastui/fastui/components/display.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class DisplayMode(str, enum.Enum):
markdown = 'markdown'
json = 'json'
inline_code = 'inline_code'
currency = 'currency'


class DisplayBase(BaseModel, ABC, defer_build=True):
Expand Down