Skip to content

Commit

Permalink
Add defaultTextFont prop to PortingEmbed (#9)
Browse files Browse the repository at this point in the history
Removing "Satoshi-Regular" as the default font and adding a
`defaultTextFont` prop to PortingEmbed so that a custom default font can
be passed instead.
  • Loading branch information
dharmadeveloper108 authored Apr 19, 2024
1 parent 06f6c48 commit 265626a
Show file tree
Hide file tree
Showing 10 changed files with 123 additions and 4 deletions.
11 changes: 11 additions & 0 deletions docs/porting-embed.md
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,16 @@ renderPortingProtectionDisabledConfirmation={(onConfirm) => (
)}
/>
```
#### `defaultTextFont`
The default font that the embed uses for all default components, i.e. all components that are not replaced by a corresponding render prop (for example all inputs will use the `defaultTextFont` if no `renderInput` component was passed to the embed).
```jsx
<PortingEmbed
//...
defaultTextFont='Custom-font'
/>
```
#### Porting Embed props
Expand All @@ -347,6 +357,7 @@ renderPortingProtectionDisabledConfirmation={(onConfirm) => (
| `renderAlertBanner` | function | ❌ | `(variant: 'error' | 'info', message: string) => React.ReactNode` |
| `renderDate` | function | ❌ | `(name: string, onChange: (value: string) => void) => React.ReactNode` |
| `renderPortingProtectionDisabledConfirmation` | function | ❌ | `(onConfirm: () => void) => React.ReactNode` |
| `defaultTextFont` | string | ❌ | Custom font used in all default components. |
#### Text
Expand Down
1 change: 1 addition & 0 deletions example/app/porting/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ export default function PortingEmbedScreen() {
onCompleted={() => setCompleted(true)}
onSupportRequested={() => setClickedCustomerSupport(true)}
onPortingStep={(step) => setPortingStep(step)}
defaultTextFont="Satoshi-Regular"
renderTitle={(step) =>
step != null && (
<Text style={[customStyles.title]}>{titles[step]}</Text>
Expand Down
3 changes: 3 additions & 0 deletions src/PortingEmbed/CustomOptionsProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ type EmbedOptions = {
renderPortingProtectionDisabledConfirmation?: (
onConfirm: () => void
) => React.ReactNode
defaultTextFont?: string
}

export const CustomOptionsContext = createContext<EmbedOptions | null>(null)
Expand All @@ -46,6 +47,7 @@ export function CustomOptionsProvider({
renderAlertBanner,
renderDate,
renderPortingProtectionDisabledConfirmation,
defaultTextFont,
children,
}: Props) {
return (
Expand All @@ -59,6 +61,7 @@ export function CustomOptionsProvider({
renderAlertBanner,
renderDate,
renderPortingProtectionDisabledConfirmation,
defaultTextFont,
}}
>
{children}
Expand Down
3 changes: 3 additions & 0 deletions src/PortingEmbed/PortingEmbed.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ type Props = {
renderPortingProtectionDisabledConfirmation?: (
onConfirm: () => void
) => React.ReactNode
defaultTextFont?: string
}

export function PortingEmbed({
Expand All @@ -60,6 +61,7 @@ export function PortingEmbed({
renderAlertBanner,
renderDate,
renderPortingProtectionDisabledConfirmation,
defaultTextFont,
}: Props) {
return (
<ConnectSessionProvider
Expand All @@ -80,6 +82,7 @@ export function PortingEmbed({
renderPortingProtectionDisabledConfirmation={
renderPortingProtectionDisabledConfirmation
}
defaultTextFont={defaultTextFont}
>
<PortingFormContainer
onLoaded={onLoaded}
Expand Down
21 changes: 21 additions & 0 deletions src/PortingEmbed/__tests__/AccountHolderForm.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -335,4 +335,25 @@ describe('AccountHolderForm', () => {
color: 'red',
})
})

it('uses the default font if present', async () => {
render(
<CustomOptionsContext.Provider
value={{
defaultTextFont: 'Custom-font',
}}
>
<AccountHolderForm
onSubmit={onSaveMock}
porting={{ ...porting, firstName: null, lastName: null }}
/>
</CustomOptionsContext.Provider>
)

const button = screen.getByText('Save')

expect(button).toHaveStyle({
fontFamily: 'Custom-font',
})
})
})
21 changes: 21 additions & 0 deletions src/PortingEmbed/__tests__/AddressForm.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -295,4 +295,25 @@ describe('AddressForm', () => {
color: 'red',
})
})
it('uses the default font if present', async () => {
render(
<CustomOptionsContext.Provider
value={{
defaultTextFont: 'Custom-font',
}}
>
<AddressForm
onSubmit={onSaveMock}
porting={{ ...porting, address: null }}
error="Something went very wrong."
/>
</CustomOptionsContext.Provider>
)

const button = screen.getByText('Save')

expect(button).toHaveStyle({
fontFamily: 'Custom-font',
})
})
})
21 changes: 21 additions & 0 deletions src/PortingEmbed/__tests__/CarrierDetailsForm.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -227,4 +227,25 @@ describe('CarrierInfoForm', () => {
color: 'red',
})
})
it('uses the default font if present', async () => {
render(
<CustomOptionsContext.Provider
value={{
defaultTextFont: 'Custom-font',
}}
>
<CarrierInfoForm
onSubmit={onSaveMock}
porting={{ ...porting, accountNumber: null, accountPinExists: false }}
error="Something went very wrong."
/>
</CustomOptionsContext.Provider>
)

const button = screen.getByText('Save')

expect(button).toHaveStyle({
fontFamily: 'Custom-font',
})
})
})
20 changes: 20 additions & 0 deletions src/PortingEmbed/__tests__/DonorApprovalForm.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -151,4 +151,24 @@ describe('DonorApprovalForm', () => {
donorProviderApproval: true,
})
})
it('uses the default font if present', async () => {
render(
<CustomOptionsContext.Provider
value={{
defaultTextFont: 'Custom-font',
}}
>
<DonorApprovalForm
onSubmit={onSaveMock}
porting={{ ...porting, address: null }}
/>
</CustomOptionsContext.Provider>
)

const button = screen.getByText('Save')

expect(button).toHaveStyle({
fontFamily: 'Custom-font',
})
})
})
12 changes: 11 additions & 1 deletion src/components/EmbedText.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
import { Text, TextProps } from 'react-native'

import { useOptionsContext } from '../PortingEmbed/CustomOptionsProvider'

type Props = {
children: React.ReactNode
} & TextProps

export const EmbedText = (props: Props) => {
const { style, children } = props
const options = useOptionsContext()

return (
<Text style={[style, { fontFamily: 'Satoshi-Regular' }]}>{children}</Text>
<Text
style={[
style,
options?.defaultTextFont ? { fontFamily: options.defaultTextFont } : {},
]}
>
{children}
</Text>
)
}
14 changes: 11 additions & 3 deletions src/components/Input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
TextInputProps,
} from 'react-native'

import { useOptionsContext } from '../PortingEmbed/CustomOptionsProvider'
import { EmbedText } from './EmbedText'

type Props = {
Expand All @@ -18,6 +19,7 @@ type Props = {
export function Input({ error, placeholder, ...rest }: Props) {
const [inputText, setInputText] = useState('')
const animatedValue = useRef(new Animated.Value(0))
const options = useOptionsContext()

const returnAnimatedLabelStyles = {
transform: [
Expand Down Expand Up @@ -65,7 +67,6 @@ export function Input({ error, placeholder, ...rest }: Props) {
borderRadius: 8,
fontWeight: '500',
borderWidth: 0,
fontFamily: 'Satoshi-Regular',
},
})

Expand All @@ -81,8 +82,10 @@ export function Input({ error, placeholder, ...rest }: Props) {
pointerEvents: 'none',
color: '#6b7280',
height: 20,
fontFamily: 'Satoshi-Regular',
},
options?.defaultTextFont
? { fontFamily: options.defaultTextFont }
: {},
]}
>
{placeholder}
Expand All @@ -91,7 +94,12 @@ export function Input({ error, placeholder, ...rest }: Props) {
onChange={(e: NativeSyntheticEvent<TextInputChangeEventData>): void =>
setInputText(e.nativeEvent.text)
}
style={styles.textStyle}
style={[
styles.textStyle,
options?.defaultTextFont
? { fontFamily: options.defaultTextFont }
: {},
]}
placeholderTextColor={error ? '#e11d48' : '#6b7280'}
{...rest}
/>
Expand Down

0 comments on commit 265626a

Please sign in to comment.