-
Notifications
You must be signed in to change notification settings - Fork 317
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(ui-react-native): Migrate Checkbox primitive (#2621)
- Loading branch information
1 parent
ce8dd26
commit a7f6af4
Showing
28 changed files
with
535 additions
and
68 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
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
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
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
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
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
27 changes: 27 additions & 0 deletions
27
examples/react-native/storybook/stories/Checkbox.stories.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,27 @@ | ||
import React, { useState } from 'react'; | ||
import { storiesOf } from '@storybook/react-native'; | ||
import { select } from '@storybook/addon-knobs'; | ||
import { Checkbox } from '@aws-amplify/ui-react-native/dist/primitives'; | ||
|
||
const StatefulCheckbox = ({ ...props }: any) => { | ||
const [value, setValue] = useState(props.value); | ||
const onChange = (nextValue: string) => { | ||
setValue(nextValue); | ||
}; | ||
|
||
return <Checkbox {...props} value={value} onChange={onChange} size={20} />; | ||
}; | ||
storiesOf('Checkbox', module) | ||
.add('default', () => <StatefulCheckbox />) | ||
.add('with Label', () => ( | ||
<StatefulCheckbox | ||
label="Label" | ||
labelPosition={select( | ||
'LabelPosition', | ||
['start', 'end', 'top', 'bottom'], | ||
'end' | ||
)} | ||
/> | ||
)) | ||
.add('selected', () => <StatefulCheckbox selected />) | ||
.add('disabled', () => <StatefulCheckbox label="Disabled" disabled />); |
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
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
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
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
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
File renamed without changes
File renamed without changes
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
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
50 changes: 50 additions & 0 deletions
50
packages/react-native/src/primitives/Checkbox/__tests__/Checkbox.spec.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,50 @@ | ||
import React from 'react'; | ||
import { fireEvent, render } from '@testing-library/react-native'; | ||
import Checkbox from '../Checkbox'; | ||
|
||
const onChange = jest.fn(); | ||
|
||
describe('Checkbox', () => { | ||
beforeEach(() => { | ||
onChange.mockClear(); | ||
}); | ||
|
||
[true, false].forEach((value) => { | ||
it(`renders as expected when selected is ${value}`, () => { | ||
const { toJSON } = render( | ||
<Checkbox selected={value} value={value} onChange={onChange} /> | ||
); | ||
expect(toJSON()).toMatchSnapshot(); | ||
}); | ||
}); | ||
|
||
it('renders as expected when disabled', () => { | ||
const { toJSON } = render( | ||
<Checkbox disabled value="" onChange={onChange} /> | ||
); | ||
expect(toJSON()).toMatchSnapshot(); | ||
}); | ||
|
||
it('renders as expected with accessibilityRole', () => { | ||
const { toJSON } = render( | ||
<Checkbox value="" onChange={onChange} accessibilityRole={'none'} /> | ||
); | ||
expect(toJSON()).toMatchSnapshot(); | ||
}); | ||
|
||
it('calls the expected handler when selected', () => { | ||
const { getByRole } = render(<Checkbox value="" onChange={onChange} />); | ||
const checkbox = getByRole('checkbox'); | ||
fireEvent.press(checkbox); | ||
expect(onChange).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it('does nothing when disabled', () => { | ||
const { getByRole } = render( | ||
<Checkbox value="" onChange={onChange} disabled /> | ||
); | ||
const checkbox = getByRole('checkbox'); | ||
fireEvent.press(checkbox); | ||
expect(onChange).not.toHaveBeenCalled(); | ||
}); | ||
}); |
Oops, something went wrong.