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

[Playground] EuiBadge, EuiNotificationBadge, EuiBetaBadge #3722

Merged
merged 20 commits into from
Jul 20, 2020
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
8 changes: 6 additions & 2 deletions src-docs/src/routes.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { createElement } from 'react';
import React, { createElement, Fragment } from 'react';

import { GuidePage, GuideSection } from './components';

Expand Down Expand Up @@ -263,7 +263,11 @@ const createExample = (example, customTitle) => {

let playgroundComponent;
if (playground) {
playgroundComponent = playgroundCreator(playground());
if (Array.isArray(playground)) {
playgroundComponent = playground.map((elm, idx) => {
return <Fragment key={idx}>{playgroundCreator(elm())}</Fragment>;
});
} else playgroundComponent = playgroundCreator(playground());
}

const component = () => (
Expand Down
129 changes: 101 additions & 28 deletions src-docs/src/services/playground/knobs.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react';
import { assertUnreachable, PropTypes, useValueDebounce } from 'react-view';
import React, { useState, useEffect } from 'react';
import { assertUnreachable, PropTypes } from 'react-view';
import {
EuiSpacer,
EuiSwitch,
Expand All @@ -15,6 +15,8 @@ import {
EuiTableHeaderCell,
EuiTableRow,
EuiTableRowCell,
EuiTextColor,
EuiFormRow,
} from '../../../../src/components/';

import {
Expand Down Expand Up @@ -44,17 +46,26 @@ const Label = ({ children, tooltip }) => {

const Knob = ({
name,
error,
error: errorMsg,
type,
defaultValue,
val: globalVal,
set: globalSet,
val,
set,
options = {},
description,
placeholder,
custom,
state,
}) => {
const [val, set] = useValueDebounce(globalVal, globalSet);
const [error, setError] = useState(errorMsg);

useEffect(() => {
if (custom && custom.checkDep) {
setError(custom.checkDep(val, state));
}
}, [state, val, custom]);

let knobProps = {};
switch (type) {
case PropTypes.Ref:
return (
Expand Down Expand Up @@ -92,27 +103,45 @@ const Knob = ({

case PropTypes.String:
case PropTypes.Date:
if (custom && custom.validator) {
knobProps = {};
knobProps.onChange = e => {
const value = e.target.value;
if (custom.validator(value)) set(value);
else set(undefined);
};
} else if (custom && custom.sanitize) {
knobProps = {};
knobProps.value = val;
knobProps.onChange = e => {
const value = e.target.value;
set(custom.sanitize(value));
};
} else {
knobProps = {};
knobProps.value = val;
knobProps.onChange = e => {
const value = e.target.value;
set(value);
};
}

return (
<>
<EuiFormRow
isInvalid={error && error.length > 0}
error={error}
fullWidth>
<EuiFieldText
placeholder={placeholder}
onChange={e => {
const value = e.target.value;
if (custom && custom.validator) {
if (custom.validator(value)) set(value);
else set(undefined);
} else {
set(value);
}
}}
aria-label={description}
isInvalid={error && error.length > 0}
compressed
fullWidth
{...knobProps}
/>

{error && <div>error {error}</div>}
</>
</EuiFormRow>
);

case PropTypes.Boolean:
return (
<>
Expand All @@ -121,13 +150,14 @@ const Knob = ({
label=""
checked={val}
onChange={e => {
globalSet(e.target.checked);
set(e.target.checked);
}}
compressed
/>
{error && <div>error {error}</div>}
</>
);

case PropTypes.Enum:
const optionsKeys = Object.keys(options);
const numberOfOptions = optionsKeys.length;
Expand All @@ -151,7 +181,7 @@ const Knob = ({
onChange={id => {
let val = id;
if (val.includes('__')) val = val.split('__')[0];
globalSet(val);
set(val);
}}
name={`Select ${name}`}
/>
Expand All @@ -165,28 +195,52 @@ const Knob = ({
}));

return (
<>
<EuiFormRow
isInvalid={error && error.length > 0}
error={error}
fullWidth>
<EuiSelect
fullWidth
id={name}
options={flattenedOptions}
value={valueKey}
onChange={e => {
globalSet(e.target.value);
set(e.target.value);
}}
isInvalid={error && error.length > 0}
aria-label={`Select ${name}`}
compressed
/>
{error && <div>error {error}</div>}
</>
</EuiFormRow>
);
}

case PropTypes.Custom:
anishagg17 marked this conversation as resolved.
Show resolved Hide resolved
if (custom && custom.use) {
switch (custom.use) {
case 'switch':
return (
<>
<EuiSwitch
id={name}
label={custom.label || ''}
checked={typeof val !== 'undefined' && val}
onChange={e => {
const value = e.target.checked;

set(value ? value : undefined);
}}
compressed
/>
</>
);
}
}

case PropTypes.ReactNode:
case PropTypes.Function:
case PropTypes.Array:
case PropTypes.Object:
case PropTypes.Custom:
return null;
default:
return assertUnreachable();
Expand All @@ -210,13 +264,30 @@ const KnobColumn = ({ state, knobNames, error, set }) => {
<span className="eui-textBreakNormal">{markup(humanizedType)}</span>
);

let humanizedName = (
<strong className="eui-textBreakNormal">{name}</strong>
);

if (
state[name].custom &&
state[name].custom.origin &&
state[name].custom.origin.required
) {
humanizedName = (
<span>
{humanizedName}{' '}
<EuiTextColor color="danger">(required)</EuiTextColor>
</span>
);
}

return (
<EuiTableRow key={name}>
<EuiTableRowCell
key={`prop__${name}-${idx}`}
header="Prop"
className="playgroundKnobs__rowCell">
<strong className="eui-textBreakNormal">{name}</strong>
{humanizedName}
{state[name].description && (
<>
<br />
Expand Down Expand Up @@ -257,7 +328,9 @@ const KnobColumn = ({ state, knobNames, error, set }) => {
set={value => set(value, name)}
enumName={state[name].enumName}
defaultValue={state[name].defaultValue}
custom={state[name].custom}
custom={state[name] && state[name].custom}
state={state}
orgSet={set}
/>
</EuiTableRowCell>
</EuiTableRow>
Expand Down
10 changes: 2 additions & 8 deletions src-docs/src/services/playground/playground.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React, { useState, useEffect } from 'react';
import classNames from 'classnames';
import format from 'html-format';

import { useView, Compiler, Error, Placeholder } from 'react-view';
import { useView, Compiler, Placeholder } from 'react-view';
import { EuiSpacer, EuiTitle, EuiCodeBlock } from '../../../../src/components';
import Knobs from './knobs';

Expand Down Expand Up @@ -61,15 +61,9 @@ export default ({ config, setGhostBackground }) => {
placeholder={Placeholder}
/>
</div>
<Error msg={params.errorProps.msg} isPopup />
<EuiSpacer />

<EuiCodeBlock
language="html"
fontSize="m"
paddingSize="m"
overflowHeight={300}
isCopyable>
<EuiCodeBlock language="html" fontSize="m" paddingSize="m" isCopyable>
{getSnippet(params.editorProps.code)}
</EuiCodeBlock>
<EuiSpacer />
Expand Down
25 changes: 7 additions & 18 deletions src-docs/src/services/playground/props.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable guard-for-in */
import { PropTypes } from 'react-view';

const getProp = (prop, propName) => {
const getProp = prop => {
const newProp = {};
if (prop.description) newProp.description = prop.description;
newProp.custom = { origin: prop };
Expand All @@ -23,13 +23,8 @@ const getProp = (prop, propName) => {
newProp.required = prop.required;
if (prop.defaultValue) {
newProp.defaultValue = prop.defaultValue.value;
newProp.value = prop.defaultValue.value.substring(
1,
prop.defaultValue.value.length - 1
);
} else {
newProp.value = undefined;
}
newProp.value = undefined;
newProp.options = {};
for (const i in prop.type.value) {
const val = prop.type.value[i].value;
Expand All @@ -40,30 +35,24 @@ const getProp = (prop, propName) => {

case 'number':
newProp.type = PropTypes.Number;
newProp.placeholder = propName;
if (prop.defaultValue) newProp.value = prop.defaultValue.value;
else newProp.value = 0;
if (prop.defaultValue) newProp.defaultValue = prop.defaultValue.value;
break;

case 'string':
newProp.type = PropTypes.String;
newProp.placeholder = propName;
if (prop.defaultValue) newProp.value = prop.defaultValue.value;
else newProp.value = '';
if (prop.defaultValue) newProp.defaultValue = prop.defaultValue.value;
break;

case 'func':
newProp.type = PropTypes.Function;
newProp.placeholder = propName;

break;

case 'node':
case 'element':
newProp.type = PropTypes.ReactNode;
newProp.placeholder = propName;
if (prop.defaultValue) newProp.value = prop.defaultValue.value;
else newProp.value = undefined;
if (prop.defaultValue) newProp.defaultValue = prop.defaultValue.value;
newProp.value = undefined;
break;

default:
Expand All @@ -78,7 +67,7 @@ const propUtilityForPlayground = props => {
const modifiedProps = {};

for (const key in props) {
if (props[key].type) modifiedProps[key] = getProp(props[key], key);
if (props[key].type) modifiedProps[key] = getProp(props[key]);
}
return modifiedProps;
};
Expand Down
6 changes: 6 additions & 0 deletions src-docs/src/views/badge/badge_example.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ import {
EuiBadgeGroup,
EuiCallOut,
} from '../../../../src/components';
import {
badgeConfig,
betaBadgeConfig,
notificationBadgeConfig,
} from './playground';

import Badge from './badge';

Expand Down Expand Up @@ -317,4 +322,5 @@ export const BadgeExample = {
demo: <NotificationBadge />,
},
],
playground: [badgeConfig, betaBadgeConfig, notificationBadgeConfig],
};
Loading