Skip to content

Commit

Permalink
feat: useIndent hook
Browse files Browse the repository at this point in the history
  • Loading branch information
stijnvanhulle committed Oct 9, 2023
1 parent 0b5980f commit 27a5f0f
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 30 deletions.
34 changes: 4 additions & 30 deletions packages/react-template/src/components/Text.tsx
Original file line number Diff line number Diff line change
@@ -1,41 +1,15 @@
import React from 'react'

import type { ReactNode } from 'react'
import { useIndent } from '../hooks/useIndent'

type Props = {
children?: ReactNode
children?: React.ReactNode
indentSize?: number
}

export function createIndent(size: number): string {
return Array.from({ length: size }).join(' ')
}

export function Text({ indentSize = 0, children }: Props): React.ReactNode {
const indentBefore = createIndent(indentSize)

let indentChildren

if (indentSize && typeof children === 'string') {
indentChildren = children.replaceAll('\n', `\n${createIndent(4)}`)
}

if (indentSize && Array.isArray(children)) {
indentChildren = children.map((child) => {
let text: string = child as string

if (typeof text === 'string') {
if (text.startsWith('\n')) {
text = text.replace('\n', '')
}
if (text.substring(text.length - 1, text.length) === '\n') {
text = text.substring(0, text.length - 2)
}
text = text.replaceAll('\n', `\n${createIndent(4)}`)
}
return text
})
}
const indentBefore = useIndent({ size: indentSize })
const indentChildren = useIndent({ size: 4, children })

return (
<kubb-text>
Expand Down
1 change: 1 addition & 0 deletions packages/react-template/src/hooks/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './useApp.ts'
export * from './useMeta.ts'
export * from './useIndent.ts'
39 changes: 39 additions & 0 deletions packages/react-template/src/hooks/useIndent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
type Props = {
size: number
children?: React.ReactNode
}

function createIndent(size: number): string {
return Array.from({ length: size }).join(' ')
}

export function useIndent({ size, children }: Props): React.ReactNode {
let indentWithChildren: React.ReactNode

if (!children) {
return createIndent(size)
}

if (typeof children === 'string') {
indentWithChildren = children.replaceAll('\n', `\n${createIndent(size)}`)
}

if (Array.isArray(children)) {
indentWithChildren = children.map((child) => {
let text: string = child as string

if (typeof text === 'string') {
if (text.startsWith('\n')) {
text = text.replace('\n', '')

Check failure

Code scanning / CodeQL

Incomplete string escaping or encoding High

This replaces only the first occurrence of '\n'.
}
if (text.substring(text.length - 1, text.length) === '\n') {
text = text.substring(0, text.length - 2)
}
text = text.replaceAll('\n', `\n${createIndent(size)}`)
}
return text
})
}

return indentWithChildren
}

0 comments on commit 27a5f0f

Please sign in to comment.