Skip to content

Commit

Permalink
progress
Browse files Browse the repository at this point in the history
Signed-off-by: Max You <[email protected]>
  • Loading branch information
jz5426 committed Oct 20, 2023
1 parent 859be30 commit 9121343
Show file tree
Hide file tree
Showing 6 changed files with 182 additions and 139 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from 'react';
import {
Dropdown,
Tooltip,
DefinitionTooltip,
TextInput
} from '@carbon/react';
import { css } from 'emotion';
Expand All @@ -10,38 +10,31 @@ import { AComponent, ComponentInfo } from './a-component';
import image from './../assets/component-icons/tooltip.svg';
import { angularClassNamesFromComponentObj, nameStringToVariableString, reactClassNamesFromComponentObj } from '../tools';
import { styleObjectToString } from '../../../ui-fragment/src/utils';
import { Checkbox } from '@carbon/react';

const preventCheckEvent = css`
.bx--tooltip__label {
pointer-events: none;
}`;

export const ATooltipSettingsUI = ({ selectedComponent, setComponent }: any) => {
const placements = [
{ id: 'bottom', text: 'Bottom' },
{ id: 'top', text: 'Top' },
{ id: 'left', text: 'Left' },
{ id: 'right', text: 'Right' }
];
export const ADefinitionTooltipSettingsUI = ({ selectedComponent, setComponent }: any) => {

const alignments = [
{ id: 'start', text: 'Start' },
{ id: 'center', text: 'Center' },
{ id: 'end', text: 'End' }
{ id: 'top', text: 'Top' },
{ id: 'top-left', text: 'Top left' },
{ id: 'top-right', text: 'Top right' },
{ id: 'bottom', text: 'Bottom' },
{ id: 'bottom-left', text: 'Bottom left' },
{ id: 'bottom-right', text: 'Bottom right' },
{ id: 'left', text: 'Left'},
{ id: 'left-bottom', text: 'Left bottom' },
{ id: 'left-top', text: 'Left top' },
{ id: 'right', text: 'Right' },
{ id: 'right-bottom', text: 'Right bottom' },
{ id: 'right-top', text: 'Right top' }
];

return <>
<Dropdown
id='direction'
label='Placement of the pop up'
titleText='Placement'
items={placements}
selectedItem={placements.find(item => item.id === selectedComponent.direction)}
itemToString={(item: any) => (item ? item.text : '')}
onChange={(event: any) => setComponent({
...selectedComponent,
direction: event.selectedItem.id
})} />
<Dropdown
id='alignment'
label='Alignment the text'
Expand All @@ -54,23 +47,41 @@ export const ATooltipSettingsUI = ({ selectedComponent, setComponent }: any) =>
alignment: event.selectedItem.id
})} />
<TextInput
id='description'
value={selectedComponent.description}
labelText='Description'
onChange={(event: any) => setComponent({
...selectedComponent,
description: event.currentTarget.value
})} />
<TextInput
value={selectedComponent.triggerText}
labelText='Trigger text'
id="definition"
value={selectedComponent.definition}
labelText='Tooltip Message'
onChange={(event: any) => setComponent({
...selectedComponent,
triggerText: event.currentTarget.value
definition: event.currentTarget.definition
})} />
<Checkbox
id='defaultOpen'
labelText='Default open'
checked={selectedComponent.isDefaultOpened}
onChange={(_: any, { checked }: any) => setComponent({
...selectedComponent,
isDefaultOpened: checked
})} />
<Checkbox
id='openOnHover'
labelText='Open on hover'
checked={selectedComponent.isOpenOnHover}
onChange={(_: any, { checked }: any) => setComponent({
...selectedComponent,
isOpenOnHover: checked
})} />
</>;
};

export const ATooltipCodeUI = ({ selectedComponent, setComponent }: any) => <TextInput
export const ADefinitionTooltipCodeUI = ({ selectedComponent, setComponent }: any) => <TextInput
value={selectedComponent.codeContext?.name}
id='input-name'
labelText='Input name'
Expand All @@ -84,7 +95,7 @@ export const ATooltipCodeUI = ({ selectedComponent, setComponent }: any) => <Tex
});
}} />;

