Skip to content

Commit aea0f51

Browse files
committed
feat(CFormControl): split CFormControl to CFormInput and CFormTextarea
1 parent e399de0 commit aea0f51

File tree

2 files changed

+158
-0
lines changed

2 files changed

+158
-0
lines changed

src/components/form/CFormInput.tsx

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
import React, { ChangeEventHandler, forwardRef, HTMLAttributes } from 'react'
2+
import PropTypes from 'prop-types'
3+
import classNames from 'classnames'
4+
// import { CFormLabel } from './CFormLabel'
5+
6+
export interface CFormInputProps extends HTMLAttributes<HTMLInputElement> {
7+
/**
8+
* A string of all className you want applied to the component. [docs]
9+
*/
10+
className?: string
11+
/**
12+
* Toggle the disabled state for the component. [docs]
13+
*/
14+
disabled?: boolean
15+
/**
16+
* Set component validation state to invalid. [docs]
17+
*/
18+
invalid?: boolean
19+
// label?: string | ReactNode
20+
/**
21+
* Method called immediately after the `value` prop changes. [docs]
22+
*/
23+
onChange?: ChangeEventHandler<HTMLInputElement>
24+
/**
25+
* Render the component styled as plain text. Removes the default form field styling and preserve the correct margin and padding. Recommend to use only along side `readonly` [docs]
26+
*/
27+
plainText?: boolean
28+
/**
29+
* Toggle the readonly state for the component. [docs]
30+
*/
31+
readOnly?: boolean
32+
/**
33+
* Size the component small or large. [docs]
34+
*
35+
* @type 'sm' | 'lg'
36+
*/
37+
size?: 'sm' | 'lg'
38+
/**
39+
* Specifies the type of component. [docs]
40+
*
41+
* @type 'color' | 'file' | 'text' | string
42+
* @default 'text'
43+
*/
44+
type?: 'color' | 'file' | 'text' | string
45+
/**
46+
* Set component validation state to valid. [docs]
47+
*/
48+
valid?: boolean
49+
/**
50+
* The `value` attribute of component. [docs]
51+
*
52+
* @controllable onChange
53+
* */
54+
value?: string | string[] | number
55+
}
56+
57+
export const CFormInput = forwardRef<HTMLInputElement, CFormInputProps>(
58+
({ children, className, invalid, plainText, size, type = 'text', valid, ...rest }, ref) => {
59+
const _className = classNames(
60+
plainText ? 'form-control-plaintext' : 'form-control',
61+
{
62+
[`form-control-${size}`]: size,
63+
'form-control-color': type === 'color',
64+
'is-invalid': invalid,
65+
'is-valid': valid,
66+
},
67+
className,
68+
)
69+
return (
70+
<input type={type} className={_className} {...rest} ref={ref}>
71+
{children}
72+
</input>
73+
)
74+
},
75+
)
76+
77+
CFormInput.propTypes = {
78+
children: PropTypes.node,
79+
className: PropTypes.string,
80+
invalid: PropTypes.bool,
81+
plainText: PropTypes.bool,
82+
size: PropTypes.oneOf(['sm', 'lg']),
83+
type: PropTypes.oneOfType([PropTypes.oneOf(['color', 'file', 'text']), PropTypes.string]),
84+
valid: PropTypes.bool,
85+
}
86+
87+
CFormInput.displayName = 'CFormInput'

src/components/form/CFormTextarea.tsx

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import React, { ChangeEventHandler, ElementType, forwardRef, HTMLAttributes } from 'react'
2+
import PropTypes from 'prop-types'
3+
import classNames from 'classnames'
4+
5+
export interface CFormTextareaProps extends HTMLAttributes<HTMLTextAreaElement> {
6+
/**
7+
* A string of all className you want applied to the component. [docs]
8+
*/
9+
className?: string
10+
/**
11+
* Toggle the disabled state for the component. [docs]
12+
*/
13+
disabled?: boolean
14+
/**
15+
* Set component validation state to invalid. [docs]
16+
*/
17+
invalid?: boolean
18+
/**
19+
* Method called immediately after the `value` prop changes. [docs]
20+
*/
21+
onChange?: ChangeEventHandler<HTMLTextAreaElement>
22+
/**
23+
* Render the component styled as plain text. Removes the default form field styling and preserve the correct margin and padding. Recommend to use only along side `readonly` [docs]
24+
*/
25+
plainText?: boolean
26+
/**
27+
* Toggle the readonly state for the component. [docs]
28+
*/
29+
readOnly?: boolean
30+
/**
31+
* Set component validation state to valid. [docs]
32+
*/
33+
valid?: boolean
34+
/**
35+
* The `value` attribute of component. [docs]
36+
*
37+
* @controllable onChange
38+
* */
39+
value?: string | string[] | number
40+
}
41+
42+
export const CFormTextarea = forwardRef<HTMLTextAreaElement, CFormTextareaProps>(
43+
({ children, className, customClassName, invalid, plainText, valid, ...rest }, ref) => {
44+
const _className = customClassName
45+
? customClassName
46+
: classNames(
47+
plainText ? 'form-control-plaintext' : 'form-control',
48+
{
49+
'is-invalid': invalid,
50+
'is-valid': valid,
51+
},
52+
className,
53+
)
54+
return (
55+
<textarea className={_className} {...rest} ref={ref}>
56+
{children}
57+
</textarea>
58+
)
59+
},
60+
)
61+
62+
CFormTextarea.propTypes = {
63+
children: PropTypes.node,
64+
className: PropTypes.string,
65+
customClassName: PropTypes.string,
66+
invalid: PropTypes.bool,
67+
plainText: PropTypes.bool,
68+
valid: PropTypes.bool,
69+
}
70+
71+
CFormTextarea.displayName = 'CFormTextarea'

0 commit comments

Comments
 (0)