Skip to content

Commit 96aff04

Browse files
authored
Add currency to DisplayMode (#345)
1 parent e8b12f5 commit 96aff04

File tree

3 files changed

+16
-0
lines changed

3 files changed

+16
-0
lines changed

src/npm-fastui/src/components/display.tsx

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,8 @@ export const DisplayPrimitive: FC<DisplayPrimitiveProps> = (props) => {
135135
case 'json':
136136
case 'inline_code':
137137
return <DisplayInlineCode value={value} />
138+
case 'currency':
139+
return <DisplayCurrency value={value} />
138140
default:
139141
unreachable('Unexpected display type', mode, props)
140142
}
@@ -219,6 +221,18 @@ const DisplayInlineCode: FC<{ value: JSONPrimitive }> = ({ value }) => {
219221
}
220222
}
221223

224+
const DisplayCurrency: FC<{ value: JSONPrimitive }> = ({ value }) => {
225+
if (typeof value === 'boolean') {
226+
return value.toString()
227+
} else if (value === null) {
228+
return <DisplayNull />
229+
} else {
230+
return Intl.NumberFormat('en-US', { style: 'currency', currency: 'USD' }).format(
231+
typeof value === 'string' ? parseFloat(value) : value,
232+
)
233+
}
234+
}
235+
222236
export type DataModel = Record<string, JsonData>
223237

224238
export interface DisplayLookupProps extends Omit<Display, 'type' | 'value'> {

src/npm-fastui/src/models.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ export type DisplayMode =
7171
| 'markdown'
7272
| 'json'
7373
| 'inline_code'
74+
| 'currency'
7475
export type SelectOptions = SelectOption[] | SelectGroup[]
7576

7677
/**

src/python-fastui/fastui/components/display.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ class DisplayMode(str, enum.Enum):
2727
markdown = 'markdown'
2828
json = 'json'
2929
inline_code = 'inline_code'
30+
currency = 'currency'
3031

3132

3233
class DisplayBase(BaseModel, ABC, defer_build=True):

0 commit comments

Comments
 (0)