Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

[PLAY-713] Convert kit to typescript #2444

Merged
merged 8 commits into from
Apr 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
135 changes: 0 additions & 135 deletions playbook/app/pb_kits/playbook/pb_textarea/_textarea.jsx

This file was deleted.

129 changes: 129 additions & 0 deletions playbook/app/pb_kits/playbook/pb_textarea/_textarea.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
/* eslint-disable react-hooks/rules-of-hooks */

import React, { forwardRef, useEffect, useRef } from 'react'
import classnames from 'classnames'

import PbTextarea from '.'
import type { InputCallback } from '../types'

import { buildAriaProps, buildDataProps } from '../utilities/props'
import { globalProps, GlobalProps } from '../utilities/globalProps'

import Body from '../pb_body/_body'
import Caption from '../pb_caption/_caption'
import Flex from '../pb_flex/_flex'
import FlexItem from '../pb_flex/_flex_item'

type TextareaProps = {
aria?: {[key: string]: string},
characterCount?: string,
className?: string,
children?: React.ReactChild[],
data?: {[key: string]: string},
disabled?: boolean,
error?: string,
id?: string,
inline?: boolean,
object?: string,
method?: string,
label?: string,
maxCharacters?: string,
placeholder?: string,
value?: string,
name?: string,
required?: boolean,
rows?: number,
resize: "none" | "both" | "horizontal" | "vertical" | "auto",
onChange?: InputCallback<HTMLTextAreaElement>,
} & GlobalProps

const Textarea = ({
aria = {},
characterCount,
className,
children,
data = {},
disabled,
inline = false,
resize = 'none',
error,
label,
maxCharacters,
name,
onChange = () => {},
placeholder,
required,
rows = 4,
value,
...props
}: TextareaProps, ref: any) => {
ref = useRef<HTMLTextAreaElement>(null)
useEffect(() => {
if (ref.current && resize === 'auto') {
PbTextarea.addMatch(ref.current)
}
})

const errorClass = error ? 'error' : null
const inlineClass = inline ? 'inline' : ''
const resizeClass = `resize_${resize}`
const classes = classnames('pb_textarea_kit', errorClass, inlineClass, resizeClass, globalProps(props), className)
const noCount = typeof characterCount !== 'undefined'
const ariaProps: {[key: string]: any} = buildAriaProps(aria)
const dataProps: {[key: string]: any} = buildDataProps(data)

const characterCounter = () => {
return maxCharacters && characterCount ? `${checkIfZero(characterCount)} / ${maxCharacters}` : `${checkIfZero(characterCount)}`
}

const checkIfZero = (characterCount: string | number) => {
return characterCount == 0 ? characterCount.toString() : characterCount
}

return (
<div
{...ariaProps}
{...dataProps}
className={classes}
>
<Caption text={label} />
{children || (
<textarea
className="pb_textarea_kit"
disabled={disabled}
name={name}
onChange={onChange}
placeholder={placeholder}
ref={ref}
required={required}
rows={rows}
value={value}
{...props}
/>
)}

{error ? (
<>
{characterCount ? (
<Flex spacing="between" vertical="center">
<FlexItem>
<Body margin="none" status="negative" text={error} />
</FlexItem>
<FlexItem>
<Caption margin="none" size="xs" text={characterCounter()} />
</FlexItem>
</Flex>
) : (
<Body status="negative" text={error} />
)}
</>
) : (
noCount && (
<Caption margin="none" size="xs" text={characterCounter()} />
)
)}
</div>
);
}

export default forwardRef(Textarea)
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import PbEnhancedElement from '../pb_enhanced_element'

export default class PbTextarea extends PbEnhancedElement {
style: {[key: string]: string}
scrollHeight: string
static get selector() {
return '.resize_auto textarea'
}
Expand Down
Loading