mirror of
https://github.com/strapi/strapi.git
synced 2025-09-01 04:42:58 +00:00
Init custom checkbox that display children for min max settings
This commit is contained in:
parent
98642ebbf9
commit
4c840e07ff
@ -8,10 +8,14 @@ import styled from 'styled-components';
|
|||||||
|
|
||||||
const StyledCustomCheckbox = styled.div`
|
const StyledCustomCheckbox = styled.div`
|
||||||
width: 100%;
|
width: 100%;
|
||||||
padding: 0 15px;
|
// padding: 0 15px;
|
||||||
margin-top: -6px;
|
padding: 0;
|
||||||
margin-bottom: 16px;
|
// margin-top: -6px;
|
||||||
label {
|
// margin-bottom: 16px;
|
||||||
|
> label {
|
||||||
|
font-weight: 500 !important;
|
||||||
|
font-size: 12px;
|
||||||
|
line-height: 10px;
|
||||||
cursor: pointer;
|
cursor: pointer;
|
||||||
input[type='checkbox'] {
|
input[type='checkbox'] {
|
||||||
margin-left: 0;
|
margin-left: 0;
|
||||||
@ -28,10 +32,10 @@ const StyledCustomCheckbox = styled.div`
|
|||||||
line-height: 10px;
|
line-height: 10px;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
input[type='number'] {
|
// input[type='number'] {
|
||||||
margin-top: -10px;
|
// margin-top: -10px;
|
||||||
margin-bottom: -4px;
|
// margin-bottom: -4px;
|
||||||
}
|
// }
|
||||||
`;
|
`;
|
||||||
|
|
||||||
export default StyledCustomCheckbox;
|
export default StyledCustomCheckbox;
|
||||||
|
@ -4,92 +4,55 @@
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import React from 'react';
|
import React, { useState } from 'react';
|
||||||
import PropTypes from 'prop-types';
|
import PropTypes from 'prop-types';
|
||||||
import { FormattedMessage } from 'react-intl';
|
import { isEmpty, isNumber } from 'lodash';
|
||||||
|
import { Inputs } from '@buffetjs/custom';
|
||||||
import { InputNumberWithErrors as InputNumber } from 'strapi-helper-plugin';
|
|
||||||
|
|
||||||
import StyledCustomCheckbox from './StyledCustomCheckbox';
|
import StyledCustomCheckbox from './StyledCustomCheckbox';
|
||||||
|
|
||||||
class CustomCheckbox extends React.Component {
|
const CustomCheckbox = ({ label, name, onChange, value, ...rest }) => {
|
||||||
// eslint-disable-line react/prefer-stateless-function
|
const [checked, setChecked] = useState(isNumber(value) || !isEmpty(value));
|
||||||
state = {
|
|
||||||
isChecked: this.props.value !== null && this.props.value !== undefined,
|
|
||||||
};
|
|
||||||
|
|
||||||
handleChange = ({ target: { checked } }) => {
|
return (
|
||||||
this.setState({ isChecked: checked });
|
<StyledCustomCheckbox>
|
||||||
|
<Inputs
|
||||||
const { name, onChange } = this.props;
|
label={label}
|
||||||
const value = checked ? '' : null;
|
name={name}
|
||||||
const target = { name, value };
|
type="checkbox"
|
||||||
|
value={checked}
|
||||||
onChange({ target });
|
onChange={() => {
|
||||||
};
|
if (checked) {
|
||||||
|
onChange({
|
||||||
handleInputNumberChange = ({ target: { value } }) => {
|
target: {
|
||||||
const { name, onChange } = this.props;
|
name,
|
||||||
const target = {
|
value: null,
|
||||||
name,
|
},
|
||||||
type: 'number',
|
});
|
||||||
value: parseInt(value, 10),
|
}
|
||||||
};
|
setChecked(prev => !prev);
|
||||||
|
}}
|
||||||
onChange({ target });
|
/>
|
||||||
};
|
{checked && (
|
||||||
|
<Inputs
|
||||||
render() {
|
{...rest}
|
||||||
const { isChecked } = this.state;
|
name={name}
|
||||||
const { didCheckErrors, errors, label, name, value } = this.props;
|
onChange={onChange}
|
||||||
|
value={value}
|
||||||
// TODO: remove inline after migrating to Buffet
|
type="number"
|
||||||
return (
|
/>
|
||||||
<StyledCustomCheckbox>
|
)}
|
||||||
<FormattedMessage id={label.id}>
|
</StyledCustomCheckbox>
|
||||||
{msg => (
|
);
|
||||||
<label htmlFor={name}>
|
};
|
||||||
<input
|
|
||||||
checked={isChecked}
|
|
||||||
name={name}
|
|
||||||
id={name}
|
|
||||||
onChange={this.handleChange}
|
|
||||||
type="checkbox"
|
|
||||||
/>
|
|
||||||
<span>{msg}</span>
|
|
||||||
</label>
|
|
||||||
)}
|
|
||||||
</FormattedMessage>
|
|
||||||
{isChecked && (
|
|
||||||
<InputNumber
|
|
||||||
didCheckErrors={didCheckErrors}
|
|
||||||
errors={errors}
|
|
||||||
name={name}
|
|
||||||
onChange={this.handleInputNumberChange}
|
|
||||||
value={value || ''}
|
|
||||||
/>
|
|
||||||
)}
|
|
||||||
</StyledCustomCheckbox>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
CustomCheckbox.defaultProps = {
|
CustomCheckbox.defaultProps = {
|
||||||
didCheckErrors: false,
|
label: null,
|
||||||
errors: [],
|
|
||||||
label: {
|
|
||||||
id: 'app.utils.defaultMessage',
|
|
||||||
},
|
|
||||||
name: '',
|
name: '',
|
||||||
value: null,
|
value: null,
|
||||||
};
|
};
|
||||||
|
|
||||||
CustomCheckbox.propTypes = {
|
CustomCheckbox.propTypes = {
|
||||||
didCheckErrors: PropTypes.bool,
|
label: PropTypes.string,
|
||||||
errors: PropTypes.array,
|
|
||||||
label: PropTypes.shape({
|
|
||||||
id: PropTypes.string,
|
|
||||||
}),
|
|
||||||
name: PropTypes.string,
|
name: PropTypes.string,
|
||||||
onChange: PropTypes.func.isRequired,
|
onChange: PropTypes.func.isRequired,
|
||||||
value: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
|
value: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
|
||||||
|
@ -19,6 +19,7 @@ import pluginId from '../../pluginId';
|
|||||||
import useQuery from '../../hooks/useQuery';
|
import useQuery from '../../hooks/useQuery';
|
||||||
import useDataManager from '../../hooks/useDataManager';
|
import useDataManager from '../../hooks/useDataManager';
|
||||||
import AttributeOption from '../../components/AttributeOption';
|
import AttributeOption from '../../components/AttributeOption';
|
||||||
|
import CustomCheckbox from '../../components/CustomCheckbox';
|
||||||
import ModalHeader from '../../components/ModalHeader';
|
import ModalHeader from '../../components/ModalHeader';
|
||||||
import HeaderModalNavContainer from '../../components/HeaderModalNavContainer';
|
import HeaderModalNavContainer from '../../components/HeaderModalNavContainer';
|
||||||
import HeaderNavLink from '../../components/HeaderNavLink';
|
import HeaderNavLink from '../../components/HeaderNavLink';
|
||||||
@ -167,7 +168,7 @@ const FormModal = () => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (name === 'maxLength') {
|
if (name === 'maxLength') {
|
||||||
delete clonedErrors.maxLength;
|
delete clonedErrors.minLength;
|
||||||
}
|
}
|
||||||
|
|
||||||
delete clonedErrors[name];
|
delete clonedErrors[name];
|
||||||
@ -211,7 +212,6 @@ const FormModal = () => {
|
|||||||
});
|
});
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
const errors = getYupInnerErrors(err);
|
const errors = getYupInnerErrors(err);
|
||||||
console.log({ err, errors });
|
|
||||||
|
|
||||||
dispatch({
|
dispatch({
|
||||||
type: 'SET_ERRORS',
|
type: 'SET_ERRORS',
|
||||||
@ -341,6 +341,9 @@ const FormModal = () => {
|
|||||||
key={input.name}
|
key={input.name}
|
||||||
>
|
>
|
||||||
<Inputs
|
<Inputs
|
||||||
|
customInputs={{
|
||||||
|
customCheckboxWithChildren: CustomCheckbox,
|
||||||
|
}}
|
||||||
value={get(modifiedData, input.name, '')}
|
value={get(modifiedData, input.name, '')}
|
||||||
{...input}
|
{...input}
|
||||||
error={
|
error={
|
||||||
|
@ -6,7 +6,6 @@ const initialState = fromJS({
|
|||||||
});
|
});
|
||||||
|
|
||||||
const reducer = (state, action) => {
|
const reducer = (state, action) => {
|
||||||
console.log({ action: action.type });
|
|
||||||
switch (action.type) {
|
switch (action.type) {
|
||||||
case 'ON_CHANGE':
|
case 'ON_CHANGE':
|
||||||
return state.updateIn(
|
return state.updateIn(
|
||||||
|
@ -145,7 +145,8 @@ const forms = {
|
|||||||
{
|
{
|
||||||
autoFocus: false,
|
autoFocus: false,
|
||||||
name: type === 'number' ? 'max' : 'maxLength',
|
name: type === 'number' ? 'max' : 'maxLength',
|
||||||
type: 'number',
|
// type: 'number',
|
||||||
|
type: 'customCheckboxWithChildren',
|
||||||
label: {
|
label: {
|
||||||
id: getTrad(
|
id: getTrad(
|
||||||
`form.attribute.item.maximum${
|
`form.attribute.item.maximum${
|
||||||
@ -161,7 +162,7 @@ const forms = {
|
|||||||
{
|
{
|
||||||
autoFocus: false,
|
autoFocus: false,
|
||||||
name: type === 'number' ? 'min' : 'minLength',
|
name: type === 'number' ? 'min' : 'minLength',
|
||||||
type: 'number',
|
type: 'customCheckboxWithChildren',
|
||||||
label: {
|
label: {
|
||||||
id: getTrad(
|
id: getTrad(
|
||||||
`form.attribute.item.minimum${
|
`form.attribute.item.minimum${
|
||||||
@ -169,9 +170,6 @@ const forms = {
|
|||||||
}`
|
}`
|
||||||
),
|
),
|
||||||
},
|
},
|
||||||
// description: {
|
|
||||||
// id: getTrad('form.attribute.item.uniqueField.description'),
|
|
||||||
// },
|
|
||||||
validations: {},
|
validations: {},
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
|
Loading…
x
Reference in New Issue
Block a user