export const ATooltip = ({
export const ADefinitionTooltip = ({
componentObj,
...rest
}: any) => {
Expand All @@ -95,67 +106,73 @@ export const ATooltip = ({
// make the icon hover only and whatever other customer css classes to be augmented
className={`${preventCheckEvent} ${componentObj.cssClasses?.map((cc: any) => cc.id).join(' ')} `}
{...rest}>
<Tooltip
<DefinitionTooltip
className={css`${styleObjectToString(componentObj.style)}`}
triggerText={componentObj.triggerText}
direction={componentObj.direction}
definition={componentObj.definition}
align={componentObj.alignment}>
{componentObj.description}
</Tooltip>
</DefinitionTooltip>
</AComponent>
);
};

export const componentInfo: ComponentInfo = {
component: ATooltip,
settingsUI: ATooltipSettingsUI,
codeUI: ATooltipCodeUI,
render: ({ componentObj, select, remove, selected }) => <ATooltip
component: ADefinitionTooltip,
settingsUI: ADefinitionTooltipSettingsUI,
codeUI: ADefinitionTooltipCodeUI,
render: ({ componentObj, select, remove, selected }) => <ADefinitionTooltip
componentObj={componentObj}
select={select}
remove={remove}
selected={selected} />,
keywords: ['tooltip'],
name: 'Tooltip',
type: 'tooltip',
keywords: ['definition tooltip', 'definition', 'tooltip'],
name: 'Definition tooltip',
type: 'definition-tooltip',
defaultComponentObj: {
type: 'tooltip'
type: 'definition-tooltip',
description: 'default text',
alignment: 'bottom-left'
},
image,
codeExport: {
angular: {
latest: {
inputs: ({ json }) => `@Input() ${nameStringToVariableString(json.codeContext?.name)}Description = "${json.description}";
@Input() ${nameStringToVariableString(json.codeContext?.name)}TriggerText = "${json.triggerText}";
@Input() ${nameStringToVariableString(json.codeContext?.name)}Direction = "${json.direction}";`,
outputs: () => '',
imports: ['TooltipModule'],
code: ({ json }) => `<div class="bx--tooltip__label">
{{${nameStringToVariableString(json.codeContext?.name)}TriggerText}}
<span
${angularClassNamesFromComponentObj(json)}
[ibmTooltip]="${nameStringToVariableString(json.codeContext?.name)}Description"
trigger="click"
${json.direction ? `[direction]='${nameStringToVariableString(json.codeContext?.name)}Direction` : ''}>
<div role="button">
<svg ibmIcon="information--filled" size="16"></svg>
</div>
</span>
</div>`
@Input() ${nameStringToVariableString(json.codeContext?.name)}IsOpen = "${json.isDefaultOpened}";
@Input() ${nameStringToVariableString(json.codeContext?.name)}IsAlign = "${json.alignment}";
@Input() ${nameStringToVariableString(json.codeContext?.name)}definition = "${json.definition}";`,
outputs: ({ json }) => {
const name = nameStringToVariableString(json.codeContext?.name);
return `@Output() ${name}isOpenChange = new EventEmitter<any>();
@Output() ${name}onClose = new EventEmitter<any>();
@Output() ${name}onOpen = new EventEmitter<string>();`;
},
imports: ['DefinitionTooltipModule'], // TODO: do we need this?
code: ({ json}) => {
const name = nameStringToVariableString(json.codeContext?.name);
return `<cds-tooltip-definition
"${json.isDefaultOpened ? `[isOpen]='${name}IsOpen` : ''}"
"${json.alignment ? `[isOpen]='${name}IsAlign` : ''}"
(onOpen)="${name}onOpen.emit(event)"
(onClose)="${name}onClose.emit(event)"
(isOpenChange)="${name}isOpenChange.emit(event)"
[description]="${name}definition">
${name}Description
</cds-tooltip-definition>`;
}
},
v10: {
inputs: ({ json }) => `@Input() ${nameStringToVariableString(json.codeContext?.name)}Description = "${json.description}";
@Input() ${nameStringToVariableString(json.codeContext?.name)}TriggerText = "${json.triggerText}";
@Input() ${nameStringToVariableString(json.codeContext?.name)}Direction = "${json.direction}";`,
@Input() ${nameStringToVariableString(json.codeContext?.name)}Direction = "${json.align}";`,
outputs: () => '',
imports: ['TooltipModule'],
code: ({ json }) => `<div class="bx--tooltip__label">
{{${nameStringToVariableString(json.codeContext?.name)}TriggerText}}
<span
${angularClassNamesFromComponentObj(json)}
[ibmTooltip]="${nameStringToVariableString(json.codeContext?.name)}Description"
trigger="click"
${json.direction ? `[direction]='${nameStringToVariableString(json.codeContext?.name)}Direction` : ''}>
${json.align ? `[align]='${nameStringToVariableString(json.codeContext?.name)}Direction` : ''}>
<div role="button">
<svg ibmIcon="information--filled" size="16"></svg>
</div>
Expand All @@ -165,26 +182,26 @@ export const componentInfo: ComponentInfo = {
},
react: {
latest: {
imports: ['Tooltip'],
code: ({ json }) => `<Tooltip
imports: ['DefinitionTooltip'],
code: ({ json }) => `<DefinitionTooltip
${reactClassNamesFromComponentObj(json)}
description="${json.description}"
className="tooltip-trigger"
triggerText="${json.triggerText}"
${json.alignment ? `align="${json.alignment}"` : ''}
${json.direction ? `direction="${json.direction}"` : ''}>
align="${json.alignment}"
${json.isDefaultOpened ? `defaultOpen=${json.isDefaultOpened}` : ''}
definition="${json.definition}
${json.isOpenOnHover ? `openOnHover` : ''}
>
${json.description}
</Tooltip>`
</DefinitionTooltip>`
},
v10: {
imports: ['Tooltip'],
imports: ['DefinitionTooltip'],
code: ({ json }) => `<Tooltip
${reactClassNamesFromComponentObj(json)}
description="${json.description}"
className="tooltip-trigger"
triggerText="${json.triggerText}"
${json.alignment ? `align="${json.alignment}"` : ''}
${json.direction ? `direction="${json.direction}"` : ''}>
${json.align ? `align="${json.align}"` : ''}>
${json.description}
</Tooltip>`
}
Expand Down
6 changes: 3 additions & 3 deletions src/sdk/src/fragment-components/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import * as link from './a-link';
import * as loading from './a-loading';
import * as inlineLoading from './a-inline-loading';
import * as overflowMenu from './a-overflow-menu';
import * as tooltip from './a-tooltip';
import * as definitionTooltip from './a-definition-tooltip';
// Tiles
import * as tile from './tiles/a-tile';
import * as toggle from './a-toggle';
Expand Down Expand Up @@ -64,7 +64,7 @@ export { ATextInput, ATextInputSettingsUI, ATextInputCodeUI } from './a-text-inp
export { AOverflowMenu, AOverflowMenuCodeUI, AOverflowMenuSettingsUI } from './a-overflow-menu';
export { ARadio, ARadioSettingsUI, ARadioCodeUI } from './a-radio';
export { ARadioGroup, ARadioGroupSettingsUI, ARadioGroupCodeUI } from './a-radio-group';
export { ATooltip, ATooltipSettingsUI, ATooltipCodeUI } from './a-tooltip';
export { ADefinitionTooltip, ADefinitionTooltipSettingsUI, ADefinitionTooltipCodeUI } from './a-definition-tooltip';

// Tiles
export { ATile, ATileCodeUI, ATileSettingsUI } from './tiles/a-tile';
Expand Down Expand Up @@ -104,7 +104,7 @@ export const allComponents = {
textarea,
textinput,
overflowMenu,
tooltip,
definitionTooltip,
// Tiles
tile,
toggle,
Expand Down
2 changes: 1 addition & 1 deletion src/ui-fragment/src/components/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import * as searchinput from './ui-search-input';
import * as text from './ui-text';
import * as textarea from './ui-text-area';
import * as textinput from './ui-text-input';
import * as tooltip from './ui-tooltip';
import * as tooltip from './ui-definition-tooltip';
import * as link from './ui-link';
import * as loading from './ui-loading';
import * as inlineLoading from './ui-inline-loading';
Expand Down
90 changes: 90 additions & 0 deletions src/ui-fragment/src/components/ui-definition-tooltip.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import React from 'react';
import { DefinitionTooltip } from '@carbon/react';
import { CssClasses, SendSignal } from '../types';
import { stringToCssClassName } from '../utils';
import { commonSlots, slotsDisabled } from '../common-slots';

export interface DefinitionTooltipState {
id: string;
type: string;
alignment: string;
codeContext: {
name: string;
};
description: string;
defaultOpen?: boolean;
definition?: string;
isOpenOnHover?: boolean;
cssClasses?: CssClasses[];
style?: any;
}

export const type = 'definition-tooltip';
export const slots = {
...commonSlots,
...slotsDisabled,
enableOpenOnHover: (state: any) => ({
...state,
isOpenOnHover: true
}),
disableOpenOnHover: (state: any) => ({
...state,
isOpenOnHover: false
}),
toggleOpenOnHover: (state: any) => ({
...state,
isOpenOnHover: !state.isOpenOnHover
}),
enableOpenOnDefault: (state: any) => ({
...state,
defaultOpen: true
}),
disableOpenOnDefault: (state: any) => ({
...state,
defaultOpen: false
}),
toggleOpenOnDefault: (state: any) => ({
...state,
defaultOpen: !state.defaultOpen
}),
isOpenOnHover: 'boolean',
defaultOpen: 'boolean',
alignment: 'string',
definition: 'string',
description: 'string'
};

export const signals = ['click'];

export const UIDefinitionTooltip = ({ state, sendSignal }: {
state: DefinitionTooltipState;
setState: (state: any) => void;
setGlobalState: (state: any) => void;
sendSignal: SendSignal;
}) => {
if (state.type !== 'definition-tooltip') {
// eslint-disable-next-line react/jsx-no-useless-fragment
return <></>;
}

let cssClasses = state.cssClasses?.map((cc: any) => cc.id).join(' ') || '';

if (state.style) {
if (cssClasses.length > 0) {
cssClasses += ' ';
}
cssClasses += stringToCssClassName(state.codeContext.name);
}

return <DefinitionTooltip
id={state.id}
onClick={() => sendSignal(state.id, 'click')}
align={state.alignment ? state.alignment : 'bottom-left'}
definition={state.definition}
name={state.codeContext?.name}
openOnHover={state.isOpenOnHover ? state.isOpenOnHover : false}
defaultOpen={state.defaultOpen ? state.defaultOpen : false}
className={cssClasses}>
{state.description}
</DefinitionTooltip>;
};
Loading

0 comments on commit 9121343

Please sign in to comment.