Skip to content

Commit

Permalink
Add currency to DisplayMode (#345)
Browse files Browse the repository at this point in the history
  • Loading branch information
Viicos authored Aug 22, 2024
1 parent e8b12f5 commit 96aff04
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 0 deletions.
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

0 comments on commit 96aff04

Please sign in to comment.