-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #100 from 8845musign/add-refs-to-forms
Allow refs props to Form Components(exclude <Label>)
- Loading branch information
Showing
20 changed files
with
261 additions
and
106 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { render } from '@testing-library/react'; | ||
import { createRef } from 'react'; | ||
import { Button } from './Button'; | ||
|
||
describe('Button', () => { | ||
it('access to DOM through ref prop', () => { | ||
const ref = createRef<HTMLButtonElement>(); | ||
render(<Button ref={ref}>Test</Button>); | ||
expect(ref.current).not.toBeNull(); | ||
expect(ref.current?.tagName).toBe('BUTTON'); | ||
}); | ||
}); |
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,17 @@ | ||
import { render } from '@testing-library/react'; | ||
import { createRef } from 'react'; | ||
import { Checkbox } from './Checkbox'; | ||
|
||
describe('Checkbox', () => { | ||
it('access to DOM through ref prop', () => { | ||
const ref = createRef<HTMLInputElement>(); | ||
render( | ||
<Checkbox name="test" value="test" ref={ref}> | ||
Test | ||
</Checkbox>, | ||
); | ||
expect(ref.current).not.toBeNull(); | ||
expect(ref.current?.tagName).toBe('INPUT'); | ||
expect(ref.current?.type).toBe('checkbox'); | ||
}); | ||
}); |
2 changes: 1 addition & 1 deletion
2
src/stories/Checkbox.stories.tsx → src/components/Checkbox/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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { render } from '@testing-library/react'; | ||
import { createRef } from 'react'; | ||
import { CheckboxGroup } from './CheckboxGroup'; | ||
|
||
describe('CheckboxGroup', () => { | ||
it('access to DOM through ref prop', () => { | ||
const ref = createRef<HTMLFieldSetElement>(); | ||
render( | ||
<CheckboxGroup label="test" ref={ref}> | ||
<p>Test</p> | ||
<p>Test</p> | ||
</CheckboxGroup>, | ||
); | ||
expect(ref.current).not.toBeNull(); | ||
expect(ref.current?.tagName).toBe('FIELDSET'); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -1,46 +1,34 @@ | ||
'use client'; | ||
|
||
import { forwardRef } from 'react'; | ||
import styles from './CheckboxGroup.module.css'; | ||
import { RequiredLabel } from '../../sharedComponents/RequiredLabel/RequiredLabel'; | ||
import { CustomDataAttributeProps } from '../../types/attributes'; // 追加したインポート | ||
import { CustomDataAttributeProps } from '../../types/attributes'; | ||
import { Checkbox } from '../Checkbox/Checkbox'; | ||
import { Flex } from '../Flex/Flex'; | ||
import type { FC, ReactElement } from 'react'; | ||
import type { ReactElement } from 'react'; | ||
|
||
export type Props = { | ||
children: ReactElement<typeof Checkbox>[]; | ||
/** | ||
* チェックボックスグループの見出し(legend要素) | ||
*/ | ||
label: string; | ||
/** | ||
* 必須マークを表示するか | ||
* 注意: trueとしてもinput要素のrequired属性は付与されません | ||
*/ | ||
showRequiredLabel?: boolean; | ||
/** | ||
* チェックボックスの配置方向 | ||
* @default column | ||
*/ | ||
direction?: 'column' | 'row'; | ||
} & CustomDataAttributeProps; | ||
|
||
export const CheckboxGroup: FC<Props> = ({ | ||
children, | ||
label, | ||
showRequiredLabel, | ||
direction = 'column', | ||
...otherProps | ||
}) => { | ||
return ( | ||
<fieldset className={styles.wrapper} {...otherProps}> | ||
<legend className={styles.legend}> | ||
{label} | ||
{showRequiredLabel && <RequiredLabel />} | ||
</legend> | ||
<Flex spacing="md" direction={direction}> | ||
{children} | ||
</Flex> | ||
</fieldset> | ||
); | ||
}; | ||
export const CheckboxGroup = forwardRef<HTMLFieldSetElement, Props>( | ||
({ children, label, showRequiredLabel, direction = 'column', ...otherProps }, ref) => { | ||
return ( | ||
<fieldset className={styles.wrapper} ref={ref} {...otherProps}> | ||
<legend className={styles.legend}> | ||
{label} | ||
{showRequiredLabel && <RequiredLabel />} | ||
</legend> | ||
<Flex spacing="md" direction={direction}> | ||
{children} | ||
</Flex> | ||
</fieldset> | ||
); | ||
}, | ||
); | ||
|
||
CheckboxGroup.displayName = 'CheckboxGroup'; |
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,12 @@ | ||
import { render } from '@testing-library/react'; | ||
import { createRef } from 'react'; | ||
import { ErrorMessage } from './ErrorMessage'; | ||
|
||
describe('ErrorMessage', () => { | ||
it('access to DOM through ref prop', () => { | ||
const ref = createRef<HTMLParagraphElement>(); | ||
render(<ErrorMessage ref={ref}>Test</ErrorMessage>); | ||
expect(ref.current).not.toBeNull(); | ||
expect(ref.current?.tagName).toBe('P'); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -1,15 +1,18 @@ | ||
'use client'; | ||
|
||
import { forwardRef } from 'react'; | ||
import styles from './ErrorMessage.module.css'; | ||
import { CustomDataAttributeProps } from '../../types/attributes'; // 追加したインポート | ||
import type { FC, ReactNode } from 'react'; | ||
import { CustomDataAttributeProps } from '../../types/attributes'; | ||
import type { ReactNode } from 'react'; | ||
|
||
type Props = { | ||
children: ReactNode; | ||
} & CustomDataAttributeProps; | ||
|
||
export const ErrorMessage: FC<Props> = ({ children, ...otherProps }) => ( | ||
<p aria-live="polite" className={styles.error} {...otherProps}> | ||
export const ErrorMessage = forwardRef<HTMLParagraphElement, Props>(({ children, ...otherProps }, ref) => ( | ||
<p aria-live="polite" className={styles.error} ref={ref} {...otherProps}> | ||
{children} | ||
</p> | ||
); | ||
)); | ||
|
||
ErrorMessage.displayName = 'ErrorMessage'; |
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,14 @@ | ||
import { render } from '@testing-library/react'; | ||
import { createRef } from 'react'; | ||
import { Input } from './Input'; | ||
|
||
describe('Input', () => { | ||
it('access to DOM through ref prop', () => { | ||
const ref = createRef<HTMLInputElement>(); | ||
|
||
render(<Input name="test" value="test" ref={ref} />); | ||
expect(ref.current).not.toBeNull(); | ||
expect(ref.current?.tagName).toBe('INPUT'); | ||
expect(ref.current?.type).toBe('text'); | ||
}); | ||
}); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { render } from '@testing-library/react'; | ||
import { createRef } from 'react'; | ||
import { RadioButton } from './RadioButton'; | ||
|
||
describe('RadioButton', () => { | ||
it('access to DOM through ref prop', () => { | ||
const ref = createRef<HTMLInputElement>(); | ||
render( | ||
<RadioButton | ||
name="test" | ||
value="test" | ||
ref={ref} | ||
onChange={() => { | ||
/**/ | ||
}} | ||
checked={false} | ||
> | ||
Test | ||
</RadioButton>, | ||
); | ||
expect(ref.current).not.toBeNull(); | ||
expect(ref.current?.tagName).toBe('INPUT'); | ||
expect(ref.current?.type).toBe('radio'); | ||
}); | ||
}); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { render } from '@testing-library/react'; | ||
import { createRef } from 'react'; | ||
import { RadioGroup } from './RadioGroup'; | ||
|
||
describe('RadioGroup', () => { | ||
it('access to DOM through ref prop', () => { | ||
const ref = createRef<HTMLFieldSetElement>(); | ||
render( | ||
<RadioGroup label="test" ref={ref}> | ||
<p>Test</p> | ||
<p>Test</p> | ||
</RadioGroup>, | ||
); | ||
expect(ref.current).not.toBeNull(); | ||
expect(ref.current?.tagName).toBe('FIELDSET'); | ||
}); | ||
}); |
Oops, something went wrong.