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

Set properly slider position for input switch #326

Merged
merged 7 commits into from
Jul 23, 2019
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
1 change: 0 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

106 changes: 70 additions & 36 deletions packages/Form/Input/switch/src/Switch.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,72 +20,106 @@ const defaultProps = {
};

const SwitchItems = props => {
const { options, value, name, onChange, onBlur, onFocus, disabled, ...otherProps } = props;
const {
options,
value,
name,
onChange,
onBlur,
onFocus,
disabled,
...otherProps
} = props;
return options.map(option => {
const isChecked = option.value === value;
return (<SwitchItem
key={option.value}
id={option.id}
value={option.value}
label={option.label}
isChecked={isChecked}
name={name}
onChange={onChange}
onBlur={onBlur}
onFocus={onFocus}
disabled={disabled}
{...omitProperties(otherProps)}
/>)
})
return (
<SwitchItem
key={option.value}
id={option.id}
value={option.value}
label={option.label}
isChecked={isChecked}
name={name}
onChange={onChange}
onBlur={onBlur}
onFocus={onFocus}
disabled={disabled}
{...omitProperties(otherProps)}
/>
);
});
};

export const getSliderPosition = (value, options, ref) => {
const option = options.find(v => v.value === value);
const emptyResult = { left: 0, width: 0 };
if (!option || !ref.current) {
return emptyResult;
}
const currentElementResult = ref.current.querySelectorAll(`input[value="${value}"]`);
export const getSliderPosition = (radioName, ref) => {
const currentElementResult =
ref.current &&
ref.current.querySelectorAll(`input[name="${radioName}"]:checked`);
if (!currentElementResult || currentElementResult.length !== 1) {
return emptyResult;
return { left: 0, width: 0 };
}
const currentElement = currentElementResult[0].parentNode;
const left = currentElement.offsetLeft;
const width = currentElement.clientWidth;
return {
left,
width
width,
};
};

class Switch extends React.Component {
constructor(props) {
super(props);
this.ref = React.createRef();
this.state = {
selectedValue: props.value,
sliderStyle: {
display: 'none',
},
};
}

render() {
const { value, options } = this.props;
const sliderPosition = getSliderPosition(value, options, this.ref);
const sliderStyle = {
componentDidMount() {
this.setState({
sliderStyle: this.getSliderStyle(),
});
}

getSliderStyle() {
guillaumechervetaxa marked this conversation as resolved.
Show resolved Hide resolved
const { name } = this.props;
const sliderPosition = getSliderPosition(name, this.ref);
return {
width: `${sliderPosition.width}px`,
left: `${sliderPosition.left}px`,
};
}

handleChange(e, onChange) {
this.setState({
selectedValue: e.currentTarget.value,
sliderStyle: this.getSliderStyle(),
});
onChange(e);
}

render() {
const { onChange, ...props } = this.props;
const { selectedValue: value, sliderStyle } = this.state;
return (
<div className="af-form-switch" ref={this.ref}>
<SwitchItems {...this.props} />
<SwitchItems
{...props}
onChange={e => this.handleChange(e, onChange)}
value={value}
/>
<span className="af-btn-switch-slider" style={sliderStyle} />
</div>
);
}
};

}

const EnhancedComponent = withInput(
defaultClassName,
propTypes,
defaultProps
)(Switch);
const EnhancedComponent = withInput(defaultClassName, propTypes, defaultProps)(
Switch
);

EnhancedComponent.displayName = 'SwitchInput';

Expand Down
1 change: 0 additions & 1 deletion packages/Form/Input/switch/src/SwitchInput.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ describe('<SwitchInput>', () => {
id="iddateinput"
name="placeImage"
options={[]}
values={[]}
onChange={() => {}}
/>
)
Expand Down
1 change: 1 addition & 0 deletions packages/Form/Input/switch/src/switch.scss
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
box-sizing: border-box;
z-index: 1;
cursor: pointer;
user-select: none;

.af-form__label,
.af-form__input-radio:checked + .af-form__label,
Expand Down
4 changes: 2 additions & 2 deletions packages/tabs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
},
"dependencies": {
"@axa-fr/react-toolkit-core": "^1.2.8",
"classnames": "^2.2.6",
"prop-types": "^15.5.10",
"recompose": "^0.27.1"
"recompose": "^0.27.1",
"classnames": "^2.2.6"
guillaumechervetaxa marked this conversation as resolved.
Show resolved Hide resolved
},
"license": "MIT",
"publishConfig": {
Expand Down