-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(FormLabel): rewrite to TypeScript
- Loading branch information
1 parent
72de227
commit 083c3d3
Showing
9 changed files
with
181 additions
and
247 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 0 additions & 59 deletions
59
packages/dnb-eufemia/src/components/form-label/FormLabel.d.ts
This file was deleted.
Oops, something went wrong.
154 changes: 0 additions & 154 deletions
154
packages/dnb-eufemia/src/components/form-label/FormLabel.js
This file was deleted.
Oops, something went wrong.
109 changes: 109 additions & 0 deletions
109
packages/dnb-eufemia/src/components/form-label/FormLabel.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
/** | ||
* Web FormLabel Component | ||
* | ||
*/ | ||
|
||
import React from 'react' | ||
import classnames from 'classnames' | ||
import { | ||
extendPropsWithContext, | ||
isTrue, | ||
validateDOMAttributes, | ||
} from '../../shared/component-helper' | ||
import { createSpacingClasses } from '../space/SpacingHelper' | ||
import { | ||
createSkeletonClass, | ||
skeletonDOMAttributes, | ||
} from '../skeleton/SkeletonHelper' | ||
import { pickFormElementProps } from '../../shared/helpers/filterValidProps' | ||
import Context from '../../shared/Context' | ||
import { | ||
DynamicElement, | ||
DynamicElementParams, | ||
SpacingProps, | ||
} from '../../shared/types' | ||
|
||
export type FormLabelProps = { | ||
forId?: string | ||
element?: DynamicElement<HTMLLabelElement> | ||
text?: React.ReactNode | ||
size?: 'basis' | 'medium' | 'large' | ||
id?: string | ||
skeleton?: boolean | ||
label?: React.ReactNode | ||
vertical?: boolean | ||
srOnly?: boolean | ||
innerRef?: React.RefObject<HTMLElement> | ||
|
||
/** Is not a part of HTMLLabelElement and not documented as of now */ | ||
disabled?: boolean | ||
|
||
/** @deprecated use forId instead */ | ||
for_id?: string | ||
/** @deprecated use srOnly instead */ | ||
sr_only?: boolean | ||
/** @deprecated use labelDirection instead (was not documented before) */ | ||
label_direction?: 'vertical' | 'horizontal' | ||
} | ||
|
||
export type FormLabelAllProps = FormLabelProps & | ||
React.HTMLAttributes<HTMLLabelElement> & | ||
SpacingProps | ||
|
||
export default function FormLabel(localProps: FormLabelAllProps) { | ||
const context = React.useContext(Context) | ||
|
||
// use only the props from context, who are available here anyway | ||
const props = extendPropsWithContext( | ||
localProps, | ||
null, | ||
{ skeleton: context?.skeleton }, | ||
pickFormElementProps(context?.FormRow), // Deprecated – can be removed in v11 | ||
pickFormElementProps(context?.formElement), | ||
context?.FormLabel | ||
) | ||
|
||
const { | ||
forId, | ||
text, | ||
srOnly, | ||
vertical, | ||
size, | ||
skeleton, | ||
element: Element = 'label', | ||
innerRef, | ||
className, | ||
children, | ||
|
||
/** @deprecated can be removed in v11 */ | ||
for_id, | ||
sr_only, | ||
label_direction, | ||
|
||
...attributes | ||
} = props | ||
|
||
const params = { | ||
className: classnames( | ||
'dnb-form-label', | ||
(isTrue(vertical) || label_direction === 'vertical') && | ||
`dnb-form-label--vertical`, | ||
(srOnly || isTrue(sr_only)) && 'dnb-sr-only', | ||
size && `dnb-h--${size}`, | ||
createSkeletonClass('font', skeleton, context), | ||
createSpacingClasses(props), | ||
className | ||
), | ||
htmlFor: forId || for_id, | ||
...(attributes as DynamicElementParams), | ||
} | ||
|
||
params['ref'] = innerRef | ||
|
||
skeletonDOMAttributes(params, skeleton, context) | ||
validateDOMAttributes(localProps, params) | ||
|
||
return <Element {...params}>{text || children}</Element> | ||
} | ||
|
||
FormLabel._supportsSpacingProps = true |
Oops, something went wrong.