Init custom checkbox that display children for min max settings

This commit is contained in:
soupette 2019-11-19 17:28:54 +01:00 committed by Alexandre Bodin
parent 98642ebbf9
commit 4c840e07ff
5 changed files with 58 additions and 91 deletions

View File

@ -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;

View File

@ -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 } }) => {
this.setState({ isChecked: checked });
const { name, onChange } = this.props;
const value = checked ? '' : null;
const target = { name, value };
onChange({ target });
};
handleInputNumberChange = ({ target: { value } }) => {
const { name, onChange } = this.props;
const target = {
name,
type: 'number',
value: parseInt(value, 10),
};
onChange({ target });
};
render() {
const { isChecked } = this.state;
const { didCheckErrors, errors, label, name, value } = this.props;
// TODO: remove inline after migrating to Buffet
return ( return (
<StyledCustomCheckbox> <StyledCustomCheckbox>
<FormattedMessage id={label.id}> <Inputs
{msg => ( label={label}
<label htmlFor={name}>
<input
checked={isChecked}
name={name} name={name}
id={name}
onChange={this.handleChange}
type="checkbox" type="checkbox"
value={checked}
onChange={() => {
if (checked) {
onChange({
target: {
name,
value: null,
},
});
}
setChecked(prev => !prev);
}}
/> />
<span>{msg}</span> {checked && (
</label> <Inputs
)} {...rest}
</FormattedMessage>
{isChecked && (
<InputNumber
didCheckErrors={didCheckErrors}
errors={errors}
name={name} name={name}
onChange={this.handleInputNumberChange} onChange={onChange}
value={value || ''} value={value}
type="number"
/> />
)} )}
</StyledCustomCheckbox> </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]),

View File

@ -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={

View File

@ -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(

View File

@ -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: {},
}, },
], ],