-
Notifications
You must be signed in to change notification settings - Fork 61
[UIK-5011][checkbox] rewrite component to ts #2821
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: release/v17
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| import { Box, Flex, InvalidStateBox } from '@semcore/base-components'; | ||
| import type { IRootComponentProps } from '@semcore/core'; | ||
| import { createComponent, Component, sstyled, Root } from '@semcore/core'; | ||
| import { callAllEventHandlers } from '@semcore/core/lib/utils/assignProps'; | ||
| import resolveColorEnhance from '@semcore/core/lib/utils/enhances/resolveColorEnhance'; | ||
|
|
@@ -8,9 +9,14 @@ import { useColorResolver } from '@semcore/core/lib/utils/use/useColorResolver'; | |
| import { Text as TypographyText } from '@semcore/typography'; | ||
| import React from 'react'; | ||
|
|
||
| import type { CheckboxComponent, CheckboxProps, CheckboxTextProps, CheckboxValueCheckMarkProps, CheckboxValueComponent, CheckboxValueControlProps, CheckboxValueProps } from './Checkbox.type'; | ||
| import style from './style/checkbox.shadow.css'; | ||
|
|
||
| class CheckboxRoot extends Component { | ||
| type State = { | ||
| hoistedDisabled?: boolean; | ||
| }; | ||
|
|
||
| class CheckboxRoot extends Component<CheckboxProps, never, {}, State> { | ||
| static displayName = 'Checkbox'; | ||
| static style = style; | ||
|
|
||
|
|
@@ -24,7 +30,7 @@ class CheckboxRoot extends Component { | |
| hoistedDisabled: undefined, | ||
| }; | ||
|
|
||
| hoistDisabled = (disabled) => { | ||
| hoistDisabled = (disabled: CheckboxProps['disabled']) => { | ||
| logger.warn( | ||
| true, | ||
| `Don't set disabled on Checkbox.Value or Checkbox.Text, set it on Checkbox instead. Otherwise it will produce wrong SSR output.`, | ||
|
|
@@ -100,31 +106,30 @@ class CheckboxRoot extends Component { | |
| } | ||
| } | ||
|
|
||
| class ValueRoot extends Component { | ||
| static defaultProps = (props) => { | ||
| type ValuePropsFromRoot = ReturnType<InstanceType<typeof CheckboxRoot>['getValueProps']>; | ||
| class ValueRoot extends Component< | ||
| ValuePropsFromRoot & CheckboxValueProps, | ||
| typeof ValueRoot.enhance, | ||
| { checked: (e: React.ChangeEvent<HTMLInputElement>) => boolean } | ||
| > { | ||
| static defaultProps = () => { | ||
| return { | ||
| includeInputProps: [ | ||
| ...inputProps, | ||
| ...(props.includeInputProps ?? []), | ||
| 'aria-label', | ||
| 'aria-labelledby', | ||
| 'aria-describedby', | ||
| ], | ||
| includeInputProps: [...inputProps, 'aria-label', 'aria-labelledby', 'aria-describedby'], | ||
| }; | ||
| }; | ||
|
|
||
| static enhance = [resolveColorEnhance()]; | ||
| static enhance = [resolveColorEnhance()] as const; | ||
| static displayName = 'Value'; | ||
| static style = style; | ||
|
|
||
| handleClick(e) { | ||
| handleClick(e: React.MouseEvent) { | ||
| // idk for what it exists, leaving just in case it saves us from some bugs | ||
| e.stopPropagation(); | ||
| } | ||
|
|
||
| uncontrolledProps() { | ||
| return { | ||
| checked: (e) => e.target.checked, | ||
| checked: (e: React.ChangeEvent<HTMLInputElement>) => e.target.checked, | ||
| }; | ||
| } | ||
|
|
||
|
|
@@ -151,19 +156,21 @@ class ValueRoot extends Component { | |
| indeterminate, | ||
| includeInputProps, | ||
| resolveColor, | ||
| children, | ||
| Children, | ||
| ...other | ||
| } = this.asProps; | ||
| const [, checkMarkProps] = getInputProps(other, includeInputProps); | ||
| const { children: _children, Children: _Children, ...propsWithoutChildren } = checkMarkProps; | ||
|
|
||
| return { | ||
| theme, | ||
| theme: resolveColor(theme), | ||
| size, | ||
| state, | ||
| // keyboardFocused, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. let's remove this |
||
| checked, | ||
| indeterminate, | ||
| resolveColor, | ||
| ...propsWithoutChildren, | ||
| ...checkMarkProps, | ||
| }; | ||
| } | ||
|
|
||
|
|
@@ -195,13 +202,16 @@ class ValueRoot extends Component { | |
| } | ||
| } | ||
|
|
||
| function Control(props) { | ||
| type ControlPropsFromRoot = ReturnType<InstanceType<typeof ValueRoot>['getControlProps']>; | ||
| function Control(props: IRootComponentProps & ControlPropsFromRoot & CheckboxValueControlProps) { | ||
| const SControl = Root; | ||
| const { indeterminate, styles, state } = props; | ||
| const checkboxRef = React.useRef(null); | ||
| const checkboxRef = React.useRef<HTMLInputElement>(null); | ||
|
|
||
| React.useEffect(() => { | ||
| if (checkboxRef.current) { | ||
| if (indeterminate === undefined) return; | ||
|
|
||
| checkboxRef.current.indeterminate = indeterminate; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think, it's better to set |
||
| } | ||
| }, [indeterminate, checkboxRef]); | ||
|
|
@@ -218,24 +228,28 @@ function Control(props) { | |
| } | ||
| Control.displayName = 'Control'; | ||
|
|
||
| function CheckMark(props) { | ||
| type CheckMarkPropsFromRoot = ReturnType<InstanceType<typeof ValueRoot>['getCheckMarkProps']>; | ||
| function CheckMark(props: IRootComponentProps & CheckMarkPropsFromRoot & CheckboxValueCheckMarkProps) { | ||
| const SCheckbox = Root; | ||
| const SInvalidPattern = InvalidStateBox; | ||
| const { theme, styles, resolveColor, state, checked, indeterminate } = props; | ||
| const { styles, state, checked, indeterminate } = props; | ||
| return sstyled(styles)( | ||
| <SCheckbox render={Flex} tag='span' use:theme={resolveColor(theme)}> | ||
| <SCheckbox render={Flex} tag='span'> | ||
| {state === 'invalid' && !checked && !indeterminate && <SInvalidPattern />} | ||
| </SCheckbox>, | ||
| ); | ||
| } | ||
| CheckMark.displayName = 'CheckMark'; | ||
|
|
||
| function Text(props) { | ||
| type TextPropsFromRoot = ReturnType<InstanceType<typeof CheckboxRoot>['getTextProps']>; | ||
| function Text(props: IRootComponentProps & TextPropsFromRoot & CheckboxTextProps) { | ||
| const SText = Root; | ||
| const { styles, color } = props; | ||
|
|
||
| React.useEffect(() => { | ||
| if (props.rootDisabled !== props.disabled) { | ||
| if (props.disabled === undefined) return; | ||
|
|
||
| props.hoistDisabled(props.disabled); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And |
||
| } | ||
| }, [props.rootDisabled, props.disabled, props.hoistDisabled]); | ||
|
|
@@ -251,11 +265,11 @@ Text.displayName = 'Text'; | |
| const Value = createComponent(ValueRoot, { | ||
| Control, | ||
| CheckMark, | ||
| }); | ||
| }) as CheckboxValueComponent; | ||
|
|
||
| const Checkbox = createComponent(CheckboxRoot, { | ||
| Text, | ||
| Value, | ||
| }); | ||
| export { inputProps }; | ||
| }) as CheckboxComponent; | ||
|
|
||
| export default Checkbox; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,2 @@ | ||
| export { default } from './Checkbox'; | ||
| export * from './Checkbox'; | ||
| export * from './Checkbox.type'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why did you remove this?