Skip to content
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

add FormattedText component #72

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions demo/components_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ def components_view() -> list[AnyComponent]:
c.Text(text='This is a text component.'),
]
),
c.Div(
components=[
c.Heading(text='FormattedText', level=2),
c.Markdown(text='`FormattedText` can be used to change the style of the text.'),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please add something like

this is particularly useful to avoid the overhead of loading the markdown renderer, or when you're including user generated content that you don't want to escape styling.

c.FormattedText(text='This is a FormattedText component.', text_color='red', background_color='blue', text_format='italic'),
],
class_name='border-top mt-3 pt-1',
),
c.Div(
components=[
c.Heading(text='Paragraph', level=2),
Expand Down
1 change: 1 addition & 0 deletions demo/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ def api_index() -> list[AnyComponent]:

* `Markdown` — that's me :-)
* `Text`— example [here](/components#text)
* `FormattedText`— example [here](/components#FormattedText)
* `Paragraph` — example [here](/components#paragraph)
* `PageTitle` — you'll see the title in the browser tab change when you navigate through the site
* `Heading` — example [here](/components#heading)
Expand Down
27 changes: 27 additions & 0 deletions src/npm-fastui/src/components/FormattedText.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { FC } from 'react'

import { ClassName } from '../hooks/className'

export interface FormattedTextProps {
type: 'FormattedText'
text: string
textFormat?: 'bold' | 'italic' | 'underline' | 'strikethrough'
color?: string
backgroundColor?: string
className?: ClassName
}

export const FormattedTextComp: FC<FormattedTextProps> = (props) => {
const { text, textFormat, color, backgroundColor } = props

const style = {
backgroundColor,
color,
fontWeight: textFormat === 'bold' ? 'bold' : 'normal',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should probably be omitted, not normal if bold is not set.

fontStyle: textFormat === 'italic' ? 'italic' : 'normal',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same.

textDecoration:
textFormat === 'underline' ? 'underline' : textFormat === 'strikethrough' ? 'line-through' : 'none',
}

return <span style={style}>{text}</span>
}
5 changes: 5 additions & 0 deletions src/npm-fastui/src/components/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import { JsonComp, JsonProps } from './Json'
import { ServerLoadComp, ServerLoadProps } from './ServerLoad'
import { ImageComp, ImageProps } from './image'
import { IframeComp, IframeProps } from './Iframe'
import { FormattedTextComp, FormattedTextProps } from './FormattedText'

export type {
TextProps,
Expand Down Expand Up @@ -67,6 +68,7 @@ export type {
ServerLoadProps,
ImageProps,
IframeProps,
FormattedTextProps,
}

// TODO some better way to export components
Expand Down Expand Up @@ -97,6 +99,7 @@ export type FastProps =
| ServerLoadProps
| ImageProps
| IframeProps
| FormattedTextProps

export type FastClassNameProps = Exclude<FastProps, TextProps | AllDisplayProps | ServerLoadProps | PageTitleProps>

Expand Down Expand Up @@ -179,6 +182,8 @@ export const AnyComp: FC<FastProps> = (props) => {
return <ImageComp {...props} />
case 'Iframe':
return <IframeComp {...props} />
case 'FormattedText':
return <FormattedTextComp {...props} />
default:
unreachable('Unexpected component type', type, props)
return <DisplayError title="Invalid Server Response" description={`Unknown component type: "${type}"`} />
Expand Down
15 changes: 13 additions & 2 deletions src/python-fastui/fastui/components/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class Text(pydantic.BaseModel, extra='forbid'):


class Paragraph(pydantic.BaseModel, extra='forbid'):
text: str
text: '_t.List[_t.Union[str, FormattedText, Link]]'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
text: '_t.List[_t.Union[str, FormattedText, Link]]'
text: '_t.Union[str, _t.List[_t.Union[str, FormattedText, Link]]]'

you need to support just str as well.

Then you need to update Paragraph and Button to render this.

I guess Text should also be changed.

type: _t.Literal['Paragraph'] = 'Paragraph'


Expand Down Expand Up @@ -114,7 +114,7 @@ class Code(pydantic.BaseModel, extra='forbid'):


class Button(pydantic.BaseModel, extra='forbid'):
text: str
text: '_t.List[_t.Union[str, FormattedText, Link]]'
on_click: _t.Union[events.AnyEvent, None] = pydantic.Field(default=None, serialization_alias='onClick')
html_type: _t.Union[_t.Literal['button', 'submit', 'reset'], None] = pydantic.Field(
default=None, serialization_alias='htmlType'
Expand Down Expand Up @@ -202,6 +202,17 @@ class Iframe(pydantic.BaseModel, extra='forbid'):
type: _t.Literal['Iframe'] = 'Iframe'


class FormattedText(pydantic.BaseModel, extra='forbid'):
text: str
text_format: _t.Union[_t.Literal['bold', 'italic', 'underline', 'strikethrough'], None] = pydantic.Field(
None, serialization_alias='textFormat'
)
# TODO, use pydantic-extra-types Color?
text_color: _t.Union[str, None] = pydantic.Field(None, serialization_alias='color')
background_color: _t.Union[str, None] = pydantic.Field(None, serialization_alias='backgroundColor')
type: _t.Literal['FormattedText'] = 'FormattedText'


AnyComponent = _te.Annotated[
_t.Union[
Text,
Expand Down