-
Notifications
You must be signed in to change notification settings - Fork 202
[BpkLabel] Migrate component to TypeScript #4110
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
Changes from all commits
af7f730
33fa7be
be9af0f
5d84a61
1e1060a
6a2a540
24912e5
60d75a9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
| /* | ||
| * Backpack - Skyscanner's Design System | ||
| * | ||
| * Copyright 2016 Skyscanner Ltd | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| import { render } from '@testing-library/react'; | ||
| import '@testing-library/jest-dom'; | ||
|
|
||
| import BpkLabel from './BpkLabel'; | ||
|
|
||
| describe('BpkLabel', () => { | ||
| it('should render correctly', () => { | ||
| const { container } = render(<BpkLabel>Origin</BpkLabel>); | ||
| const label = container.querySelector('label'); | ||
|
|
||
| expect(label).toBeInTheDocument(); | ||
| expect(label).toHaveClass('bpk-label'); | ||
| expect(label).toHaveTextContent('Origin'); | ||
| expect(label?.querySelector('.bpk-label__asterisk')).not.toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it('should render correctly with a "white" attribute', () => { | ||
| const { container } = render(<BpkLabel white>Origin</BpkLabel>); | ||
| const label = container.querySelector('label'); | ||
|
|
||
| expect(label).toBeInTheDocument(); | ||
| expect(label).toHaveClass('bpk-label'); | ||
| expect(label).toHaveClass('bpk-label--white'); | ||
| expect(label).toHaveTextContent('Origin'); | ||
| }); | ||
|
|
||
| it('should render correctly with a "required" attribute', () => { | ||
| const { container } = render(<BpkLabel required>Origin</BpkLabel>); | ||
| const label = container.querySelector('label'); | ||
| const asterisk = label?.querySelector('.bpk-label__asterisk'); | ||
|
|
||
| expect(label).toBeInTheDocument(); | ||
| expect(label).toHaveClass('bpk-label'); | ||
| expect(label).toHaveTextContent('Origin'); | ||
| expect(asterisk).toBeInTheDocument(); | ||
| expect(asterisk).toHaveTextContent('*'); | ||
| }); | ||
|
|
||
| it('should render correctly with "valid" attribute false', () => { | ||
| const { container } = render(<BpkLabel valid={false}>Origin</BpkLabel>); | ||
| const label = container.querySelector('label'); | ||
|
|
||
| expect(label).toBeInTheDocument(); | ||
| expect(label).toHaveClass('bpk-label'); | ||
| expect(label).toHaveClass('bpk-label--invalid'); | ||
| expect(label).toHaveTextContent('Origin'); | ||
| }); | ||
|
|
||
| it('should render correctly with "valid" attribute false and "required" attributes', () => { | ||
| const { container } = render( | ||
| <BpkLabel required valid={false}> | ||
| Origin | ||
| </BpkLabel>, | ||
| ); | ||
| const label = container.querySelector('label'); | ||
| const asterisk = label?.querySelector('.bpk-label__asterisk'); | ||
|
|
||
| expect(label).toBeInTheDocument(); | ||
| expect(label).toHaveClass('bpk-label'); | ||
| expect(label).toHaveClass('bpk-label--invalid'); | ||
| expect(label).toHaveTextContent('Origin'); | ||
| expect(asterisk).toBeInTheDocument(); | ||
| expect(asterisk).toHaveTextContent('*'); | ||
| }); | ||
|
|
||
| it('should render correctly without an asterisk when disabled and required', () => { | ||
| const { container } = render( | ||
| <BpkLabel disabled required> | ||
| Origin | ||
| </BpkLabel>, | ||
| ); | ||
| const label = container.querySelector('label'); | ||
|
|
||
| expect(label).toBeInTheDocument(); | ||
| expect(label).toHaveClass('bpk-label'); | ||
| expect(label).toHaveClass('bpk-label--disabled'); | ||
| expect(label).toHaveTextContent('Origin'); | ||
| expect(label?.querySelector('.bpk-label__asterisk')).not.toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it('should render correctly with a "className" attribute', () => { | ||
| const { container } = render(<BpkLabel className="test">Origin</BpkLabel>); | ||
| const label = container.querySelector('label'); | ||
|
|
||
| expect(label).toBeInTheDocument(); | ||
| expect(label).toHaveClass('bpk-label'); | ||
| expect(label).toHaveClass('test'); | ||
| expect(label).toHaveTextContent('Origin'); | ||
| }); | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,10 +15,8 @@ | |
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| /* @flow strict */ | ||
|
|
||
| import PropTypes from 'prop-types'; | ||
| import type { Node } from 'react'; | ||
| import type { ComponentPropsWithoutRef, ReactNode } from 'react'; | ||
|
|
||
| import { cssModules } from '../../bpk-react-utils'; | ||
|
|
||
|
|
@@ -27,17 +25,17 @@ import STYLES from './BpkLabel.module.scss'; | |
| const getClassName = cssModules(STYLES); | ||
|
|
||
| export type Props = { | ||
| children: Node, | ||
| className: ?string, | ||
| disabled: boolean, | ||
| valid: ?boolean, | ||
| required: boolean, | ||
| white: boolean, | ||
| }; | ||
| children: ReactNode; | ||
| className?: string; | ||
| disabled?: boolean; | ||
| valid?: boolean | null; | ||
|
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. There is nowhere use
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. Hi Ezreal (@Ezreal-Yang), thanks for asking! I have considered this question. Currently, the I'm unsure how this will be used downstream, especially in cases where |
||
| required?: boolean; | ||
| white?: boolean; | ||
| } & ComponentPropsWithoutRef<'label'>; | ||
|
|
||
| const BpkLabel = ({ | ||
| children, | ||
| className = null, | ||
| className, | ||
| disabled = false, | ||
| required = false, | ||
| valid = null, | ||
|
|
@@ -56,7 +54,6 @@ const BpkLabel = ({ | |
| ); | ||
|
|
||
| return ( | ||
| // $FlowFixMe[cannot-spread-inexact] - inexact rest. See 'decisions/flowfixme.md'. | ||
| <label className={classNames} {...rest}> | ||
| {children} | ||
| {!disabled && required && ( | ||
|
|
@@ -66,15 +63,4 @@ const BpkLabel = ({ | |
| ); | ||
| }; | ||
|
|
||
| BpkLabel.propTypes = { | ||
| children: PropTypes.node.isRequired, | ||
| className: PropTypes.string, | ||
| disabled: PropTypes.bool, | ||
| valid: PropTypes.bool, | ||
| required: PropTypes.bool, | ||
| white: PropTypes.bool, | ||
| }; | ||
|
|
||
|
|
||
|
|
||
| export default BpkLabel; | ||
This file was deleted.
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.
The
validis supposed to be a boolean and only has 2 values, but it requires changing the code below. To keep the TS migration pure and safe, keep it as it was for now.