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

Support prefix & suffix for 3 widgets: string, number & boolean #6836

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
26 changes: 23 additions & 3 deletions dev-test/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -140,11 +140,31 @@ collections: # A list of collections the CMS should be able to edit
display_fields: ['title', 'datetime']
search_fields: ['title', 'body']
value_field: 'title'
- { label: 'Title', name: 'title', widget: 'string' }
- { label: 'Boolean', name: 'boolean', widget: 'boolean', default: true }
- {
label: 'Title',
name: 'title',
widget: 'string',
prefix: 'This string:',
suffix: 'is a title'
}
- {
label: 'Boolean',
name: 'boolean',
widget: 'boolean',
prefix: 'OFF',
suffix: 'ON',
hint: 'Toggle this to switch on/off',
default: true
}
- { label: 'Map', name: 'map', widget: 'map' }
- { label: 'Text', name: 'text', widget: 'text', hint: 'Plain text, not markdown' }
- { label: 'Number', name: 'number', widget: 'number', hint: 'To infinity and beyond!' }
- {
label: 'Number',
name: 'number',
widget: 'number',
suffix: 'px',
hint: 'To infinity and beyond!'
}
- { label: 'Markdown', name: 'markdown', widget: 'markdown' }
- { label: 'Datetime', name: 'datetime', widget: 'datetime' }
- { label: 'Image', name: 'image', widget: 'image' }
Expand Down
59 changes: 36 additions & 23 deletions packages/decap-cms-widget-boolean/src/BooleanControl.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ import ImmutablePropTypes from 'react-immutable-proptypes';
import { css } from '@emotion/react';
import { Toggle, ToggleBackground, colors } from 'decap-cms-ui-default';

const innerWrapper = css`
display: flex;
align-items: center;
`;

function BooleanBackground({ isActive, ...props }) {
return (
<ToggleBackground
Expand All @@ -16,34 +21,42 @@ function BooleanBackground({ isActive, ...props }) {
}

export default class BooleanControl extends React.Component {
static propTypes = {
field: ImmutablePropTypes.map.isRequired,
onChange: PropTypes.func.isRequired,
classNameWrapper: PropTypes.string.isRequired,
setActiveStyle: PropTypes.func.isRequired,
setInactiveStyle: PropTypes.func.isRequired,
forID: PropTypes.string,
value: PropTypes.bool,
};

static defaultProps = {
value: false,
};

render() {
const { value, forID, onChange, classNameWrapper, setActiveStyle, setInactiveStyle } =
const { value, forID, onChange, classNameWrapper, setActiveStyle, setInactiveStyle, field } =
this.props;

const prefix = field.get('prefix', false);
const suffix = field.get('suffix', false);

return (
<div className={classNameWrapper}>
<Toggle
id={forID}
active={value}
onChange={onChange}
onFocus={setActiveStyle}
onBlur={setInactiveStyle}
Background={BooleanBackground}
/>
<div css={innerWrapper}>
{prefix && <span>{prefix}&nbsp;</span>}
<Toggle
id={forID}
active={value}
onChange={onChange}
onFocus={setActiveStyle}
onBlur={setInactiveStyle}
Background={BooleanBackground}
/>
{suffix && <span>&nbsp;{suffix}</span>}
</div>
</div>
);
}
}

BooleanControl.propTypes = {
field: ImmutablePropTypes.map.isRequired,
onChange: PropTypes.func.isRequired,
classNameWrapper: PropTypes.string.isRequired,
setActiveStyle: PropTypes.func.isRequired,
setInactiveStyle: PropTypes.func.isRequired,
forID: PropTypes.string,
value: PropTypes.bool,
};

BooleanControl.defaultProps = {
value: false,
};
43 changes: 31 additions & 12 deletions packages/decap-cms-widget-number/src/NumberControl.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
import React from 'react';
import PropTypes from 'prop-types';
import ImmutablePropTypes from 'react-immutable-proptypes';
import { css } from '@emotion/react';

const ValidationErrorTypes = {
PRESENCE: 'PRESENCE',
PATTERN: 'PATTERN',
RANGE: 'RANGE',
CUSTOM: 'CUSTOM',
};

const innerWrapper = css`
display: flex;
align-items: baseline;
`;

export function validateMinMax(value, min, max, field, t) {
let error;

Expand Down Expand Up @@ -100,19 +107,31 @@ export default class NumberControl extends React.Component {
const min = field.get('min', '');
const max = field.get('max', '');
const step = field.get('step', field.get('value_type') === 'int' ? 1 : '');

const prefix = field.get('prefix', false);
const suffix = field.get('suffix', false);

return (
<input
type="number"
id={forID}
className={classNameWrapper}
onFocus={setActiveStyle}
onBlur={setInactiveStyle}
value={value || (value === 0 ? value : '')}
step={step}
min={min}
max={max}
onChange={this.handleChange}
/>
<div className={classNameWrapper}>
<div css={innerWrapper}>
{prefix && <span>{prefix}&nbsp;</span>}
<input
type="number"
id={forID}
onFocus={setActiveStyle}
onBlur={setInactiveStyle}
value={value || (value === 0 ? value : '')}
step={step}
min={min}
max={max}
onChange={this.handleChange}
css={css`
flex-grow: 1;
`}
/>
{suffix && <span>&nbsp;{suffix}</span>}
</div>
</div>
);
}
}
45 changes: 32 additions & 13 deletions packages/decap-cms-widget-string/src/StringControl.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
import React from 'react';
import PropTypes from 'prop-types';
import ImmutablePropTypes from 'react-immutable-proptypes';
import { css } from '@emotion/react';

const innerWrapper = css`
display: flex;
align-items: baseline;
`;

export default class StringControl extends React.Component {
static propTypes = {
field: ImmutablePropTypes.map.isRequired,
onChange: PropTypes.func.isRequired,
forID: PropTypes.string,
value: PropTypes.node,
Expand Down Expand Up @@ -41,21 +49,32 @@ export default class StringControl extends React.Component {
};

render() {
const { forID, value, classNameWrapper, setActiveStyle, setInactiveStyle } = this.props;
const { field, forID, value, classNameWrapper, setActiveStyle, setInactiveStyle } = this.props;

const prefix = field.get('prefix', false);
const suffix = field.get('suffix', false);

return (
<input
ref={el => {
this._el = el;
}}
type="text"
id={forID}
className={classNameWrapper}
value={value || ''}
onChange={this.handleChange}
onFocus={setActiveStyle}
onBlur={setInactiveStyle}
/>
<div className={classNameWrapper}>
<div css={innerWrapper}>
{prefix && <span>{prefix}&nbsp;</span>}
<input
ref={el => {
this._el = el;
}}
type="text"
id={forID}
value={value || ''}
onChange={this.handleChange}
onFocus={setActiveStyle}
onBlur={setInactiveStyle}
css={css`
flex-grow: 1;
`}
/>
{suffix && <span>&nbsp;{suffix}</span>}
</div>
</div>
);
}
}
4 changes: 1 addition & 3 deletions packages/decap-server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,5 @@
"engines": {
"node": ">=v10.22.1"
},
"bin": {
"decap-server": "./dist/index.js"
}
"bin": "./dist/index.js"
}
Loading