Skip to content

Commit

Permalink
Fix primefaces#5609: ToggleButton focusedState
Browse files Browse the repository at this point in the history
  • Loading branch information
melloware committed Dec 18, 2023
1 parent 05a83b4 commit 0deb349
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 10 deletions.
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

0 comments on commit 0deb349

Please sign in to comment.