Skip to content
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

Fix #5609: ToggleButton focusedState #5612

Merged
merged 1 commit into from
Dec 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions components/doc/togglebutton/theming/tailwinddoc.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ export function TailwindDoc(props) {
basic: `
const Tailwind = {
togglebutton: {
root: ({ props, context }) => ({
root: ({ props, state }) => ({
className: classNames(
'inline-flex cursor-pointer select-none items-center align-bottom text-center overflow-hidden relative',
'px-4 py-3 rounded-md text-base w-36',
'border transition duration-200 ease-in-out',
{
'outline-none outline-offset-0 shadow-[0_0_0_0.2rem_rgba(191,219,254,1)] dark:shadow-[0_0_0_0.2rem_rgba(147,197,253,0.5)]': context.focused
'outline-none outline-offset-0 shadow-[0_0_0_0.2rem_rgba(191,219,254,1)] dark:shadow-[0_0_0_0.2rem_rgba(147,197,253,0.5)]': state.focused
},
{
'bg-white dark:bg-gray-900 border-gray-300 dark:border-blue-900/40 text-gray-700 dark:text-white/80 hover:bg-gray-100 dark:hover:bg-gray-800/80 hover:border-gray-300 dark:hover:bg-gray-800/70 hover:text-gray-700 dark:hover:text-white/80':
Expand Down
8 changes: 4 additions & 4 deletions components/lib/passthrough/tailwind/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -982,14 +982,14 @@ const Tailwind = {
transition: TRANSITIONS.overlay
},
togglebutton: {
root: ({ props, context }) => ({
root: ({ props, state }) => ({
className: classNames(
'inline-flex cursor-pointer select-none items-center align-bottom text-center overflow-hidden relative',
'px-4 py-3 rounded-md text-base w-36',
'border transition duration-200 ease-in-out',
// {
// 'outline-none outline-offset-0 shadow-[0_0_0_0.2rem_rgba(191,219,254,1)] dark:shadow-[0_0_0_0.2rem_rgba(147,197,253,0.5)]': context.focused
// },
{
'outline-none outline-offset-0 shadow-[0_0_0_0.2rem_rgba(191,219,254,1)] dark:shadow-[0_0_0_0.2rem_rgba(147,197,253,0.5)]': state.focused
},
{
'bg-white dark:bg-gray-900 border-gray-300 dark:border-blue-900/40 text-gray-700 dark:text-white/80 hover:bg-gray-100 dark:hover:bg-gray-800/80 hover:border-gray-300 dark:hover:bg-gray-800/70 hover:text-gray-700 dark:hover:text-white/80':
!props.checked,
Expand Down
22 changes: 18 additions & 4 deletions components/lib/togglebutton/ToggleButton.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
import * as React from 'react';
import { PrimeReactContext } from '../api/Api';
import { useHandleStyle } from '../componentbase/ComponentBase';
import { useMountEffect } from '../hooks/Hooks';
import { Ripple } from '../ripple/Ripple';
import { Tooltip } from '../tooltip/Tooltip';
import { DomHandler, IconUtils, ObjectUtils, mergeProps } from '../utils/Utils';
import { ToggleButtonBase } from './ToggleButtonBase';
import { useHandleStyle } from '../componentbase/ComponentBase';

export const ToggleButton = React.memo(
React.forwardRef((inProps, ref) => {
const context = React.useContext(PrimeReactContext);
const props = ToggleButtonBase.getProps(inProps, context);
const elementRef = React.useRef(null);
const [focusedState, setFocusedState] = React.useState(false);
const { ptm, cx, isUnstyled } = ToggleButtonBase.setMetaData({
props
props,
state: {
focused: focusedState
}
});

useHandleStyle(ToggleButtonBase.css.styles, isUnstyled, { name: 'togglebutton' });
Expand Down Expand Up @@ -50,6 +54,16 @@ export const ToggleButton = React.memo(
}
};

const onFocus = (event) => {
setFocusedState(true);
props.onFocus && props.onFocus(event);
};

const onBlur = (event) => {
setFocusedState(false);
props.onBlur && props.onBlur(event);
};

const createIcon = () => {
if (hasIcon) {
const iconProps = mergeProps(
Expand Down Expand Up @@ -95,8 +109,8 @@ export const ToggleButton = React.memo(
className: cx('root', { hasIcon, hasLabel }),
style: props.style,
onClick: toggle,
onFocus: props.onFocus,
onBlur: props.onBlur,
onFocus: onFocus,
onBlur: onBlur,
onKeyDown: onKeyDown,
tabIndex: tabIndex,
role: 'button',
Expand Down
Loading