Skip to content

Commit

Permalink
fix(datepicker): add type=button to all buttons (#807)
Browse files Browse the repository at this point in the history
* fix(datepicker): add type=button to all buttons

* fix(datepicker): show calendar on custom icon click

* chore: update pull request template

* chore: configure ESLint to prevent non-button types of button

* chore: apply ESLint rule to buttons in stories
  • Loading branch information
arthurdenner committed Nov 11, 2022
1 parent 3537d19 commit 995c185
Show file tree
Hide file tree
Showing 9 changed files with 31 additions and 12 deletions.
1 change: 1 addition & 0 deletions .eslintrc.js
Expand Up @@ -2,5 +2,6 @@ module.exports = {
extends: ['react-app', 'prettier', 'plugin:prettier/recommended'],
rules: {
'@typescript-eslint/consistent-type-assertions': 0,
'react/button-has-type': [2, { submit: false, reset: false }],
},
};
8 changes: 2 additions & 6 deletions .github/PULL_REQUEST_TEMPLATE.md
@@ -1,8 +1,3 @@
<!--
Make sure you've read the CONTRIBUTING.md file.
Fill out the information to help the review and merge of your pull request!
-->

**What kind of change does this PR introduce?**
<!-- You can also link to an open issue here -->

Expand All @@ -19,4 +14,5 @@ Fill out the information to help the review and merge of your pull request!
- [ ] Tests
- [ ] Ready to be merged <!-- In your opinion, is this ready to be merged as soon as it's reviewed? -->

<!-- Feel free to add additional comments -->
<!-- Make sure you've read the CONTRIBUTING.md file too -->
<!-- Feel free to add additional comments -->
2 changes: 1 addition & 1 deletion src/components/button.tsx
@@ -1,7 +1,7 @@
import { Button, ButtonProps } from 'semantic-ui-react';

const CustomButton = ({ icon, ...otherProps }: ButtonProps) => (
<Button basic compact icon={icon} {...otherProps} />
<Button basic compact icon={icon} type="button" {...otherProps} />
);

export default CustomButton;
8 changes: 6 additions & 2 deletions src/components/calendar/calendar.tsx
Expand Up @@ -102,6 +102,7 @@ const Calendar: React.FC<CalendarProps> = ({
icon="angle double left"
inverted={inverted}
title={previousYear}
type="button"
{...getBackProps({
calendars,
'aria-label': previousYear,
Expand All @@ -114,6 +115,7 @@ const Calendar: React.FC<CalendarProps> = ({
inverted={inverted}
style={{ marginRight: 0 }}
title={previousMonth}
type="button"
{...getBackProps({
calendars,
'aria-label': previousMonth,
Expand All @@ -135,6 +137,7 @@ const Calendar: React.FC<CalendarProps> = ({
icon="angle right"
inverted={inverted}
title={nextMonth}
type="button"
{...getForwardProps({
calendars,
'aria-label': nextMonth,
Expand All @@ -146,6 +149,7 @@ const Calendar: React.FC<CalendarProps> = ({
inverted={inverted}
style={{ marginRight: 0 }}
title={nextYear}
type="button"
{...getForwardProps({
calendars,
'aria-label': nextYear,
Expand Down Expand Up @@ -201,7 +205,7 @@ const Calendar: React.FC<CalendarProps> = ({
</div>
))}
</div>
{showToday && (
{showToday ? (
<TodayButton
inverted={inverted}
{...getToday(minDate, maxDate)}
Expand All @@ -212,7 +216,7 @@ const Calendar: React.FC<CalendarProps> = ({
>
{todayButton}
</TodayButton>
)}
) : null}
</Segment>
</Ref>
);
Expand Down
7 changes: 6 additions & 1 deletion src/components/cell/cell.tsx
Expand Up @@ -48,7 +48,12 @@ const CalendarCell = ({
}

return (
<button className={className} disabled={!selectable} {...otherProps}>
<button
className={className}
disabled={!selectable}
type="button"
{...otherProps}
>
{children}
</button>
);
Expand Down
3 changes: 3 additions & 0 deletions src/components/icon.tsx
Expand Up @@ -7,13 +7,15 @@ type CustomIconProps = {
icon: SemanticDatepickerProps['icon'];
isClearIconVisible: boolean;
onClear: () => void;
onClick: () => void;
};

const CustomIcon = ({
clearIcon,
icon,
isClearIconVisible,
onClear,
onClick,
}: CustomIconProps) => {
if (isClearIconVisible && clearIcon && React.isValidElement(clearIcon)) {
return React.cloneElement<any>(clearIcon, {
Expand All @@ -37,6 +39,7 @@ const CustomIcon = ({
if (icon && React.isValidElement(icon)) {
return React.cloneElement<any>(icon, {
'data-testid': 'datepicker-icon',
onClick,
});
}

Expand Down
1 change: 1 addition & 0 deletions src/components/input.tsx
Expand Up @@ -46,6 +46,7 @@ const CustomInput = React.forwardRef<Input, InputProps>((props, ref) => {
icon={icon}
isClearIconVisible={isClearIconVisible}
onClear={onClear}
onClick={onFocus}
/>
}
input={inputData}
Expand Down
1 change: 1 addition & 0 deletions src/components/today-button.tsx
Expand Up @@ -32,6 +32,7 @@ const TodayButton: React.FC<TodayButtonProps> = ({
data-testid="datepicker-today-button"
fluid
style={style}
type="button"
{...otherProps}
>
{children}
Expand Down
12 changes: 10 additions & 2 deletions stories/index.stories.tsx
Expand Up @@ -94,8 +94,16 @@ export const withCustomIcons = () => {
const clearIcon = select('Clear icon (with value)', iconMap, iconMap.close);
const useCustomIcon = boolean('Custom icon', false);
const useCustomClearIcon = boolean('Custom clear icon', false);
const CustomIcon = (props: any) => <button {...props}>Select</button>;
const CustomClearIcon = (props: any) => <button {...props}>Reset</button>;
const CustomIcon = (props: any) => (
<button type="button" {...props}>
Select
</button>
);
const CustomClearIcon = (props: any) => (
<button type="button" {...props}>
Reset
</button>
);
const x = useCustomIcon ? ((<CustomIcon />) as unknown) : icon;
const y = useCustomClearIcon ? ((<CustomClearIcon />) as unknown) : clearIcon;

Expand Down

0 comments on commit 995c185

Please sign in to comment.