Skip to content

Commit

Permalink
added a multiselect box
Browse files Browse the repository at this point in the history
  • Loading branch information
Spiral-Memory committed Feb 26, 2024
1 parent 5185cb0 commit 2b97adc
Show file tree
Hide file tree
Showing 4 changed files with 153 additions and 12 deletions.
102 changes: 102 additions & 0 deletions packages/react/src/components/MultiSelect/MultiSelect.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
import React, { useState } from 'react';
import { css, useTheme } from '@emotion/react';
import PropTypes from 'prop-types';
import useComponentOverrides from '../../theme/useComponentOverrides';
import { Box } from '../Box';

const MultiSelect = ({
className = '',
style = {},
color = 'primary',
options = [],
onChange,
...props
}) => {
const { classNames, styleOverrides } = useComponentOverrides('MultiSelect');
const theme = useTheme();
const [selectedOptions, setSelectedOptions] = useState([]);

const handleOptionToggle = (value) => {
const isSelected = selectedOptions.includes(value);
if (isSelected) {
setSelectedOptions(selectedOptions.filter((item) => item !== value));
} else {
setSelectedOptions([...selectedOptions, value]);
}
};

const MultiSelectCss = css`
position: relative;
display: inline-flex;
flex: 1 0 auto;
min-width: 8rem;
padding: 0.5rem 0.9375rem;
vertical-align: baseline;
outline: 0;
background-color: transparent;
letter-spacing: 0rem;
font-size: 0.875rem;
font-weight: 400;
line-height: 1.25rem;
overflow: hidden;
color: #2f343d;
border-right: 0.9375rem transparent;
border-width: 1px;
border-color: #cbced1;
border-style: solid;
border-radius: 0.25rem;
background-color: white;
box-shadow: none;
-webkit-appearance: none;
appearance: none;
transition: all 230ms;
&:focus {
border-color: ${theme.palette[color].main || 'currentColor'};
box-shadow: 0px 0px 2.5px ${theme.palette[color].light || 'currentColor'};
}
`;

const CheckboxCss = css`
margin-right: 8px;
cursor: pointer;
`;

return (
<Box
css={MultiSelectCss}
className={`ec-multi-select ${className} ${classNames}`}
style={{ ...styleOverrides, ...style }}
{...props}
>
<Box>
{options.map((option) => (
// eslint-disable-next-line jsx-a11y/label-has-associated-control
<label key={option.value} css={CheckboxCss}>
<input
type="checkbox"
value={option.value}
checked={selectedOptions.includes(option.value)}
onChange={() => handleOptionToggle(option.value)}
/>
{option.label}
</label>
))}
</Box>
</Box>
);
};

MultiSelect.propTypes = {
className: PropTypes.string,
style: PropTypes.object,
color: PropTypes.string,
options: PropTypes.arrayOf(
PropTypes.shape({
value: PropTypes.string,
label: PropTypes.string,
})
),
onChange: PropTypes.func,
};

export default MultiSelect;
1 change: 1 addition & 0 deletions packages/react/src/components/MultiSelect/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default as MultiSelect } from './MultiSelect';
Original file line number Diff line number Diff line change
@@ -1,3 +1,43 @@
import React, { memo, useMemo, useCallback } from 'react';

import { useUiKitState } from '../hooks/useUiKitState';
import { fromTextObjectToString } from '../utils/fromTextObjectToString';
import { MultiSelect } from '../../MultiSelect';

const MultiStaticSelectElement = ({ block, context, surfaceRenderer }) => {
const [{ loading }, action] = useUiKitState(block, context);

const options = useMemo(
() =>
block.options.map((option, i) => ({
value: option.value,
label: fromTextObjectToString(surfaceRenderer, option.text, i) ?? '',
})),
[block.options, surfaceRenderer]
);

const handleChange = useCallback(
(val) => {
action({ target: { val } });
},
[action]
);

return (
<MultiSelect
options={options}
placeholder={fromTextObjectToString(
surfaceRenderer,
block.placeholder,
0
)}
onChange={handleChange}
/>
);
};

export default memo(MultiStaticSelectElement);

// import { MultiSelectFiltered } from '@rocket.chat/fuselage';
// import React, { memo, useCallback, useMemo } from 'react';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import ButtonElement from '../elements/ButtonElement';
import DatePickerElement from '../elements/DatePickerElement';
import ImageElement from '../elements/ImageElement';
import LinearScaleElement from '../elements/LinearScaleElement';
// import MultiStaticSelectElement from '../elements/MultiStaticSelectElement';
import MultiStaticSelectElement from '../elements/MultiStaticSelectElement';
import OverflowElement from '../elements/OverflowElement';
import PlainTextInputElement from '../elements/PlainTextInputElement';
import StaticSelectElement from '../elements/StaticSelectElement';
Expand Down Expand Up @@ -234,17 +234,15 @@ export class FuselageSurfaceRenderer extends UiKit.SurfaceRenderer {
return null;
}

return null;
// implement this without fuselage
// return (
// <MultiStaticSelectElement
// key={block.actionId || index}
// block={block}
// context={context}
// index={index}
// surfaceRenderer={this}
// />
// );
return (
<MultiStaticSelectElement
key={block.actionId || index}
block={block}
context={context}
index={index}
surfaceRenderer={this}
/>
);
}

overflow(block, context, index) {
Expand Down

0 comments on commit 2b97adc

Please sign in to comment.